main.ts 2.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  1. // We need some kind of module resolver for @shared/db. We use module-alias.
  2. require("module-alias/register");
  3. import "./environment";
  4. import * as path from "path";
  5. import { client } from "./client";
  6. import { createConnection, getConnectionOptions } from "typeorm";
  7. import { assertOk } from "./util";
  8. import { DB_ENTITIES } from "@shared/db/entities";
  9. import { logger } from "./logging";
  10. import { PluginManager } from "./plugin_manager";
  11. export const plgMgr: PluginManager = new PluginManager(path.resolve(path.dirname(module.filename), "plugins"));
  12. export const COMMAND_PREFIX = "/";
  13. client.bot.on("ready", async () => {
  14. logger.info("Starting up NoctBot");
  15. await client.botUser.setActivity(`@${client.botUser.username} help`, { type: "PLAYING" });
  16. await assertOk(plgMgr.start(client.bot));
  17. logger.info("NoctBot is ready");
  18. });
  19. client.bot.on("message", async m => {
  20. if (m.author.id == client.botUser.id)
  21. return;
  22. if (m.channel.type != "text") {
  23. logger.warn("User %s (%s#%s) tried to execute command in DMs. Message: %s.", m.author.id, m.author.username, m.author.discriminator, m.content);
  24. await m.reply("DM commands are disabled, sorry!");
  25. return;
  26. }
  27. if (m.content.startsWith(COMMAND_PREFIX) && await plgMgr.runCommand("prefix", m, m.content.substring(COMMAND_PREFIX.length))) {
  28. return;
  29. }
  30. let content = m.cleanContent.trim();
  31. if (await plgMgr.trigger("message", m, content))
  32. return;
  33. if (m.mentions.users.size > 0 && m.mentions.users.has(client.botUser.id)) {
  34. const trimmedContent = m.content.trim();
  35. if (trimmedContent.startsWith(client.nameMention) || trimmedContent.startsWith(client.usernameMention)) {
  36. content = content.substring(`@${client.botUser.username}`.length).trim();
  37. const lowerCaseContent = content.toLowerCase();
  38. if (await plgMgr.runCommand("mention", m, content))
  39. return;
  40. if (await plgMgr.trigger("directMention", m, lowerCaseContent))
  41. return;
  42. }
  43. if (await plgMgr.trigger("indirectMention", m))
  44. return;
  45. }
  46. await plgMgr.trigger("postMessage", m);
  47. });
  48. async function main() {
  49. await createConnection({
  50. ...await getConnectionOptions(),
  51. entities: DB_ENTITIES
  52. });
  53. client.bot.login(process.env.BOT_TOKEN);
  54. }
  55. main();