diff --git a/packages/bot/src/index.ts b/packages/bot/src/index.ts index 7d1ae20..9c960d7 100644 --- a/packages/bot/src/index.ts +++ b/packages/bot/src/index.ts @@ -8,10 +8,14 @@ import TempBan from "automod-lib/dist/types/TempBan"; import ReactionRoles from "automod-lib/dist/types/ReactionRoles"; import { Collection } from "mongodb"; +process.on("SIGINT", () => process.exit(0)); +process.on("SIGTERM", () => { + client?.disconnect(); + process.exit(0); +}); + process.on("unhandledRejection", (reason, _promise) => { console.error("Unhandled promise rejection:", reason); - // Let the process exit so Docker restarts it — an unhandled rejection leaves - // the process in an undefined state. Node 15+ exits anyway; be explicit. process.exitCode = 1; }); diff --git a/packages/bot/src/stoat/Client.ts b/packages/bot/src/stoat/Client.ts index f8f1065..85f2ad6 100644 --- a/packages/bot/src/stoat/Client.ts +++ b/packages/bot/src/stoat/Client.ts @@ -113,6 +113,12 @@ export class Client extends EventEmitter { this.connect(); } + disconnect(): void { + clearTimeout(this._reconnectTimeout); + this.options.autoReconnect = false; + this.events.disconnect(); + } + /** * Process an array in chunks, yielding to the event loop between chunks * so heartbeats and other I/O are not starved during large data loads.