feat: better error handling

This commit is contained in:
TresAbhi committed 2023-04-26 23:42:15 +00:00
1 parent c494331470
commit 48241bd0d4
12 files changed
+347 -106

No files matched your search

+36
View File
@@ -0,0 +1,36 @@
import { Client, EmbedBuilder, TextChannel } from 'discord.js';
import discord from '../../discord.json' assert { type: 'json' };
import { NEGATIVE_COLOR } from '../constants/colors.js';
export const PROCESS_ERROR_EVENTS = ['uncaughtException'];
export const CLIENT_ERROR_EVENTS = ['error'];
function handleError(error: Error, client: Client) {
console.error(error);
(
client.guilds.cache
.get(discord.guild_id)
?.channels.cache.get(discord.log_channel) as TextChannel
).send({
embeds: [
new EmbedBuilder()
.setTitle('Skilled Bot ran into a catastrophic error')
.setColor(NEGATIVE_COLOR)
.setDescription(
`Total shutdown was avoided! Error:\n\n\`\`\`${error.name}\n${error.message}\n${error.stack}\n${error.cause}\`\`\``,
),
],
});
}
export function registerErrorHandlers(client: Client) {
PROCESS_ERROR_EVENTS.forEach((name) =>
process.on(name, (error) => handleError(error, client)),
);
CLIENT_ERROR_EVENTS.forEach((name) =>
client.on(name, (error) => handleError(error, client)),
);
return client;
}