diff options
author | IRONM00N <64110067+IRONM00N@users.noreply.github.com> | 2022-08-18 22:42:12 -0400 |
---|---|---|
committer | IRONM00N <64110067+IRONM00N@users.noreply.github.com> | 2022-08-18 22:42:12 -0400 |
commit | 2356d2c44736fb83021dacb551625852111c8ce6 (patch) | |
tree | 10408d22fdd7a358d2f5c5917c3b59e55aa4c19d /lib/common/ConfirmationPrompt.ts | |
parent | 8aed6f93f7740c592cbc0e2f9fd3269c05286077 (diff) | |
download | tanzanite-2356d2c44736fb83021dacb551625852111c8ce6.tar.gz tanzanite-2356d2c44736fb83021dacb551625852111c8ce6.tar.bz2 tanzanite-2356d2c44736fb83021dacb551625852111c8ce6.zip |
restructure, experimental presence and member automod, fixed bugs probably made some more bugs
Diffstat (limited to 'lib/common/ConfirmationPrompt.ts')
-rw-r--r-- | lib/common/ConfirmationPrompt.ts | 64 |
1 files changed, 64 insertions, 0 deletions
diff --git a/lib/common/ConfirmationPrompt.ts b/lib/common/ConfirmationPrompt.ts new file mode 100644 index 0000000..b87d9ef --- /dev/null +++ b/lib/common/ConfirmationPrompt.ts @@ -0,0 +1,64 @@ +import { type CommandMessage, type SlashMessage } from '#lib'; +import { ActionRowBuilder, ButtonBuilder, ButtonStyle, type MessageComponentInteraction, type MessageOptions } from 'discord.js'; + +/** + * Sends a message with buttons for the user to confirm or cancel the action. + */ +export class ConfirmationPrompt { + /** + * @param message The message that triggered the command + * @param messageOptions Options for sending the message + */ + protected constructor(protected message: CommandMessage | SlashMessage, protected messageOptions: MessageOptions) {} + + /** + * Sends a message with buttons for the user to confirm or cancel the action. + */ + protected async send(): Promise<boolean> { + this.messageOptions.components = [ + new ActionRowBuilder<ButtonBuilder>().addComponents( + new ButtonBuilder({ style: ButtonStyle.Success, customId: 'confirmationPrompt_confirm', label: 'Yes' }), + new ButtonBuilder({ style: ButtonStyle.Danger, customId: 'confirmationPrompt_cancel', label: 'No' }) + ) + ]; + + const msg = await this.message.channel!.send(this.messageOptions); + + return await new Promise<boolean>((resolve) => { + let responded = false; + const collector = msg.createMessageComponentCollector({ + filter: (interaction) => interaction.message?.id == msg.id, + time: 300_000 + }); + + collector.on('collect', async (interaction: MessageComponentInteraction) => { + await interaction.deferUpdate().catch(() => undefined); + if (interaction.user.id == this.message.author.id || this.message.client.config.owners.includes(interaction.user.id)) { + if (interaction.customId === 'confirmationPrompt_confirm') { + responded = true; + collector.stop(); + resolve(true); + } else if (interaction.customId === 'confirmationPrompt_cancel') { + responded = true; + collector.stop(); + resolve(false); + } + } + }); + + collector.on('end', async () => { + await msg.delete().catch(() => undefined); + if (!responded) resolve(false); + }); + }); + } + + /** + * Sends a message with buttons for the user to confirm or cancel the action. + * @param message The message that triggered the command + * @param sendOptions Options for sending the message + */ + public static async send(message: CommandMessage | SlashMessage, sendOptions: MessageOptions): Promise<boolean> { + return new ConfirmationPrompt(message, sendOptions).send(); + } +} |