Files
blitzkit/src/events/error.ts
T

42 lines
1.3 KiB
TypeScript
Raw Normal View History

2023-04-26 23:42:15 +00:00
import { Client, EmbedBuilder, TextChannel } from 'discord.js';
import discord from '../../discord.json' assert { type: 'json' };
import { NEGATIVE_COLOR } from '../constants/colors.js';
2023-05-13 22:01:58 +00:00
import isDev from '../core/process/isDev.js';
2023-04-28 13:42:42 +00:00
import { client } from '../index.js';
2023-04-26 23:42:15 +00:00
const PROCESS_ERROR_EVENTS = ['uncaughtException'];
const CLIENT_ERROR_EVENTS = ['error'];
2023-04-26 23:42:15 +00:00
2023-05-06 18:03:37 +00:00
export function handleError(error: Error, client: Client, command: string) {
2023-04-26 23:42:15 +00:00
console.error(error);
(
client.guilds.cache
.get(discord.guild_id)
?.channels.cache.get(discord.log_channel) as TextChannel
).send({
embeds: [
new EmbedBuilder()
2023-05-02 23:01:54 +00:00
.setTitle(`${client.user?.username} ran into an error`)
2023-04-26 23:42:15 +00:00
.setColor(NEGATIVE_COLOR)
.setDescription(
2023-05-06 17:32:28 +00:00
`\`\`\`${[command, error.name, error.message, error.stack]
.filter((item) => item !== undefined)
.join('\n\n')}\`\`\``,
2023-04-26 23:42:15 +00:00
),
],
});
}
2023-04-28 13:42:42 +00:00
export function registerErrorHandlers() {
2023-04-27 00:21:39 +00:00
if (!isDev()) {
PROCESS_ERROR_EVENTS.forEach((name) =>
2023-05-06 18:03:37 +00:00
process.on(name, (error) => handleError(error, client, 'process error')),
2023-04-27 00:21:39 +00:00
);
CLIENT_ERROR_EVENTS.forEach((name) =>
2023-05-06 18:03:37 +00:00
client.on(name, (error) => handleError(error, client, 'client error')),
2023-04-27 00:21:39 +00:00
);
}
2023-04-26 23:42:15 +00:00
return client;
}