mirror of
https://github.com/nx-bat/discordbot-ng.git
synced 2026-11-11 07:13:37 -05:00
(buttons): Implement claim-ticket button.
This commit is contained in:
+38
-27
@@ -1,40 +1,51 @@
|
|||||||
import { ActionRowBuilder, ButtonBuilder, ButtonInteraction, ButtonStyle, ChannelType, Client, MessageFlags, PermissionFlagsBits } from 'discord.js';
|
import { ActionRowBuilder, ButtonBuilder, ButtonInteraction, ButtonStyle, ChannelType, Client, MessageFlags } from 'discord.js';
|
||||||
import { Database } from '../shared/Database';
|
import { Database } from '../shared/Database';
|
||||||
|
import { Button } from '../types';
|
||||||
|
|
||||||
export default {
|
class ClaimTicketButton implements Button<Client> {
|
||||||
name: 'claim-ticket',
|
id = 'claim-ticket';
|
||||||
handler: async function (client: Client, interaction: ButtonInteraction) {
|
|
||||||
const channel = await interaction.channel?.fetch();
|
|
||||||
|
|
||||||
if (!channel || !channel.isThread() || !channel.isSendable() || channel.type != ChannelType.PrivateThread)
|
async execute(context: Client<boolean>, interaction: ButtonInteraction, ...args: any[]): Promise<void> {
|
||||||
return interaction.reply({ flags: [MessageFlags.Ephemeral], content: 'Oops. Something went wrong.' });
|
if (!interaction.inGuild()) return;
|
||||||
|
|
||||||
if (!interaction.memberPermissions) return interaction.reply({ flags: [MessageFlags.Ephemeral], content: 'An error has occurred.' });
|
try {
|
||||||
|
const channel = await context.channels.fetch(interaction.channelId);
|
||||||
|
|
||||||
if (!interaction.memberPermissions.has(PermissionFlagsBits.ManageMessages)) return interaction.reply({ flags: [MessageFlags.Ephemeral], content: 'You do not have permission to claim tickets.' });
|
if (!channel) throw new Error("The interaction channel couldn't be fetched.");
|
||||||
|
if (!channel.isThread() || channel.type != ChannelType.PrivateThread) throw new Error('This interaction was not run in a ticket thread.');
|
||||||
|
if (!channel.isSendable()) throw new Error("I don't have permission to send messages in this channel.");
|
||||||
|
|
||||||
const guild = await client.guilds.fetch(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 (!(interaction.member.roles as string[]).includes(settings.private_help_role_id)) throw new Error("You don't have permission to claim tickets.");
|
||||||
|
|
||||||
const settings = await Database.getOrCreateSettings(guild.id);
|
const closeButton = new ButtonBuilder()
|
||||||
if (!settings.private_help_role_id) return interaction.reply({ flags: [MessageFlags.Ephemeral], content: 'Failed to claim ticket.' });
|
.setCustomId('close-ticket')
|
||||||
|
.setLabel('Click here if you no longer need help')
|
||||||
|
.setStyle(ButtonStyle.Danger);
|
||||||
|
|
||||||
const closeButton = new ButtonBuilder()
|
const unclaimButton = new ButtonBuilder()
|
||||||
.setCustomId('close-ticket')
|
.setCustomId('unclaim-ticket')
|
||||||
.setLabel('Click here if you no longer need help')
|
.setLabel('Unclaim ticket')
|
||||||
.setStyle(ButtonStyle.Danger);
|
.setStyle(ButtonStyle.Primary);
|
||||||
|
|
||||||
const unclaimButton = new ButtonBuilder()
|
const row = new ActionRowBuilder<ButtonBuilder>().addComponents(closeButton, unclaimButton);
|
||||||
.setCustomId('unclaim-ticket')
|
|
||||||
.setLabel('Unclaim ticket')
|
|
||||||
.setStyle(ButtonStyle.Primary);
|
|
||||||
|
|
||||||
const row = new ActionRowBuilder<ButtonBuilder>().addComponents(closeButton, unclaimButton);
|
await interaction.message.edit({
|
||||||
|
content: `${interaction.message.content}\n\nClaimed by: ${interaction.user}`,
|
||||||
|
components: [row]
|
||||||
|
});
|
||||||
|
|
||||||
await interaction.message.edit({
|
await interaction.reply({ content: `Ticket claimed by ${interaction.user}.` });
|
||||||
content: `${interaction.message.content}\n\nClaimed by: ${interaction.user}`,
|
} catch (error: Error | any) {
|
||||||
components: [row]
|
await interaction.reply({
|
||||||
});
|
content: error.message,
|
||||||
|
flags: MessageFlags.Ephemeral,
|
||||||
|
}).catch(() => {});
|
||||||
|
|
||||||
await interaction.reply({ content: `Ticket claimed by ${interaction.user}.` });
|
return;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
};
|
}
|
||||||
|
|
||||||
|
export default new ClaimTicketButton();
|
||||||
Reference in New Issue
Block a user