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:
@@ -0,0 +1,3 @@
|
||||
-- AlterTable
|
||||
ALTER TABLE "TicketConfig" ADD COLUMN "panelChannelId" TEXT;
|
||||
ALTER TABLE "TicketConfig" ADD COLUMN "panelMessageId" TEXT;
|
||||
@@ -513,6 +513,8 @@ model TicketConfig {
|
||||
mode String @default("CHANNEL")
|
||||
categoryId String?
|
||||
logChannelId String?
|
||||
panelChannelId String?
|
||||
panelMessageId String?
|
||||
transcriptDm Boolean @default(false)
|
||||
inactivityHours Int @default(72)
|
||||
ratingEnabled Boolean @default(true)
|
||||
|
||||
@@ -29,7 +29,7 @@ import {
|
||||
runGiveawayDeleteJob,
|
||||
GiveawayError
|
||||
} from './modules/giveaways/index.js';
|
||||
import { closeInactiveTickets } from './modules/tickets/index.js';
|
||||
import { closeInactiveTickets, runTicketPanelSyncJob } from './modules/tickets/index.js';
|
||||
import { expireBirthdayRole, runBirthdayCheck } from './modules/birthdays/index.js';
|
||||
import { ensureStatsRecurringJobs, startStatsWorker } from './modules/stats/index.js';
|
||||
import { pollAllFeeds } from './modules/feeds/index.js';
|
||||
@@ -348,6 +348,9 @@ export function startWorkers(context: BotContext): Worker[] {
|
||||
const ticketWorker = new Worker(
|
||||
ticketQueueName,
|
||||
async (job) => {
|
||||
if (job.name === 'ticketPanelSync') {
|
||||
return runTicketPanelSyncJob(context, job.data as { guildId: string });
|
||||
}
|
||||
if (job.name === 'selfRoleExpire') {
|
||||
const data = job.data as SelfRoleExpireJob;
|
||||
await expireSelfRole(context, data.guildId, data.userId, data.roleId);
|
||||
|
||||
@@ -7,8 +7,6 @@ import { ticketCommandData } from './command-definitions.js';
|
||||
import {
|
||||
addUserToTicket,
|
||||
assertTicketStaff,
|
||||
buildPanelComponents,
|
||||
buildPanelEmbed,
|
||||
claimTicket,
|
||||
closeTicket,
|
||||
createCategoryWithRole,
|
||||
@@ -17,7 +15,9 @@ import {
|
||||
removeUserFromTicket,
|
||||
renameTicket,
|
||||
setTicketPriority,
|
||||
syncTicketPanel,
|
||||
TicketError,
|
||||
TicketPanelError,
|
||||
upsertCategoryByName
|
||||
} from './service.js';
|
||||
|
||||
@@ -67,10 +67,26 @@ const ticketCommand: SlashCommand = {
|
||||
return;
|
||||
}
|
||||
|
||||
const embed = buildPanelEmbed(locale, name, description);
|
||||
const components = buildPanelComponents(categories);
|
||||
await channel.send({ embeds: [embed], components });
|
||||
await interaction.reply({ content: t(locale, 'ticket.panel.created'), ephemeral: true });
|
||||
try {
|
||||
await syncTicketPanel(context, interaction.guildId!, {
|
||||
channelId: channel.id,
|
||||
title: name,
|
||||
description
|
||||
});
|
||||
await interaction.reply({ content: t(locale, 'ticket.panel.created'), ephemeral: true });
|
||||
} catch (error) {
|
||||
if (error instanceof TicketPanelError) {
|
||||
const key =
|
||||
error.code === 'no_categories'
|
||||
? 'ticket.error.no_categories'
|
||||
: error.code === 'not_configured'
|
||||
? 'ticket.error.invalid_channel'
|
||||
: 'ticket.error.missing_permission';
|
||||
await interaction.reply({ content: t(locale, key), ephemeral: true });
|
||||
return;
|
||||
}
|
||||
throw error;
|
||||
}
|
||||
return;
|
||||
}
|
||||
|
||||
|
||||
@@ -1,4 +1,9 @@
|
||||
export { ticketsCommands } from './commands.js';
|
||||
export { isTicketInteraction, handleTicketInteraction } from './interactions.js';
|
||||
export { closeInactiveTickets, checkInactiveTickets } from './service.js';
|
||||
export {
|
||||
closeInactiveTickets,
|
||||
checkInactiveTickets,
|
||||
runTicketPanelSyncJob,
|
||||
TicketPanelError
|
||||
} from './service.js';
|
||||
export { registerTicketEvents } from './events.js';
|
||||
|
||||
@@ -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()
|
||||
|
||||
Reference in New Issue
Block a user