From 017764d5b8bc7003b668f9e014db8ad9a5857df7 Mon Sep 17 00:00:00 2001 From: nocode Date: Sun, 13 Sep 2026 12:19:11 +0200 Subject: [PATCH] feat(commands): dispatch slash commands with access control Refs: #30 --- src/events/interaction-create.js | 47 ++++++++++++++++++++++++++++++++ 1 file changed, 47 insertions(+) create mode 100644 src/events/interaction-create.js diff --git a/src/events/interaction-create.js b/src/events/interaction-create.js new file mode 100644 index 0000000..70075b0 --- /dev/null +++ b/src/events/interaction-create.js @@ -0,0 +1,47 @@ +import { Events, MessageFlags } from 'discord.js'; +import { PermissionDeniedError, UserFacingError } from '../core/errors.js'; +import { createLogger } from '../core/logger.js'; +import { hasAccess } from '../core/permissions.js'; +import { errorEmbed } from '../ui/embeds.js'; + +const log = createLogger('interactions'); + +export const name = Events.InteractionCreate; + +async function replyError(interaction, message) { + const payload = { embeds: [errorEmbed(message)], flags: MessageFlags.Ephemeral }; + if (interaction.deferred || interaction.replied) { + await interaction.followUp(payload); + } else { + await interaction.reply(payload); + } +} + +export async function execute(client, interaction) { + if (!interaction.isChatInputCommand() && !interaction.isAutocomplete()) return; + + const command = client.commands.get(interaction.commandName); + if (!command) return; + + if (interaction.isAutocomplete()) { + await command.autocomplete?.(interaction); + return; + } + + try { + const subcommand = interaction.options.getSubcommand(false); + const requiredLevel = command.subcommandAccess?.[subcommand] ?? command.access; + if (!hasAccess(interaction.member, requiredLevel)) { + throw new PermissionDeniedError(); + } + await command.execute(interaction); + } catch (error) { + if (error instanceof UserFacingError) { + await replyError(interaction, error.message); + return; + } + + log.error(`Command failed: /${interaction.commandName}`, error); + await replyError(interaction, 'Une erreur inattendue est survenue. Réessaie plus tard.').catch(() => {}); + } +}