|
| 1 | +import type { BotClient } from "@core/BotClient"; |
| 2 | +import { TicketRepository } from "@database/repositories"; |
| 3 | +import { |
| 4 | + ContainerBuilder, |
| 5 | + MessageFlags, |
| 6 | + SeparatorBuilder, |
| 7 | + TextDisplayBuilder, |
| 8 | + type ButtonInteraction, |
| 9 | + type TextChannel, |
| 10 | +} from "discord.js"; |
| 11 | +import { |
| 12 | + SUPPORT_REPORT_CHANNEL_ID, |
| 13 | + TICKET_CLOSED_COLOR, |
| 14 | +} from "../config/misc"; |
| 15 | +import { ticketCard } from "./openTicket"; |
| 16 | + |
| 17 | +export async function runCloseTicket( |
| 18 | + ticketId: string, |
| 19 | + interaction: ButtonInteraction, |
| 20 | +) { |
| 21 | + const ticket = await TicketRepository.close(ticketId, interaction.user.id); |
| 22 | + // First, fetch the ticket without mutating it |
| 23 | + const existingTicket = await TicketRepository.findById(ticketId); |
| 24 | + |
| 25 | + if (!existingTicket) { |
| 26 | + await interaction.reply({ |
| 27 | + content: `This ticket no longer exists.`, |
| 28 | + flags: [MessageFlags.Ephemeral], |
| 29 | + }); |
| 30 | + return; |
| 31 | + } |
| 32 | + |
| 33 | + // Validate that the interaction was used in the correct channel |
| 34 | + if (interaction.channelId !== existingTicket.channelId) { |
| 35 | + await interaction.reply({ |
| 36 | + content: `You can only close this ticket from its own channel.`, |
| 37 | + flags: [MessageFlags.Ephemeral], |
| 38 | + }); |
| 39 | + return; |
| 40 | + } |
| 41 | + |
| 42 | + // Acknowledge the interaction before mutating any state |
| 43 | + if (interaction.deferUpdate) { |
| 44 | + await interaction.deferUpdate(); |
| 45 | + } else { |
| 46 | + await interaction.reply({ |
| 47 | + content: `Your Ticket was closed !`, |
| 48 | + flags: [MessageFlags.Ephemeral], |
| 49 | + }); |
| 50 | + } |
| 51 | + |
| 52 | + // Now safely perform the close operation and subsequent side effects |
| 53 | + const ticket = await TicketRepository.close(ticketId, interaction.user.id); |
| 54 | + |
| 55 | + const closedAtStr = ticket.closedAt |
| 56 | + ? `${Math.floor(ticket.closedAt?.getTime() / 1000) ?? ""}` |
| 57 | + : null; |
| 58 | + const ticketCardString = ticketCard( |
| 59 | + ticket.userId, |
| 60 | + ticket.category, |
| 61 | + ticket.subject, |
| 62 | + ); |
| 63 | + const ticketClosedString = `${ |
| 64 | + ticket.closedAt |
| 65 | + ? `Closed at: <t:${closedAtStr}> (<t:${closedAtStr}:R>)` |
| 66 | + : `` |
| 67 | + } |
| 68 | +Closed by: <@${ticket.closedBy}>`; |
| 69 | + |
| 70 | + const reportChannel = (await interaction.guild!.channels.fetch( |
| 71 | + SUPPORT_REPORT_CHANNEL_ID, |
| 72 | + )) as TextChannel; |
| 73 | + |
| 74 | + const report = new ContainerBuilder() |
| 75 | + .setAccentColor(TICKET_CLOSED_COLOR) |
| 76 | + .addTextDisplayComponents( |
| 77 | + new TextDisplayBuilder().setContent(`### Support request was closed`), |
| 78 | + ) |
| 79 | + .addSeparatorComponents(new SeparatorBuilder()) |
| 80 | + .addTextDisplayComponents( |
| 81 | + new TextDisplayBuilder().setContent(ticketCardString), |
| 82 | + ) |
| 83 | + .addSeparatorComponents(new SeparatorBuilder()) |
| 84 | + .addTextDisplayComponents( |
| 85 | + new TextDisplayBuilder().setContent(ticketClosedString), |
| 86 | + ); |
| 87 | + |
| 88 | + await reportChannel.send({ |
| 89 | + components: [report], |
| 90 | + flags: [MessageFlags.IsComponentsV2, MessageFlags.SuppressNotifications], |
| 91 | + }); |
| 92 | +} |
| 93 | + |
| 94 | +export default { |
| 95 | + customId: /^ticket_close_/, |
| 96 | + |
| 97 | + async run(interaction: ButtonInteraction, client: BotClient) { |
| 98 | + const [_0, _1, ticketId] = interaction.customId.split("_"); |
| 99 | + |
| 100 | + await runCloseTicket(ticketId, interaction); |
| 101 | + }, |
| 102 | +}; |
0 commit comments