Implement ticket panel synchronization and configuration updates

- Added `panelChannelId` and `panelMessageId` fields to the `TicketConfig` model for managing ticket panel settings.
- Introduced `syncTicketPanel` function to handle posting and updating the ticket panel in Discord channels via BullMQ.
- Enhanced error handling for ticket panel synchronization, including specific error messages for missing categories and posting failures.
- Updated WebUI components to support panel channel selection and improved error messaging for synchronization issues.
- Added localization entries for new error messages related to ticket panel synchronization in both English and German.
- Refactored ticket category management to trigger panel synchronization upon category creation, update, or deletion.
This commit is contained in:
TheOnlyMace
2026-07-25 16:06:17 +02:00
parent dba7bb3cdc
commit 1eec10ba80
19 changed files with 476 additions and 47 deletions

View File

@@ -10,8 +10,11 @@ import {
StringSelectMenuBuilder,
TextInputBuilder,
TextInputStyle,
type APIMessageComponentEmoji,
type Guild,
type GuildBasedChannel,
type GuildMember,
type GuildTextBasedChannel,
type Message,
type SendableChannels,
type TextChannel,
@@ -51,6 +54,52 @@ export class TicketError extends Error {
}
}
export class TicketPanelError extends Error {
constructor(public readonly code: 'not_configured' | 'missing_permission' | 'no_categories') {
super(code);
this.name = 'TicketPanelError';
}
}
function botCanPostPanel(
channel: GuildBasedChannel,
meId: string
): channel is GuildTextBasedChannel {
if (!channel.isTextBased() || channel.isDMBased()) {
return false;
}
const permissions = channel.permissionsFor(meId);
if (!permissions) {
return false;
}
return (
permissions.has(PermissionFlagsBits.ViewChannel) &&
permissions.has(PermissionFlagsBits.SendMessages) &&
permissions.has(PermissionFlagsBits.EmbedLinks)
);
}
function parseButtonEmoji(emoji?: string | null): APIMessageComponentEmoji | undefined {
if (!emoji) {
return undefined;
}
const customMatch = emoji.match(/^<(a)?:(\w+):(\d+)>$/);
if (customMatch) {
return {
id: customMatch[3]!,
name: customMatch[2],
animated: Boolean(customMatch[1])
};
}
if (/^\d+$/.test(emoji)) {
return { id: emoji };
}
return { name: emoji };
}
async function fetchSendableChannel(
context: BotContext,
channelId: string
@@ -185,8 +234,9 @@ export function buildPanelComponents(categories: TicketCategory[]): ActionRowBui
.setCustomId(openButtonId(category.id))
.setLabel(category.name.slice(0, 80))
.setStyle(ButtonStyle.Primary);
if (category.emoji) {
button.setEmoji(category.emoji);
const emoji = parseButtonEmoji(category.emoji);
if (emoji) {
button.setEmoji(emoji);
}
row.addComponents(button);
}
@@ -197,17 +247,120 @@ export function buildPanelComponents(categories: TicketCategory[]): ActionRowBui
.setCustomId(TICKET_SELECT_ID)
.setPlaceholder('Select a category')
.addOptions(
categories.slice(0, 25).map((category) => ({
label: category.name.slice(0, 100),
value: category.id,
description: category.description?.slice(0, 100) ?? undefined,
emoji: category.emoji ? { name: category.emoji } : undefined
}))
categories.slice(0, 25).map((category) => {
const emoji = parseButtonEmoji(category.emoji);
return {
label: category.name.slice(0, 100),
value: category.id,
description: category.description?.slice(0, 100) ?? undefined,
emoji
};
})
);
return [new ActionRowBuilder<StringSelectMenuBuilder>().addComponents(select)];
}
/**
* Posts or updates the ticket panel. Used by `/ticket panel_create` and by the
* dashboard via BullMQ (`ticketPanelSync`).
*/
export async function syncTicketPanel(
context: BotContext,
guildId: string,
options: {
channelId?: string | null;
title?: string;
description?: string | null;
} = {}
): Promise<{ panelChannelId: string; panelMessageId: string }> {
const locale = await getGuildLocale(context.prisma, guildId);
const config = await getTicketConfig(context.prisma, guildId);
const categories = await getGuildCategories(context, guildId);
if (categories.length === 0) {
throw new TicketPanelError('no_categories');
}
const targetChannelId = options.channelId || config.panelChannelId;
if (!targetChannelId) {
throw new TicketPanelError('not_configured');
}
const guild = await context.client.guilds.fetch(guildId);
const me = guild.members.me;
if (!me) {
throw new TicketPanelError('missing_permission');
}
const channel = await guild.channels.fetch(targetChannelId).catch(() => null);
if (!channel || !botCanPostPanel(channel, me.id)) {
throw new TicketPanelError('missing_permission');
}
const title = options.title ?? t(locale, 'ticket.panel.defaultTitle');
const description =
options.description !== undefined
? options.description
: t(locale, 'ticket.panel.defaultDescription');
const payload = {
embeds: [buildPanelEmbed(locale, title, description)],
components: buildPanelComponents(categories)
};
const sameChannel =
Boolean(config.panelMessageId) && config.panelChannelId === channel.id;
if (sameChannel && config.panelMessageId) {
try {
const existing = await channel.messages.fetch(config.panelMessageId);
await existing.edit(payload);
if (!config.panelChannelId) {
await context.prisma.ticketConfig.update({
where: { guildId },
data: { panelChannelId: channel.id }
});
}
return { panelChannelId: channel.id, panelMessageId: existing.id };
} catch (error) {
logger.warn(
{ error, guildId, messageId: config.panelMessageId },
'Failed to edit ticket panel; recreating'
);
}
}
if (config.panelMessageId && config.panelChannelId && config.panelChannelId !== channel.id) {
try {
const oldChannel = await guild.channels.fetch(config.panelChannelId);
if (oldChannel?.isTextBased() && !oldChannel.isDMBased()) {
const oldMessage = await oldChannel.messages.fetch(config.panelMessageId);
await oldMessage.delete();
}
} catch {
// Previous panel may already be gone
}
}
const message = await channel.send(payload);
await context.prisma.ticketConfig.update({
where: { guildId },
data: {
panelChannelId: channel.id,
panelMessageId: message.id
}
});
return { panelChannelId: channel.id, panelMessageId: message.id };
}
export async function runTicketPanelSyncJob(
context: BotContext,
data: { guildId: string }
): Promise<{ panelChannelId: string; panelMessageId: string }> {
return syncTicketPanel(context, data.guildId);
}
export function buildOpenModal(category: TicketCategory, locale: 'de' | 'en'): ModalBuilder {
const fields = parseFormFields(category.formFields);
const modal = new ModalBuilder()