blob: 2dc943a2dc19a8fb3606f286ad3d3469a2a814ef (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
|
import { type BushMessage, type BushSlashMessage } from '#lib';
import { ActionRow, ButtonComponent, 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 {
/**
* Options for sending the message
*/
protected messageOptions: MessageOptions;
/**
* The message that triggered the command
*/
protected message: BushMessage | BushSlashMessage;
/**
* @param message The message to respond to
* @param options The send message options
*/
protected constructor(message: BushMessage | BushSlashMessage, messageOptions: MessageOptions) {
this.message = message;
this.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 ActionRow().addComponents(
new ButtonComponent().setStyle(ButtonStyle.Success).setCustomId('confirmationPrompt_confirm').setLabel('Yes'),
new ButtonComponent().setStyle(ButtonStyle.Danger).setCustomId('confirmationPrompt_cancel').setLabel('No')
)
];
const msg = (await this.message.util.sendNew(this.messageOptions)) as BushMessage;
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 || client.config.owners.includes(interaction.user.id)) {
if (interaction.customId === 'confirmationPrompt_confirm') {
resolve(true);
responded = true;
collector.stop();
} else if (interaction.customId === 'confirmationPrompt_deny') {
resolve(false);
responded = true;
collector.stop();
}
}
});
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 to respond to
* @param options The send message options
*/
public static async send(message: BushMessage | BushSlashMessage, sendOptions: MessageOptions): Promise<boolean> {
return new ConfirmationPrompt(message, sendOptions).send();
}
}
|