From f1105c57e6a6d1ed42e7a17eb0dbb6c333b0458f Mon Sep 17 00:00:00 2001 From: THEMPGUYActions Date: Mon, 19 Oct 2026 19:20:24 -0400 Subject: [PATCH 1/3] Allow reviews to survive deleted tickets --- src/handlers/interactions.js | 62 ++++++++++++++++++++++++++++-------- 1 file changed, 49 insertions(+), 13 deletions(-) diff --git a/src/handlers/interactions.js b/src/handlers/interactions.js index 65d5ced..07b8dbc 100644 --- a/src/handlers/interactions.js +++ b/src/handlers/interactions.js @@ -376,22 +376,46 @@ async function handleComponent(interaction) { } if (interaction.isButton() && id.startsWith('review:rate:')) { - const [, , ticketIdRaw, ratingRaw, requestedOwnerId] = id.split(':'); + const [, , ticketIdRaw, ratingRaw, requestedOwnerIdRaw, requestedGuildIdRaw] = id.split(':'); const ticketId = Number(ticketIdRaw); const rating = Number(ratingRaw); if (!config.features?.reviews) throw new Error('Reviews are disabled.'); if (!Number.isInteger(ticketId) || ticketId < 1 || !Number.isInteger(rating) || rating < 1 || rating > 5) { throw new Error('This review button is invalid. Please use the latest review message from the bot.'); } - if (requestedOwnerId && requestedOwnerId !== user.id) { + + const ticket = db.getTicketById(ticketId); + let reviewOwnerId = requestedOwnerIdRaw; + let reviewGuildId = requestedGuildIdRaw; + + // New review buttons bind the request to the owner and guild, so ratings still work + // if staff deletes the ticket channel/record after closing it. + if (reviewOwnerId && reviewOwnerId !== user.id) { throw new Error('These review buttons were sent to a different account. Open the review DM sent to the ticket owner.'); } - const ticket = db.getTicketById(ticketId); - if (!ticket) throw new Error('This ticket record is no longer available, so this review link has expired.'); - if (ticket.owner_id !== user.id) { - throw new Error('This review link is out of date and no longer matches your ticket. Please ask support for a fresh review link.'); + if (ticket) { + if (ticket.owner_id !== user.id) { + throw new Error('This review link is out of date and no longer matches your ticket. Please use the review DM sent for your ticket.'); + } + if (reviewGuildId && reviewGuildId !== ticket.guild_id) { + throw new Error('This review link no longer matches the ticket. Please ask support for a fresh review link.'); + } + reviewOwnerId = ticket.owner_id; + reviewGuildId = ticket.guild_id; + } else if (!reviewOwnerId || !reviewGuildId) { + // Legacy DMs did not encode owner/guild. They can still be used when the bot is + // installed in exactly one server; otherwise the correct server is ambiguous. + if (client.guilds.cache.size !== 1) { + throw new Error('This older review link has expired because its ticket was removed. Please ask support for a fresh review link.'); + } + reviewOwnerId = user.id; + reviewGuildId = client.guilds.cache.first().id; } - const modal = new ModalBuilder().setCustomId(`review:submit:${ticketId}:${rating}:${user.id}`).setTitle(`Rate ticket #${ticketId}`) + if (!reviewOwnerId || !reviewGuildId) throw new Error('This review link is incomplete. Please ask support for a fresh review link.'); + + const modal = new ModalBuilder() + .setCustomId(`review:submit:${ticketId}:${rating}:${reviewOwnerId}:${reviewGuildId}`) + .setTitle(`Rate ticket #${ticketId}`) .addComponents(new ActionRowBuilder().addComponents(new TextInputBuilder().setCustomId('comment').setLabel('Optional feedback').setStyle(TextInputStyle.Paragraph).setRequired(false).setMaxLength(900))); return interaction.showModal(modal); } @@ -451,16 +475,28 @@ async function handleModal(interaction) { } if (id.startsWith('review:submit:')) { - const [, , ticketIdRaw, ratingRaw, requestedOwnerId] = id.split(':'); + const [, , ticketIdRaw, ratingRaw, requestedOwnerIdRaw, requestedGuildIdRaw] = id.split(':'); const ticketId = Number(ticketIdRaw); const rating = Number(ratingRaw); - if (requestedOwnerId && requestedOwnerId !== user.id) throw new Error('This review form was issued to a different account.'); + if (!Number.isInteger(ticketId) || ticketId < 1 || !Number.isInteger(rating) || rating < 1 || rating > 5) { + throw new Error('This review form is invalid. Please reopen the review message and try again.'); + } + if (requestedOwnerIdRaw && requestedOwnerIdRaw !== user.id) throw new Error('This review form was issued to a different account.'); + const ticket = db.getTicketById(ticketId); - if (!ticket) throw new Error('This ticket record is no longer available, so the review could not be saved.'); - if (ticket.owner_id !== user.id) throw new Error('Only the ticket owner can submit this review.'); - if (!Number.isInteger(rating) || rating < 1 || rating > 5) throw new Error('Invalid rating.'); + let reviewGuildId = requestedGuildIdRaw; + if (ticket) { + if (ticket.owner_id !== user.id) throw new Error('Only the ticket owner can submit this review.'); + if (reviewGuildId && reviewGuildId !== ticket.guild_id) throw new Error('This review form no longer matches the ticket.'); + reviewGuildId = ticket.guild_id; + } else if (!reviewGuildId && client.guilds.cache.size === 1) { + // Backward compatibility for an already-open legacy modal after its ticket was deleted. + reviewGuildId = client.guilds.cache.first().id; + } + if (!reviewGuildId) throw new Error('The ticket was removed and this older review form no longer identifies its server. Please ask support for a fresh review link.'); + const comment = interaction.fields.getTextInputValue('comment') || ''; - db.saveReview({ ticketId, guildId: ticket.guild_id, userId: user.id, rating, comment: truncate(comment, 900) }); + db.saveReview({ ticketId, guildId: reviewGuildId, userId: user.id, rating, comment: truncate(comment, 900) }); return interaction.reply({ content: `Thanks for rating ticket #${ticketId}: ${'⭐'.repeat(rating)} (${rating}/5). Your feedback has been saved.`, ...ephemeral }); } } From 3ee12cb136886a2b46f822a9b24479041919c51c Mon Sep 17 00:00:00 2001 From: THEMPGUYActions Date: Mon, 19 Oct 2026 19:20:26 -0400 Subject: [PATCH 2/3] Bind review buttons to ticket owner and guild --- src/services/tickets.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/services/tickets.js b/src/services/tickets.js index da99476..4ce3263 100644 --- a/src/services/tickets.js +++ b/src/services/tickets.js @@ -280,7 +280,7 @@ export async function closeTicket({ ticket, channel, member, reason = 'No reason if (config.features?.reviews) { components.push(new ActionRowBuilder().addComponents(...[1,2,3,4,5].map((rating) => new ButtonBuilder() - .setCustomId(`review:rate:${ticket.id}:${rating}:${ticket.owner_id}`) + .setCustomId(`review:rate:${ticket.id}:${rating}:${ticket.owner_id}:${ticket.guild_id}`) .setLabel(`${rating} star${rating === 1 ? '' : 's'}`) .setEmoji('⭐') .setStyle(rating >= 4 ? ButtonStyle.Success : rating <= 2 ? ButtonStyle.Danger : ButtonStyle.Secondary)))); From 51ab338ff52a1509b715ee096528b763a66bcdf7 Mon Sep 17 00:00:00 2001 From: THEMPGUYActions Date: Mon, 19 Oct 2026 19:20:28 -0400 Subject: [PATCH 3/3] Preserve review records after ticket deletion --- src/db/database.js | 22 +++++++++++++++++++++- 1 file changed, 21 insertions(+), 1 deletion(-) diff --git a/src/db/database.js b/src/db/database.js index 9c4cf93..0e32b64 100644 --- a/src/db/database.js +++ b/src/db/database.js @@ -101,7 +101,7 @@ export class Database { CREATE TABLE IF NOT EXISTS reviews ( id INTEGER PRIMARY KEY AUTOINCREMENT, - ticket_id INTEGER NOT NULL UNIQUE REFERENCES tickets(id) ON DELETE CASCADE, + ticket_id INTEGER NOT NULL UNIQUE, guild_id TEXT NOT NULL, user_id TEXT NOT NULL, rating INTEGER NOT NULL CHECK(rating BETWEEN 1 AND 5), @@ -133,6 +133,26 @@ export class Database { paid_at INTEGER ); `); + // Keep review history independent from ticket-channel deletion. Older versions + // cascaded reviews away whenever a ticket record was deleted. + const reviewForeignKeys = this.sqlite.pragma('foreign_key_list(reviews)'); + if (reviewForeignKeys.length) { + this.sqlite.exec(` + ALTER TABLE reviews RENAME TO reviews_legacy; + CREATE TABLE reviews ( + id INTEGER PRIMARY KEY AUTOINCREMENT, + ticket_id INTEGER NOT NULL UNIQUE, + guild_id TEXT NOT NULL, + user_id TEXT NOT NULL, + rating INTEGER NOT NULL CHECK(rating BETWEEN 1 AND 5), + comment TEXT NOT NULL DEFAULT '', + created_at INTEGER NOT NULL + ); + INSERT INTO reviews (id, ticket_id, guild_id, user_id, rating, comment, created_at) + SELECT id, ticket_id, guild_id, user_id, rating, comment, created_at FROM reviews_legacy; + DROP TABLE reviews_legacy; + `); + } const ticketColumns = new Set(this.sqlite.pragma('table_info(tickets)').map((column) => column.name)); if (!ticketColumns.has('inactivity_alerted_at')) { this.sqlite.exec('ALTER TABLE tickets ADD COLUMN inactivity_alerted_at INTEGER');