main.ts 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  1. import * as dotenv from "dotenv";
  2. dotenv.config();
  3. import * as fs from "fs";
  4. import * as path from "path";
  5. import { client } from "./client";
  6. import { ICommand, BotEvent, IBotCommand } from "./commands/command"
  7. import "reflect-metadata";
  8. import {createConnection} from "typeorm";
  9. import { migrate } from "./lowdb_migrator";
  10. import { documentation } from "./util";
  11. const REACT_PROBABILITY = 0.3;
  12. async function trigger(actions : BotEvent[], ...params: any[]) {
  13. let actionDone = false;
  14. for (let i = 0; i < actions.length; i++) {
  15. const action = actions[i];
  16. let actionResult = action(actionDone, ...params);
  17. if(actionResult instanceof Promise)
  18. actionDone = (await actionResult) || actionDone;
  19. else
  20. actionDone = actionResult || actionDone;
  21. }
  22. return actionDone;
  23. }
  24. let commands : IBotCommand[] = [];
  25. let msgActions : BotEvent[] = [];
  26. let indirectMentionActions : BotEvent[] = [];
  27. let startActions : Array<() => void | Promise<void>> = [];
  28. let directMessageActions : BotEvent[] = [];
  29. let postActions : BotEvent[] = [];
  30. client.on("ready", async () => {
  31. console.log("Starting up NoctBot!");
  32. client.user.setActivity(process.env.NODE_ENV == "dev" ? "Maintenance" : "@NoctBot help", {
  33. type: "PLAYING"
  34. });
  35. for (let i = 0; i < startActions.length; i++) {
  36. const action = startActions[i];
  37. let val = action();
  38. if(val instanceof Promise)
  39. await val;
  40. }
  41. console.log("NoctBot is ready!");
  42. });
  43. client.on("message", async m => {
  44. if (m.author.id == client.user.id)
  45. return;
  46. let content = m.cleanContent.trim();
  47. if (await trigger(msgActions, m, content))
  48. return;
  49. if (m.mentions.users.size > 0 && m.mentions.users.has(client.user.id)) {
  50. if (m.content.trim().startsWith(client.user.toString())) {
  51. content = content.substring(`@${client.user.username}`.length).trim();
  52. let lowerCaseContent = content.toLowerCase();
  53. for (let c of commands) {
  54. if (typeof(c.pattern) == "string" && lowerCaseContent.startsWith(c.pattern)) {
  55. c.action(m, content);
  56. return;
  57. }
  58. else if(c.pattern instanceof RegExp){
  59. let result = c.pattern.exec(content);
  60. if(result != null){
  61. c.action(m, content, result);
  62. return;
  63. }
  64. }
  65. }
  66. if (await trigger(directMessageActions, m, lowerCaseContent))
  67. return;
  68. }
  69. if (await trigger(indirectMentionActions, m))
  70. return;
  71. }
  72. await trigger(postActions);
  73. });
  74. client.on("messageReactionAdd", (r, u) => {
  75. if (Math.random() <= REACT_PROBABILITY && !u.bot) {
  76. console.log(`Reacting to message ${r.message.id} because user ${u.tag} reacted to it`);
  77. r.message.react(r.emoji);
  78. }
  79. });
  80. async function main() {
  81. await createConnection();
  82. await migrate();
  83. let commandsPath = path.resolve(path.dirname(module.filename), "commands");
  84. let files = fs.readdirSync(commandsPath);
  85. for (const file of files) {
  86. let ext = path.extname(file);
  87. let name = path.basename(file);
  88. if(name == "command.js")
  89. continue;
  90. if (ext != ".js")
  91. continue;
  92. let obj = require(path.resolve(commandsPath, file)).default as ICommand;
  93. if (obj.commands)
  94. for (let command of obj.commands) {
  95. commands.push(command);
  96. }
  97. if (obj.documentation)
  98. for (let command in obj.documentation) {
  99. if (obj.documentation.hasOwnProperty(command))
  100. documentation[command] = obj.documentation[command];
  101. }
  102. if (obj.onMessage)
  103. msgActions.push(obj.onMessage);
  104. if (obj.onIndirectMention)
  105. indirectMentionActions.push(obj.onIndirectMention);
  106. if (obj.onDirectMention)
  107. directMessageActions.push(obj.onDirectMention);
  108. if (obj.postMessage)
  109. postActions.push(obj.postMessage);
  110. if (obj.onStart)
  111. startActions.push(obj.onStart);
  112. }
  113. client.login(process.env.TOKEN);
  114. }
  115. main();