main.ts 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183
  1. // We need some kind of module resolver for @shared/db. We use module-alias.
  2. require("module-alias/register");
  3. import dotenv from "dotenv";
  4. if (process.env.NODE_ENV == "dev") {
  5. dotenv.config({
  6. path: "../.env"
  7. });
  8. dotenv.config({
  9. path: "../db.env"
  10. });
  11. process.env.TYPEORM_HOST = "localhost";
  12. process.env.TYPEORM_USERNAME = process.env.DB_USERNAME;
  13. process.env.TYPEORM_PASSWORD = process.env.DB_PASSWORD;
  14. process.env.TYPEORM_DATABASE = process.env.DB_NAME;
  15. }
  16. import * as fs from "fs";
  17. import * as path from "path";
  18. import { client } from "./client";
  19. import * as mCmd from "./model/command";
  20. import "reflect-metadata";
  21. import { createConnection, getConnectionOptions } from "typeorm";
  22. import { getNumberEnums } from "./util";
  23. import { DB_ENTITIES } from "@shared/db/entities";
  24. import { logger } from "./logging";
  25. const REACT_PROBABILITY = 0.3;
  26. async function trigger(type: mCmd.ActionType, ...params: unknown[]) {
  27. let actionDone = false;
  28. const actions = botEvents[type];
  29. for (let i = 0; i < actions.length; i++) {
  30. const action = actions[i] as (...args: unknown[]) => boolean | Promise<boolean>;
  31. const actionResult = action(actionDone, ...params);
  32. if (actionResult instanceof Promise)
  33. actionDone = (await actionResult) || actionDone;
  34. else
  35. actionDone = actionResult || actionDone;
  36. }
  37. return actionDone;
  38. }
  39. type BotEventCollection = { [event in mCmd.ActionType]: mCmd.BotAction[] };
  40. const commandSets: mCmd.ICommand[] = [];
  41. const botCommands: mCmd.IBotCommand[] = [];
  42. const botEvents: BotEventCollection = getNumberEnums(mCmd.ActionType).reduce((p, c) => { p[c as mCmd.ActionType] = []; return p; }, {} as BotEventCollection);
  43. const startActions: Array<() => void | Promise<void>> = [];
  44. client.bot.on("ready", async () => {
  45. logger.info("Starting up NoctBot");
  46. client.botUser.setActivity(process.env.NODE_ENV == "dev" ? "Maintenance" : "@NoctBot help", {
  47. type: "PLAYING"
  48. });
  49. for (let i = 0; i < startActions.length; i++) {
  50. const action = startActions[i];
  51. const val = action();
  52. if (val instanceof Promise)
  53. await val;
  54. }
  55. logger.info("NoctBot is ready");
  56. });
  57. client.bot.on("message", async m => {
  58. if (m.author.id == client.botUser.id)
  59. return;
  60. if(process.env.FOOLS == "TRUE" && (m.channel.id == "297109482905796608" || m.channel.id == "429295461099110402") && Math.random() <= 0.01) {
  61. const neighs = ["*NEIGH*", "neeeeeigh!", "Gimme carrots!", "NEEEEIIIIGH", "**N E I G H**"];
  62. await m.channel.send(neighs[Math.floor(Math.random() * neighs.length)]);
  63. return;
  64. }
  65. let content = m.cleanContent.trim();
  66. if (await trigger(mCmd.ActionType.MESSAGE, m, content))
  67. return;
  68. if (m.mentions.users.size > 0 && m.mentions.users.has(client.botUser.id)) {
  69. const trimmedContent = m.content.trim();
  70. if (trimmedContent.startsWith(client.nameMention) || trimmedContent.startsWith(client.usernameMention)) {
  71. content = content.substring(`@${client.botUser.username}`.length).trim();
  72. const lowerCaseContent = content.toLowerCase();
  73. for (const c of botCommands) {
  74. if (typeof (c.pattern) == "string" && lowerCaseContent.startsWith(c.pattern)) {
  75. c.action(m, content);
  76. return;
  77. }
  78. else if (c.pattern instanceof RegExp) {
  79. const result = c.pattern.exec(content);
  80. if (result != null) {
  81. c.action(m, content, result);
  82. return;
  83. }
  84. }
  85. }
  86. if (await trigger(mCmd.ActionType.DIRECT_MENTION, m, lowerCaseContent))
  87. return;
  88. }
  89. if (await trigger(mCmd.ActionType.INDIRECT_MENTION, m))
  90. return;
  91. }
  92. await trigger(mCmd.ActionType.POST_MESSAGE);
  93. });
  94. client.bot.on("messageReactionAdd", (r, u) => {
  95. if (Math.random() <= REACT_PROBABILITY && !u.bot) {
  96. logger.verbose(`Reacting to message ${r.message.id} because user ${u.tag} reacted to it`);
  97. r.message.react(r.emoji);
  98. }
  99. });
  100. function loadCommand(mod: Record<string, unknown>) {
  101. for (const i in mod) {
  102. if (!Object.prototype.hasOwnProperty.call(mod, i))
  103. continue;
  104. const commandClass = mod[i] as unknown;
  105. // Ensure this is indeed a command class
  106. if (!mCmd.isCommandSet(commandClass))
  107. continue;
  108. const cmd = new commandClass();
  109. commandSets.push(cmd);
  110. if (cmd._botCommands)
  111. botCommands.push(...cmd._botCommands.map(c => ({ ...c, action: c.action.bind(cmd) })));
  112. if (cmd._botEvents)
  113. for (const [i, event] of Object.entries(cmd._botEvents)) {
  114. botEvents[+i as mCmd.ActionType].push((event as mCmd.BotAction).bind(cmd));
  115. }
  116. if(cmd.onStart)
  117. startActions.push(cmd.onStart.bind(cmd));
  118. }
  119. }
  120. interface IDocumentationData {
  121. name: string;
  122. doc?: string;
  123. example?: string;
  124. auth: boolean;
  125. }
  126. export function getDocumentation() : IDocumentationData[] {
  127. return botCommands.filter(m => m.documentation !== undefined).map(m => ({
  128. name: m.pattern.toString(),
  129. doc: m.documentation?.description,
  130. example: m.documentation?.example,
  131. auth: m.auth || false
  132. }));
  133. }
  134. async function main() {
  135. await createConnection({
  136. ...await getConnectionOptions(),
  137. entities: DB_ENTITIES
  138. });
  139. logger.error("Failed! Oh noes!");
  140. const commandsPath = path.resolve(path.dirname(module.filename), "commands");
  141. const files = fs.readdirSync(commandsPath);
  142. for (const file of files) {
  143. const ext = path.extname(file);
  144. if (ext != ".js")
  145. continue;
  146. // eslint-disable-next-line @typescript-eslint/no-var-requires
  147. loadCommand(require(path.resolve(commandsPath, file)));
  148. }
  149. client.bot.login(process.env.BOT_TOKEN);
  150. }
  151. main();