main.js 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  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.3;
  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(process.env.NODE_ENV == "dev" ? "Maintenance" : "@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 (!util.shouldShowMaintenanceMessage(m.guild.id) && trigger(msgActions, m, content))
  37. return;
  38. if (m.mentions.users.size > 0 && m.mentions.users.first().id == client.user.id) {
  39. if(util.shouldShowMaintenanceMessage(m.guild.id)) {
  40. m.channel.send(`${m.author.toString()} I'm currently being maintained; please wait.`);
  41. return;
  42. }
  43. if (content.startsWith(`@${client.user.username}`)) {
  44. content = content.substring(`@${client.user.username}`.length).trim();
  45. let lowerCaseContent = content.toLowerCase();
  46. for (let c of commands) {
  47. if (typeof(c.pattern) == "string" && lowerCaseContent.startsWith(c.pattern)) {
  48. c.action(m, content);
  49. return;
  50. }
  51. else if(c.pattern instanceof RegExp){
  52. let result = c.pattern.exec(content);
  53. if(result != null){
  54. c.action(m, content, result);
  55. return;
  56. }
  57. }
  58. }
  59. if (trigger(directMessageActions, m, lowerCaseContent))
  60. return;
  61. }
  62. if (trigger(indirectMentionActions, m))
  63. return;
  64. }
  65. if(!util.shouldShowMaintenanceMessage(m.guild.id))
  66. trigger(postActions);
  67. });
  68. client.on("messageReactionAdd", (r, u) => {
  69. if (Math.random() <= REACT_PROBABILITY && !r.me) {
  70. console.log(`Reacting to message ${r.message.id} because user ${u.tag} reacted to it`);
  71. r.message.react(r.emoji);
  72. }
  73. });
  74. function main() {
  75. let commandsPath = path.resolve(path.dirname(module.filename), "commands");
  76. let files = fs.readdirSync(commandsPath);
  77. for (let i = 0; i < files.length; i++) {
  78. const file = files[i];
  79. let ext = path.extname(file);
  80. if (ext != ".js")
  81. continue;
  82. let obj = require(path.resolve(commandsPath, file));
  83. if (obj.commands) {
  84. for (let command of obj.commands) {
  85. // if (obj.commands.hasOwnProperty(command))
  86. // commands[command] = obj.commands[command];
  87. commands.push(command);
  88. }
  89. }
  90. if (obj.documentation) {
  91. for (let command in obj.documentation) {
  92. if (obj.documentation.hasOwnProperty(command))
  93. util.documentation[command] = obj.documentation[command];
  94. }
  95. }
  96. if (obj.onMessage)
  97. msgActions.push(obj.onMessage);
  98. if (obj.onIndirectMention)
  99. indirectMentionActions.push(obj.onIndirectMention);
  100. if (obj.onDirectMention)
  101. directMessageActions.push(obj.onDirectMention);
  102. if (obj.postMessage)
  103. postActions.push(obj.postMessage);
  104. if (obj.onStart)
  105. startActions.push(obj.onStart);
  106. }
  107. client.login(TOKEN);
  108. }
  109. main();