feat: cleanup code (which probably does nothing but I am hopeful anyway)

This commit is contained in:
TresAbhi committed 2023-04-26 01:56:12 +00:00
1 parent 57cc67c8be
commit 91260aed2a
5 files changed
+49 -2

No files matched your search

+41
View File
@@ -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();
}
+1 -1
View File
@@ -22,7 +22,7 @@ export interface CommandRegistry {
execute: (interaction: ChatInputCommandInteraction<CacheType>) => 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<string, CommandRegistry>();
const guildCommands: RESTPostAPIChatInputApplicationCommandsJSONBody[] = [];
+1 -1
View File
@@ -11,7 +11,7 @@ const DEFAULT_THRESHOLD = 7;
export default {
inProduction: true,
inDevelopment: false,
inDevelopment: true,
inPublic: true,
command: new SlashCommandBuilder()
+5
View File
@@ -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)),
);