Files
FWS-DiscordBot/src/events/interaction-create.js
T
BlackAngelTV ef8f09da10 refactor(ui): give every message one friendly, ungendered voice
Members read all replies as one voice, so the wording now lives in one place:
the shared titles, the list marker and the "what to do next" hint come from
src/ui/embeds.js, and every module follows them.

An error explains the cause, recalls the rule and offers the way out instead of
blaming; a success says what happens next. Nothing agrees on the gender of a
person ("utilisateur", "cher/chère", "ravie"), which also keeps the sentences
shorter. Two tests lock the shared titles and reject gendered descriptions.
2026-09-15 16:12:19 +02:00

89 lines
3.3 KiB
JavaScript
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
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(() => {});
}
}