diff --git a/.vscode/settings.json b/.vscode/settings.json index b46109f1e..4ceaf27d9 100644 --- a/.vscode/settings.json +++ b/.vscode/settings.json @@ -6,6 +6,7 @@ "SKLLD", "Smri", "tankopedia", + "Unregistering", "wargaming", "winrate" ], diff --git a/src/behaviors/cleanup.ts b/src/behaviors/cleanup.ts new file mode 100644 index 000000000..0edd9d861 --- /dev/null +++ b/src/behaviors/cleanup.ts @@ -0,0 +1,41 @@ +import { Client, Routes } from 'discord.js'; +import { exit } from 'process'; +import discord from '../../discord.json' assert { type: 'json' }; +import getClientId from '../utilities/getClientId.js'; +import { rest } from './interactionCreate.js'; + +export const EXIT_EVENT_NAMES = [ + 'exit', + 'SIGINT', + 'SIGUSR1', + 'SIGUSR2', + 'uncaughtException', +]; + +export default function cleanup(client: Client) { + console.log('Cleaning up...'); + + console.log('Unregistering public commands...'); + rest.put(Routes.applicationCommands(getClientId()), { body: [] }); + + console.log('Unregistering guild commands...'); + rest.put(Routes.applicationGuildCommands(getClientId(), discord.guild_id), { + body: [], + }); + + console.log('Destroying client...'); + try { + client.destroy(); + } catch (e) { + console.error('Failed to destroy client'); + console.error(e); + } + + console.log('Removing all exit event listeners...'); + EXIT_EVENT_NAMES.forEach((exitEventName) => + process.off(exitEventName, () => cleanup(client)), + ); + + console.log('Exiting...'); + exit(); +} diff --git a/src/behaviors/interactionCreate.ts b/src/behaviors/interactionCreate.ts index 9397ea904..4e3a512cb 100644 --- a/src/behaviors/interactionCreate.ts +++ b/src/behaviors/interactionCreate.ts @@ -22,7 +22,7 @@ export interface CommandRegistry { execute: (interaction: ChatInputCommandInteraction) => void; } -const rest = new REST().setToken(process.env.DISCORD_TOKEN!); +export const rest = new REST().setToken(process.env.DISCORD_TOKEN!); const commandFolders = readdirSync('src/commands/'); const commandCollection = new Collection(); const guildCommands: RESTPostAPIChatInputApplicationCommandsJSONBody[] = []; diff --git a/src/commands/inactive.ts b/src/commands/inactive.ts index ba58200b1..70e3864bf 100644 --- a/src/commands/inactive.ts +++ b/src/commands/inactive.ts @@ -11,7 +11,7 @@ const DEFAULT_THRESHOLD = 7; export default { inProduction: true, - inDevelopment: false, + inDevelopment: true, inPublic: true, command: new SlashCommandBuilder() diff --git a/src/index.ts b/src/index.ts index 62278be11..183e7eba1 100644 --- a/src/index.ts +++ b/src/index.ts @@ -1,4 +1,5 @@ import { Client, GatewayIntentBits } from 'discord.js'; +import cleanup, { EXIT_EVENT_NAMES } from './behaviors/cleanup.js'; import guildMemberAdd from './behaviors/guildMemberAdd.js'; import interactionCreate from './behaviors/interactionCreate.js'; import ready from './behaviors/ready.js'; @@ -15,3 +16,7 @@ client.on('ready', ready); client.on('guildMemberAdd', guildMemberAdd); client.on('interactionCreate', interactionCreate); client.login(process.env.DISCORD_TOKEN); + +EXIT_EVENT_NAMES.forEach((exitEventName) => + process.on(exitEventName, () => cleanup(client)), +);