From a0ffbae36d4775afa8e3ec1133523bcc2f531e7d Mon Sep 17 00:00:00 2001 From: Isaac Date: Thu, 9 Feb 2023 21:51:28 +0000 Subject: [PATCH] feat: make closed ticket DM more useful --- src/i18n/en-GB.yml | 13 +++++- src/lib/tickets/manager.js | 86 +++++++++++++++++++++++++++++++++++--- 2 files changed, 92 insertions(+), 7 deletions(-) diff --git a/src/i18n/en-GB.yml b/src/i18n/en-GB.yml index 341f1f1..cf7df34 100644 --- a/src/i18n/en-GB.yml +++ b/src/i18n/en-GB.yml @@ -231,7 +231,18 @@ commands: name: Create a ticket for user dm: closed: - archived: Type `/transcript` in **{guild}** to view the archived messages. + archived: Use the `/transcript` command in **{guild}** to view the archived messages. + fields: + closed: + name: Closed at + value: "{timestamp} (after {duration})" + closed_by: Closed by + created: Created at + feedback: Your feedback + reason: Closed because + response: Response time + ticket: Ticket + topic: Topic title: Your ticket has been closed confirm_open: title: Do you want to open a ticket with the following topic? diff --git a/src/lib/tickets/manager.js b/src/lib/tickets/manager.js index c87f58a..6b2a74b 100644 --- a/src/lib/tickets/manager.js +++ b/src/lib/tickets/manager.js @@ -19,7 +19,10 @@ const { logTicketEvent } = require('../logging'); const { isStaff } = require('../users'); const { Collection } = require('discord.js'); const Cryptr = require('cryptr'); -const { encrypt } = new Cryptr(process.env.ENCRYPTION_KEY); +const { + decrypt, + encrypt, +} = new Cryptr(process.env.ENCRYPTION_KEY); /** * @typedef {import('@prisma/client').Category & @@ -1047,7 +1050,8 @@ module.exports = class TicketManager { closedBy = null, reason = null, }) { - const ticket = await this.getTicket(ticketId); + let ticket = await this.getTicket(ticketId); + const getMessage = this.client.i18n.getLocale(ticket.guild.locale); this.$count.categories[ticket.categoryId].total -= 1; this.$count.categories[ticket.categoryId][ticket.createdById] -= 1; @@ -1077,8 +1081,13 @@ module.exports = class TicketManager { data.pinnedMessageIds = [...pinned.keys()]; } - await this.client.prisma.ticket.update({ + ticket = await this.client.prisma.ticket.update({ data, + include: { + category: true, + feedback: true, + guild: true, + }, where: { id: ticket.id }, }); @@ -1092,7 +1101,7 @@ module.exports = class TicketManager { action: 'close', target: { id: ticket.id, - name: `[${ticket.number}]`, + name: `${ticket.category.name} **#${ticket.number}**`, }, userId: closedBy, }); @@ -1101,14 +1110,79 @@ module.exports = class TicketManager { try { const creator = await channel?.guild.members.fetch(ticket.createdById); if (creator) { - const getMessage = this.client.i18n.getLocale(ticket.guild.locale); const embed = new ExtendedEmbedBuilder({ iconURL: channel.guild.iconURL(), text: ticket.guild.footer, }) .setColor(ticket.guild.primaryColour) - .setTitle(getMessage('dm.closed.title')); + .setTitle(getMessage('dm.closed.title')) + .addFields([ + { + inline: true, + name: getMessage('dm.closed.fields.ticket'), + value: `${ticket.category.name} **#${ticket.number}**`, + }, + + ]); + if (ticket.topic) { + embed.addFields({ + inline: true, + name: getMessage('dm.closed.fields.topic'), + value: decrypt(ticket.topic), + }); + } + + embed.addFields([ + { + inline: true, + name: getMessage('dm.closed.fields.created'), + value: ``, + }, + { + inline: true, + name: getMessage('dm.closed.fields.closed.name'), + value: getMessage('dm.closed.fields.closed.value', { + duration: ms(ticket.closedAt - ticket.createdAt, { long: true }), + timestamp: ``, + }), + }, + ]); + + if (ticket.firstResponseAt) { + embed.addFields({ + inline: true, + name: getMessage('dm.closed.fields.response'), + value: ms(ticket.firstResponseAt - ticket.createdAt, { long: true }), + }); + } + + if (ticket.feedback) { + embed.addFields({ + inline: true, + name: getMessage('dm.closed.fields.feedback'), + value: Array(ticket.feedback.rating).fill('⭐').join(' ') + ` (${ticket.feedback.rating}/5)`, + }); + } + + if (ticket.closedById) { + embed.addFields({ + inline: true, + name: getMessage('dm.closed.fields.closed_by'), + value: `<@${ticket.closedById}>`, + }); + } + + + if (reason) { + embed.addFields({ + inline: true, + name: getMessage('dm.closed.fields.reason'), + value: reason, + }); + } + if (ticket.guild.archive) embed.setDescription(getMessage('dm.closed.archived', { guild: channel.guild.name })); + await creator.send({ embeds: [embed] }); } } catch (error) {