From 2a96359d8db03a980940ce4af86f90f4631542c5 Mon Sep 17 00:00:00 2001 From: Nix Krystik Date: Wed, 14 Oct 2026 11:26:43 +0800 Subject: [PATCH] (buttons): Implement `open-ticket-for-reported-message` button. --- .../open-ticket-for-reported-message.ts | 41 +++++++++++-------- 1 file changed, 23 insertions(+), 18 deletions(-) diff --git a/src/buttons/open-ticket-for-reported-message.ts b/src/buttons/open-ticket-for-reported-message.ts index 8a57b82..1a7437e 100644 --- a/src/buttons/open-ticket-for-reported-message.ts +++ b/src/buttons/open-ticket-for-reported-message.ts @@ -1,29 +1,30 @@ import { ButtonInteraction, Client, MessageFlags, MessageMentions } from 'discord.js'; +import { Button } from '../types'; import { createPrivateHelpTicketThread } from '../utils'; -export default { - name: 'open-ticket-for-reported-message', - handler: async function (client: Client, interaction: ButtonInteraction) { - const message = await interaction.message.fetch(); - const guild = await interaction.guild!.fetch(); +class OpenTicketForReportedMessageButton implements Button { + id = 'open-ticket-for-reported-message'; - const reportEmbed = message.embeds[0]!; + async execute(context: Client, interaction: ButtonInteraction, ...args: any[]): Promise { + if (!interaction.inGuild()) return; + await interaction.deferReply({ flags: MessageFlags.Ephemeral }); - const regex = new RegExp(MessageMentions.UsersPattern); + try { + const guild = await context.guilds.fetch(interaction.guildId); + const message = await interaction.message.fetch(); - const reportedMessageUrl = reportEmbed.fields[0].value; - const reporterId = regex.exec(reportEmbed.fields[2].value)!.groups!.id; - const additionalInfo = reportEmbed.fields[3]?.name == 'Additional Information' ? reportEmbed.fields[3].value : ''; + const reportEmbed = message.embeds[0]!; + const regex = new RegExp(MessageMentions.UsersPattern); - const reporter = await guild.members.fetch(reporterId) ?? await client.users.fetch(reporterId); + const reportedMessageUrl = reportEmbed.fields[0].value; + const reporterId = regex.exec(reportEmbed.fields[2].value)!.groups!.id; + const additionalInfo = reportEmbed.fields[3]?.name == 'Additional Information' ? reportEmbed.fields[3].value : ''; + const reporter = await guild.members.fetch(reporterId) ?? await context.users.fetch(reporterId); - const thread = await createPrivateHelpTicketThread(client, guild, null, `Ticket creted by staff in response to message report: ${message.url}. Reported message: ${reportedMessageUrl}. ${additionalInfo ? `\n\nAdditional information provided in report:\n${additionalInfo.split('\n').map(c => `> ${c}`).join('\n')}` : ''}`, `Ticket: Message Report From ${reporter.displayName}`, [reporterId, interaction.user.id]); + const thread = await createPrivateHelpTicketThread(context, guild, null, `Ticket creted by staff in response to message report: ${message.url}. Reported message: ${reportedMessageUrl}. ${additionalInfo ? `\n\nAdditional information provided in report:\n${additionalInfo.split('\n').map(c => `> ${c}`).join('\n')}` : ''}`, `Ticket: Message Report From ${reporter.displayName}`, [reporterId, interaction.user.id]); + if (!thread) throw new Error('An error occured and a thread was not created.'); - if (thread) { - await interaction.reply({ - flags: [MessageFlags.Ephemeral], - content: `Ticket created: ${thread}` - }); + await interaction.editReply({ content: `Ticket Created: ${thread}` }); const requestIndex = reportEmbed.fields.findIndex(f => f.name == 'User Requested Private Ticket'); if (requestIndex != -1) reportEmbed.fields.splice(requestIndex, 1); @@ -35,6 +36,10 @@ export default { }); await message.edit({ embeds: [reportEmbed], components: [] }); + } catch (error: Error | any) { + await interaction.editReply({ content: error.message }).catch(() => {}); } } -}; \ No newline at end of file +} + +export default new OpenTicketForReportedMessageButton(); \ No newline at end of file