main.ts 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  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. client.bot.on("ready", async () => {
  13. logger.info("Starting up NoctBot");
  14. await client.botUser.setActivity(`@${client.botUser.username} help`, { type: "PLAYING" });
  15. await assertOk(plgMgr.start(client.bot));
  16. logger.info("NoctBot is ready");
  17. });
  18. client.bot.on("message", async m => {
  19. if (m.author.id == client.botUser.id)
  20. return;
  21. if (m.channel.type != "text") {
  22. 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);
  23. await m.reply("DM commands are disabled, sorry!");
  24. return;
  25. }
  26. let content = m.cleanContent.trim();
  27. if (await plgMgr.trigger("message", m, content))
  28. return;
  29. if (m.mentions.users.size > 0 && m.mentions.users.has(client.botUser.id)) {
  30. const trimmedContent = m.content.trim();
  31. if (trimmedContent.startsWith(client.nameMention) || trimmedContent.startsWith(client.usernameMention)) {
  32. content = content.substring(`@${client.botUser.username}`.length).trim();
  33. const lowerCaseContent = content.toLowerCase();
  34. if (await plgMgr.runCommand(m, content))
  35. return;
  36. if (await plgMgr.trigger("directMention", m, lowerCaseContent))
  37. return;
  38. }
  39. if (await plgMgr.trigger("indirectMention", m))
  40. return;
  41. }
  42. await plgMgr.trigger("postMessage", m);
  43. });
  44. async function main() {
  45. await createConnection({
  46. ...await getConnectionOptions(),
  47. entities: DB_ENTITIES
  48. });
  49. client.bot.login(process.env.BOT_TOKEN);
  50. }
  51. main();