-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathinteractionCreate.js
More file actions
45 lines (37 loc) · 1.52 KB
/
interactionCreate.js
File metadata and controls
45 lines (37 loc) · 1.52 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
// events/interactionCreate.js
// Routes every incoming interaction to the appropriate slash command handler.
// Handles errors gracefully so one bad command cannot crash the bot.
import { Events, MessageFlags } from 'discord.js';
export const name = Events.InteractionCreate;
export const once = false;
/**
* @param {import('discord.js').Interaction} interaction
*/
export async function execute(interaction) {
// We only handle chat input (slash) commands here.
// Button/select-menu/modal interactions would need separate handlers.
if (!interaction.isChatInputCommand()) return;
const command = interaction.client.commands.get(interaction.commandName);
if (!command) {
console.warn(`[interactionCreate] Unknown command: ${interaction.commandName}`);
return interaction.reply({
content: `❓ Unknown command \`/${interaction.commandName}\`. It may have been removed.`,
flags: MessageFlags.Ephemeral,
});
}
try {
await command.execute(interaction);
} catch (error) {
console.error(`[interactionCreate] Error in /${interaction.commandName}:`, error);
const errorMsg = {
content: `💥 An unexpected error occurred while running that command.\n\`${error.message}\``,
flags: MessageFlags.Ephemeral,
};
// The interaction may already be deferred/replied — handle both cases
if (interaction.deferred || interaction.replied) {
await interaction.editReply(errorMsg).catch(() => null);
} else {
await interaction.reply(errorMsg).catch(() => null);
}
}
}