Aethel Bot OSS repository!
aethel.xyz
bot
fun
ai
discord
discord-bot
aethel
1import { CommandInteraction, ButtonInteraction, MessageFlags } from 'discord.js';
2import BotClient from '@/services/Client';
3import logger from './logger';
4
5export interface ErrorHandlerOptions {
6 interaction: CommandInteraction | ButtonInteraction;
7 client: BotClient;
8 error: Error;
9 commandName: string;
10 userId?: string;
11 username?: string;
12 customMessage?: string;
13}
14
15export async function handleCommandError(options: ErrorHandlerOptions): Promise<void> {
16 const { interaction, client, error, commandName, userId, username, customMessage } = options;
17
18 const userInfo = userId && username ? `${username} (${userId})` : interaction.user.tag;
19 logger.error(`Error in ${commandName} command for user ${userInfo}: ${error.message}`);
20
21 const errorMessage = customMessage || (await client.getLocaleText('error', interaction.locale));
22
23 try {
24 if (interaction.deferred || interaction.replied) {
25 await interaction.editReply({
26 content: errorMessage,
27 });
28 } else {
29 await interaction.reply({
30 content: errorMessage,
31 flags: MessageFlags.Ephemeral,
32 });
33 }
34 } catch (replyError) {
35 logger.error(`Failed to send error message for ${commandName} command: ${replyError}`);
36 }
37}
38
39export function createErrorHandler(commandName: string) {
40 return async (options: Omit<ErrorHandlerOptions, 'commandName'>) => {
41 return handleCommandError({ ...options, commandName });
42 };
43}