import { Events, MessageFlags } from 'discord.js'; import { ACCESS_LABELS } from '../config/constants.js'; import { PermissionDeniedError, UserFacingError } from '../core/errors.js'; import { createLogger } from '../core/logger.js'; import { hasAccess } from '../core/permissions.js'; import { handleGiteaModal } from '../services/gitea/gitea-modals.js'; import { handlePollComponent } from '../services/polls/poll-service.js'; import { handleTicketComponent } from '../services/tickets/ticket-service.js'; import { handleVoiceComponent, handleVoiceModal } from '../services/voice/voice-service.js'; import { errorEmbed } from '../ui/embeds.js'; const log = createLogger('interactions'); export const name = Events.InteractionCreate; /** Custom id prefix -> handlers for the message components and the modals. */ const routes = [ { prefix: 'ticket:', component: handleTicketComponent }, { prefix: 'poll:', component: handlePollComponent }, { prefix: 'voice:', component: handleVoiceComponent, modal: handleVoiceModal }, { prefix: 'issue:', modal: handleGiteaModal }, { prefix: 'pr:', modal: handleGiteaModal }, ]; 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); } } async function runChatInputCommand(interaction) { const command = interaction.client.commands.get(interaction.commandName); if (!command) return; const subcommand = interaction.options.getSubcommand(false); const requiredLevel = command.subcommandAccess?.[subcommand] ?? command.access; if (!hasAccess(interaction.member, requiredLevel)) { throw new PermissionDeniedError( `Cette commande demande le niveau **${ACCESS_LABELS[requiredLevel]}**. Si tu en as besoin, demande à une personne du staff : elle peut le faire pour toi.`, ); } await command.execute(interaction); } async function runRoute(interaction, kind) { const route = routes.find( ({ prefix, [kind]: handler }) => handler && interaction.customId.startsWith(prefix), ); if (!route) { log.debug(`Ignored ${kind} ${interaction.customId}`); return; } await route[kind](interaction); } export async function execute(_client, interaction) { try { if (interaction.isAutocomplete()) { await interaction.client.commands.get(interaction.commandName)?.autocomplete?.(interaction); return; } if (interaction.isButton() || interaction.isAnySelectMenu()) { await runRoute(interaction, 'component'); return; } if (interaction.isModalSubmit()) { await runRoute(interaction, 'modal'); return; } if (!interaction.isChatInputCommand()) return; await runChatInputCommand(interaction); } catch (error) { if (error instanceof UserFacingError) { await replyError(interaction, error.message); return; } log.error(`Interaction failed: ${interaction.commandName ?? interaction.customId ?? 'unknown'}`, error); await replyError( interaction, 'Quelque chose s’est mal passé de mon côté, rien n’est perdu. Réessaie dans un instant — je l’ai noté dans mes logs, l’équipe pourra regarder si ça recommence.', ).catch(() => {}); } }