main.ts 2.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  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 { DB_ENTITIES } from "@shared/db/entities";
  8. import { assertOk } from "@shared/common/async_utils";
  9. import { logger } from "./logging";
  10. import { PluginManager } from "./plugin_manager";
  11. import { startRpcServer } from "./rpc";
  12. export const plgMgr: PluginManager = new PluginManager(path.resolve(path.dirname(module.filename), "plugins"));
  13. export const COMMAND_PREFIX = "/";
  14. client.bot.on("ready", async () => {
  15. logger.info("Starting up NoctBot");
  16. await client.botUser.setActivity(`@${client.botUser.username} help`, { type: "PLAYING" });
  17. await assertOk(plgMgr.start(client.bot));
  18. logger.info("NoctBot is ready");
  19. });
  20. client.bot.on("message", async m => {
  21. if (m.author.id == client.botUser.id)
  22. return;
  23. if (m.channel.type != "text") {
  24. 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);
  25. await m.reply("DM commands are disabled, sorry!");
  26. return;
  27. }
  28. if (m.content.startsWith(COMMAND_PREFIX) && await plgMgr.runCommand("prefix", m, m.content.substring(COMMAND_PREFIX.length))) {
  29. return;
  30. }
  31. let content = m.cleanContent.trim();
  32. if (await plgMgr.trigger("message", m, content))
  33. return;
  34. if (m.mentions.users.size > 0 && m.mentions.users.has(client.botUser.id)) {
  35. const trimmedContent = m.content.trim();
  36. if (trimmedContent.startsWith(client.nameMention) || trimmedContent.startsWith(client.usernameMention)) {
  37. content = content.substring(`@${client.botUser.username}`.length).trim();
  38. const lowerCaseContent = content.toLowerCase();
  39. if (await plgMgr.runCommand("mention", m, content))
  40. return;
  41. if (await plgMgr.trigger("directMention", m, lowerCaseContent))
  42. return;
  43. }
  44. if (await plgMgr.trigger("indirectMention", m))
  45. return;
  46. }
  47. await plgMgr.trigger("postMessage", m);
  48. });
  49. async function main() {
  50. await createConnection({
  51. ...await getConnectionOptions(),
  52. entities: DB_ENTITIES
  53. });
  54. startRpcServer();
  55. client.bot.login(process.env.BOT_TOKEN);
  56. }
  57. main();