feat(commands): dispatch slash commands with access control

Refs: #30
This commit is contained in:
nocode committed 2026-09-13 12:25:07 +02:00
1 parent 8f48efdcdc
commit 017764d5b8
1 file changed
+47
+47
View File
@@ -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(() => {});
}
}