2023-06-06 16:50:33 +00:00
|
|
|
import { EmbedBuilder, TextChannel } from 'discord.js';
|
2023-04-26 23:42:15 +00:00
|
|
|
import discord from '../../discord.json' assert { type: 'json' };
|
2023-06-26 15:05:05 +00:00
|
|
|
import { client } from '../bot.js';
|
2023-04-26 23:42:15 +00:00
|
|
|
import { NEGATIVE_COLOR } from '../constants/colors.js';
|
2023-06-27 01:43:30 +00:00
|
|
|
import isDev from '../core/node/isDev.js';
|
2023-04-26 23:42:15 +00:00
|
|
|
|
2023-04-27 01:00:56 +00:00
|
|
|
const PROCESS_ERROR_EVENTS = ['uncaughtException'];
|
|
|
|
|
const CLIENT_ERROR_EVENTS = ['error'];
|
2023-04-26 23:42:15 +00:00
|
|
|
|
2023-06-06 16:50:33 +00:00
|
|
|
export function handleError(error: Error, command: string) {
|
2023-04-26 23:42:15 +00:00
|
|
|
console.error(error);
|
|
|
|
|
|
|
|
|
|
(
|
|
|
|
|
client.guilds.cache
|
2023-05-19 23:36:02 +00:00
|
|
|
.get(discord.tres_guild_id)
|
|
|
|
|
?.channels.cache.get(discord.tres_log_channel) as TextChannel
|
2023-04-26 23:42:15 +00:00
|
|
|
).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-06-06 16:50:33 +00:00
|
|
|
process.on(name, (error) => handleError(error, 'process error')),
|
2023-04-27 00:21:39 +00:00
|
|
|
);
|
|
|
|
|
CLIENT_ERROR_EVENTS.forEach((name) =>
|
2023-06-06 16:50:33 +00:00
|
|
|
client.on(name, (error) => handleError(error, 'client error')),
|
2023-04-27 00:21:39 +00:00
|
|
|
);
|
|
|
|
|
}
|
2023-04-26 23:42:15 +00:00
|
|
|
}
|