index.js 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  1. import Sentry from '@sentry/node';
  2. import bancho from './bancho.js';
  3. import commands from './commands.js';
  4. import db from './database.js';
  5. import {init as init_discord_interactions} from './discord_interactions.js';
  6. import {init as init_discord_updates} from './discord_updates.js';
  7. import {listen as website_listen} from './website.js';
  8. import Config from './util/config.js';
  9. import {capture_sentry_exception} from './util/helpers.js';
  10. import {init_the_ones} from './elo_cache.js';
  11. import {auto_rejoin_lobbies} from './supervisor.js';
  12. async function init_discord() {
  13. let discord_client = null;
  14. try {
  15. discord_client = await init_discord_interactions();
  16. await init_discord_updates(discord_client);
  17. } catch (err) {
  18. console.error('Failed to login to Discord:', err.message);
  19. process.exit();
  20. }
  21. }
  22. async function main() {
  23. console.log('Starting...');
  24. if (Config.ENABLE_SENTRY) {
  25. Sentry.init({
  26. dsn: Config.sentry_dsn,
  27. });
  28. }
  29. init_the_ones();
  30. bancho.on('pm', (msg) => {
  31. for (const cmd of commands) {
  32. const match = cmd.regex.exec(msg.message);
  33. if (match) {
  34. if (!cmd.modes.includes('pm')) {
  35. bancho.privmsg(msg.from, 'You should send that command in #multiplayer.');
  36. return;
  37. }
  38. cmd.handler(msg, match, null).catch(capture_sentry_exception);
  39. return;
  40. }
  41. }
  42. });
  43. if (Config.CONNECT_TO_DISCORD) {
  44. init_discord();
  45. }
  46. if (Config.HOST_WEBSITE) {
  47. website_listen();
  48. }
  49. if (Config.CONNECT_TO_BANCHO) {
  50. bancho.on('disconnect', () => {
  51. db.close();
  52. process.exit();
  53. });
  54. await bancho.connect();
  55. await auto_rejoin_lobbies();
  56. }
  57. console.log('All ready and fired up!');
  58. }
  59. main();