main.js 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  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 of commands) {
  43. if (typeof(c.pattern) == "string" && lowerCaseContent.startsWith(c.pattern)) {
  44. c.action(m, content);
  45. return;
  46. }
  47. else if(c.pattern instanceof RegExp){
  48. let result = c.pattern.exec(content);
  49. if(result != null){
  50. c.action(m, content, result);
  51. return;
  52. }
  53. }
  54. }
  55. if (trigger(directMessageActions, m, lowerCaseContent))
  56. return;
  57. }
  58. if (trigger(indirectMentionActions, m))
  59. return;
  60. }
  61. trigger(postActions);
  62. });
  63. client.on("messageReactionAdd", (r, u) => {
  64. if (Math.random() <= REACT_PROBABILITY && !r.me)
  65. r.message.react(r.emoji);
  66. });
  67. function main() {
  68. let commandsPath = path.resolve(path.dirname(module.filename), "commands");
  69. let files = fs.readdirSync(commandsPath);
  70. for (let i = 0; i < files.length; i++) {
  71. const file = files[i];
  72. let ext = path.extname(file);
  73. if (ext != ".js")
  74. continue;
  75. let obj = require(path.resolve(commandsPath, file));
  76. if (obj.commands) {
  77. for (let command of obj.commands) {
  78. // if (obj.commands.hasOwnProperty(command))
  79. // commands[command] = obj.commands[command];
  80. commands.push(command);
  81. }
  82. }
  83. if (obj.documentation) {
  84. for (let command in obj.documentation) {
  85. if (obj.documentation.hasOwnProperty(command))
  86. util.documentation[command] = obj.documentation[command];
  87. }
  88. }
  89. if (obj.onMessage)
  90. msgActions.push(obj.onMessage);
  91. if (obj.onIndirectMention)
  92. indirectMentionActions.push(obj.onIndirectMention);
  93. if (obj.onDirectMention)
  94. directMessageActions.push(obj.onDirectMention);
  95. if (obj.postMessage)
  96. postActions.push(obj.postMessage);
  97. if (obj.onStart)
  98. startActions.push(obj.onStart);
  99. }
  100. client.login(TOKEN);
  101. }
  102. main();