blob: 3226a43b500386340225fd50a6458ff596473aa9 [file] [log] [blame]
pineafan4f164f32022-02-26 22:07:12 +00001import Discord, { CommandInteraction, MessageActionRow, Message } from "discord.js";
pineafan377794f2022-04-18 19:01:01 +01002import generateEmojiEmbed from "./generateEmojiEmbed.js"
pineafan8b4b17f2022-02-27 20:42:52 +00003import getEmojiByName from "./getEmojiByName.js";
pineafan4f164f32022-02-26 22:07:12 +00004
5class confirmationMessage {
6 interaction: CommandInteraction;
7 title: string;
8 emoji: string;
9 description: string;
10 color: string;
pineafan377794f2022-04-18 19:01:01 +010011 customCallback: () => any;
12 customButtonTitle: string;
13 customButtonDisabled: boolean;
14 customCallbackString: string = "";
15 customCallbackClicked: boolean = false;
16 customCallbackResponse: any = null;
PineappleFan19b002b2022-05-19 11:54:01 +010017 inverted: boolean;
pineafan4f164f32022-02-26 22:07:12 +000018
19 constructor(interaction: CommandInteraction) {
20 this.interaction = interaction;
21
22 this.title = "";
23 this.emoji = "";
24 this.description = "";
25 this.color = "";
PineappleFan19b002b2022-05-19 11:54:01 +010026 this.inverted = false
pineafan377794f2022-04-18 19:01:01 +010027 this.customCallback = () => {}
pineafan4f164f32022-02-26 22:07:12 +000028 }
29
30 setTitle(title: string) { this.title = title; return this }
31 setEmoji(emoji: string) { this.emoji = emoji; return this }
32 setDescription(description: string) { this.description = description; return this }
33 setColor(color: string) { this.color = color; return this }
pineafan167bde32022-05-19 19:33:46 +010034 setInverted(inverted: boolean) { this.inverted = inverted; return this }
pineafan377794f2022-04-18 19:01:01 +010035 addCustomCallback(title: string, disabled: boolean, callback: () => any, callbackClicked: string) {
36 this.customButtonTitle = title;
37 this.customButtonDisabled = disabled;
38 this.customCallback = callback;
39 this.customCallbackString = callbackClicked;
40 return this;
41 }
pineafan4f164f32022-02-26 22:07:12 +000042
pineafan8b4b17f2022-02-27 20:42:52 +000043 async send(editOnly?: boolean) {
pineafan377794f2022-04-18 19:01:01 +010044 while (true) {
45 let object = {
46 embeds: [
47 new generateEmojiEmbed()
48 .setEmoji(this.emoji)
49 .setTitle(this.title)
50 .setDescription(this.description)
51 .setStatus(this.color)
52 .setFooter({text: this.customCallbackClicked ? this.customCallbackString : ""})
53 ],
54 components: [
55 new MessageActionRow().addComponents([
56 new Discord.MessageButton()
57 .setCustomId("yes")
PineappleFan19b002b2022-05-19 11:54:01 +010058 .setLabel("Confirm")
59 .setStyle(this.inverted ? "SUCCESS" : "DANGER")
pineafan377794f2022-04-18 19:01:01 +010060 .setEmoji(getEmojiByName("CONTROL.TICK", "id")),
61 new Discord.MessageButton()
62 .setCustomId("no")
63 .setLabel("Cancel")
PineappleFan19b002b2022-05-19 11:54:01 +010064 .setStyle(this.inverted ? "DANGER" : "SUCCESS")
pineafan377794f2022-04-18 19:01:01 +010065 .setEmoji(getEmojiByName("CONTROL.CROSS", "id"))
66 ].concat(this.customButtonTitle ? [new Discord.MessageButton()
67 .setCustomId("custom")
68 .setLabel(this.customButtonTitle)
69 .setStyle("SECONDARY")
70 .setDisabled(this.customButtonDisabled)
pineafane625d782022-05-09 18:04:32 +010071 .setEmoji(getEmojiByName("CONTROL.TICKET", "id"))
pineafan377794f2022-04-18 19:01:01 +010072 ] : []))
73 ],
74 ephemeral: true,
75 fetchReply: true
76 }
77 let m;
78 if ( editOnly ) {
79 m = await this.interaction.editReply(object);
80 } else {
81 m = await this.interaction.reply(object)
82 }
83 let component;
84 try {
85 component = await (m as Message).awaitMessageComponent({filter: (m) => m.user.id === this.interaction.user.id, time: 2.5 * 60 * 1000});
86 } catch (e) {
PineappleFan19b002b2022-05-19 11:54:01 +010087 return { success: false, buttonClicked: this.customCallbackClicked, response: this.customCallbackResponse };
pineafan377794f2022-04-18 19:01:01 +010088 }
89 if (component.customId === "yes") {
90 component.deferUpdate();
PineappleFan19b002b2022-05-19 11:54:01 +010091 return { success: true, buttonClicked: this.customCallbackClicked, response: this.customCallbackResponse };
pineafan377794f2022-04-18 19:01:01 +010092 } else if (component.customId === "no") {
93 component.deferUpdate();
PineappleFan19b002b2022-05-19 11:54:01 +010094 return { success: false, buttonClicked: this.customCallbackClicked, response: this.customCallbackResponse };
pineafan377794f2022-04-18 19:01:01 +010095 } else if (component.customId === "custom") {
96 component.deferUpdate();
97 this.customCallbackResponse = this.customCallback();
98 this.customCallbackClicked = true;
99 this.customButtonDisabled = true;
100 editOnly = true;
101 }
pineafan8b4b17f2022-02-27 20:42:52 +0000102 }
pineafan4f164f32022-02-26 22:07:12 +0000103 }
104}
105
106export default confirmationMessage;