(buttons): Implement close-mod-ticket button & fix claim-ticket.

This commit is contained in:
2026-10-14 10:23:00 +08:00
parent f4f0044dc3
commit 0d2e4403f7
2 changed files with 42 additions and 35 deletions
+7 -8
View File
@@ -7,6 +7,7 @@ class ClaimTicketButton implements Button<Client> {
async execute(context: Client<boolean>, interaction: ButtonInteraction, ...args: any[]): Promise<void> { async execute(context: Client<boolean>, interaction: ButtonInteraction, ...args: any[]): Promise<void> {
if (!interaction.inGuild()) return; if (!interaction.inGuild()) return;
await interaction.deferReply({ flags: [MessageFlags.Ephemeral] });
try { try {
const channel = await context.channels.fetch(interaction.channelId); const channel = await context.channels.fetch(interaction.channelId);
@@ -17,7 +18,10 @@ class ClaimTicketButton implements Button<Client> {
const settings = await Database.getOrCreateSettings(interaction.guildId); const settings = await Database.getOrCreateSettings(interaction.guildId);
if (!settings.private_help_role_id) throw new Error("\`private_help_role_id\` hasn't been configured. Please report this error to staff."); if (!settings.private_help_role_id) throw new Error("\`private_help_role_id\` hasn't been configured. Please report this error to staff.");
if (!(interaction.member.roles as string[]).includes(settings.private_help_role_id)) throw new Error("You don't have permission to claim tickets.");
const guild = await context.guilds.fetch(interaction.guildId);
const member = await guild.members.fetch(interaction.user.id);
if (!member.roles.cache.has(settings.private_help_role_id)) throw new Error("You don't have permission to claim tickets.");
const closeButton = new ButtonBuilder() const closeButton = new ButtonBuilder()
.setCustomId('close-ticket') .setCustomId('close-ticket')
@@ -36,14 +40,9 @@ class ClaimTicketButton implements Button<Client> {
components: [row] components: [row]
}); });
await interaction.reply({ content: `Ticket claimed by ${interaction.user}.` }); await interaction.editReply({ content: `Ticket claimed by ${interaction.user}.` });
} catch (error: Error | any) { } catch (error: Error | any) {
await interaction.reply({ await interaction.editReply({ content: error.message }).catch(() => {});
content: error.message,
flags: MessageFlags.Ephemeral,
}).catch(() => {});
return;
} }
} }
} }
+31 -23
View File
@@ -1,31 +1,39 @@
import { ButtonInteraction, Client, MessageFlags, ChannelType, PermissionFlagsBits } from 'discord.js'; import { ButtonInteraction, Client, MessageFlags, ChannelType } from 'discord.js';
import { Button } from '../types';
export default { class CloseModTicketButton implements Button<Client> {
name: 'close-mod-ticket', id = 'close-mod-ticket';
handler: async function (client: Client, interaction: ButtonInteraction) {
await interaction.deferReply({ flags: [MessageFlags.Ephemeral] });
const channel = await interaction.channel?.fetch();
const guild = await interaction.guild?.fetch();
const member = await guild?.members.fetch(interaction.user.id);
if (!channel || !channel.isThread() || !channel.isSendable() || channel.type != ChannelType.PrivateThread || !member) async execute(context: Client<boolean>, interaction: ButtonInteraction, ...args: any[]): Promise<void> {
return interaction.editReply({ content: 'Oops. Something went wrong. Please report this to a staff member.' }); if (!interaction.inGuild()) return;
await interaction.deferReply({ flags: MessageFlags.Ephemeral });
if (!member.permissions.has(PermissionFlagsBits.KickMembers)) try {
return interaction.editReply({ content: 'Only staff members may close mod tickets.' }); const channel = await context.channels.fetch(interaction.channelId);
await interaction.message.edit({ if (!channel) throw new Error("The interaction channel couldn't be fetched.");
content: interaction.message.content, if (!channel.isThread() || channel.type != ChannelType.PrivateThread) throw new Error('This interaction was not run in a ticket thread.');
components: [] if (!channel.isSendable()) throw new Error("I don't have permission to send messages in this channel.");
});
await channel.send('This ticket has been closed by staff.'); if (!interaction.memberPermissions.has('KickMembers')) throw new Error("You don't have permission to close this ticket.");
await interaction.editReply({ content: 'Ticket closed.' }); await interaction.message.edit({
content: interaction.message.content,
components: []
});
channel.edit({ await channel.send('This ticket has been closed by staff.');
archived: true,
locked: true await interaction.editReply({ content: 'Ticket closed.' });
});
await channel.edit({
archived: true,
locked: true
});
} catch (error: Error | any) {
await interaction.editReply({ content: error.message }).catch(() => {});
}
} }
}; }
export default new CloseModTicketButton();