main.js 3.2 KB

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