Files
FWS-DiscordBot/src/events/interaction-create.js
T
BlackAngelTV 6f3ec439d4 feat(gitea): recruit developers from a Discord panel into FWS
Recruited people need a Gitea account without a public signup on the forge.
The panel posts in its own Gitea category, the single modal keeps the whole
application in one gesture, and acceptance creates a restricted account in the
FWS organization, adds it to the Dev team, grants the Discord Dev role and
sends the temporary password by DM. Restrictions keep the new account working
inside the organization only, with no personal repository of its own.
2026-12-06 17:17:05 +01:00

91 lines
3.5 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 { handleTeamComponent, handleTeamModal } from '../services/gitea/team-recruitment.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 },
{ prefix: 'gitea-team:', component: handleTeamComponent, modal: handleTeamModal },
];
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(() => {});
}
}