feat(tickets): implement the ticket service, command and channel controls
Each ticket category is visible to a different set of staff roles, and the claim and close actions must also be reachable from the channel itself, so the buttons live in the ticket and share the service checks.
This commit is contained in:
3 files changed
+353
-18
No files matched your search
@@ -2,12 +2,23 @@ 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 { handleGiteaModal } from '../services/gitea/gitea-modals.js';
|
||||
import { handlePollComponent } from '../services/polls/poll-service.js';
|
||||
import { handleTicketComponent } from '../services/tickets/ticket-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: '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) {
|
||||
@@ -17,31 +28,53 @@ async function replyError(interaction, message) {
|
||||
}
|
||||
}
|
||||
|
||||
export async function execute(client, interaction) {
|
||||
if (!interaction.isChatInputCommand() && !interaction.isAutocomplete()) return;
|
||||
|
||||
const command = client.commands.get(interaction.commandName);
|
||||
async function runChatInputCommand(interaction) {
|
||||
const command = interaction.client.commands.get(interaction.commandName);
|
||||
if (!command) return;
|
||||
|
||||
if (interaction.isAutocomplete()) {
|
||||
await command.autocomplete?.(interaction);
|
||||
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);
|
||||
}
|
||||
|
||||
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 {
|
||||
const subcommand = interaction.options.getSubcommand(false);
|
||||
const requiredLevel = command.subcommandAccess?.[subcommand] ?? command.access;
|
||||
if (!hasAccess(interaction.member, requiredLevel)) {
|
||||
throw new PermissionDeniedError();
|
||||
if (interaction.isAutocomplete()) {
|
||||
await interaction.client.commands.get(interaction.commandName)?.autocomplete?.(interaction);
|
||||
return;
|
||||
}
|
||||
await command.execute(interaction);
|
||||
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(`Command failed: /${interaction.commandName}`, error);
|
||||
log.error(`Interaction failed: ${interaction.commandName ?? interaction.customId ?? 'unknown'}`, error);
|
||||
await replyError(interaction, 'Une erreur inattendue est survenue. Réessaie plus tard.').catch(() => {});
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user