feat(commands): dispatch slash commands with access control
Refs: #30 Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
This commit is contained in:
1 file changed
+47
@@ -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(() => {});
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user