main.js 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  1. const TOKEN = require("./token.js");
  2. const fs = require("fs");
  3. const path = require("path");
  4. const client = require("./client.js");
  5. const util = require("./util.js");
  6. const REACT_PROBABILITY = 0.6;
  7. function trigger(actions, ...params) {
  8. let actionDone = false;
  9. for (let i = 0; i < actions.length; i++) {
  10. const action = actions[i];
  11. actionDone |= action(...params, actionDone);
  12. }
  13. return actionDone;
  14. }
  15. let commands = {};
  16. let msgActions = [];
  17. let indirectMentionActions = [];
  18. let startActions = [];
  19. let directMessageActions = [];
  20. let postActions = [];
  21. client.on("ready", () => {
  22. console.log("Starting up NoctBot!");
  23. client.user.setActivity("@NoctBot help", {
  24. type: "PLAYING"
  25. });
  26. for (let i = 0; i < startActions.length; i++) {
  27. const action = startActions[i];
  28. action();
  29. }
  30. console.log("NoctBot is ready!");
  31. });
  32. client.on("message", m => {
  33. if (m.author.id == client.user.id)
  34. return;
  35. let content = m.cleanContent.trim();
  36. if (trigger(msgActions, m, content))
  37. return;
  38. if (m.mentions.users.size > 0 && m.mentions.users.first().id == client.user.id) {
  39. if (content.startsWith(`@${client.user.username}`)) {
  40. content = content.substring(`@${client.user.username}`.length).trim();
  41. let lowerCaseContent = content.toLowerCase();
  42. for (let c in commands) {
  43. if (lowerCaseContent.startsWith(c)) {
  44. commands[c](m, content);
  45. return;
  46. }
  47. }
  48. if (trigger(directMessageActions, m, lowerCaseContent))
  49. return;
  50. }
  51. if (trigger(indirectMentionActions, m))
  52. return;
  53. }
  54. trigger(postActions);
  55. });
  56. client.on("messageReactionAdd", (r, u) => {
  57. if (Math.random() <= REACT_PROBABILITY && !r.me)
  58. r.message.react(r.emoji);
  59. });
  60. function main() {
  61. let commandsPath = path.resolve(path.dirname(module.filename), "commands");
  62. let files = fs.readdirSync(commandsPath);
  63. for (let i = 0; i < files.length; i++) {
  64. const file = files[i];
  65. let ext = path.extname(file);
  66. if (ext != ".js")
  67. continue;
  68. let obj = require(path.resolve(commandsPath, file));
  69. if (obj.commands) {
  70. for (let command in obj.commands) {
  71. if (obj.commands.hasOwnProperty(command))
  72. commands[command] = obj.commands[command];
  73. }
  74. }
  75. if (obj.documentation) {
  76. for (let command in obj.documentation) {
  77. if (obj.documentation.hasOwnProperty(command))
  78. util.documentation[command] = obj.documentation[command];
  79. }
  80. }
  81. if (obj.onMessage)
  82. msgActions.push(obj.onMessage);
  83. if (obj.onIndirectMention)
  84. indirectMentionActions.push(obj.onIndirectMention);
  85. if (obj.onDirectMention)
  86. directMessageActions.push(obj.onDirectMention);
  87. if (obj.postMessage)
  88. postActions.push(obj.postMessage);
  89. if (obj.onStart)
  90. startActions.push(obj.onStart);
  91. }
  92. client.login(TOKEN);
  93. }
  94. main();