blob: fc8b76c94b5cf956612b9efc8bc1e5d45a7c5e9f [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;
pineafan4f164f32022-02-26 22:07:12 +000017
18 constructor(interaction: CommandInteraction) {
19 this.interaction = interaction;
20
21 this.title = "";
22 this.emoji = "";
23 this.description = "";
24 this.color = "";
pineafan377794f2022-04-18 19:01:01 +010025 this.customCallback = () => {}
pineafan4f164f32022-02-26 22:07:12 +000026 }
27
28 setTitle(title: string) { this.title = title; return this }
29 setEmoji(emoji: string) { this.emoji = emoji; return this }
30 setDescription(description: string) { this.description = description; return this }
31 setColor(color: string) { this.color = color; return this }
pineafan377794f2022-04-18 19:01:01 +010032 addCustomCallback(title: string, disabled: boolean, callback: () => any, callbackClicked: string) {
33 this.customButtonTitle = title;
34 this.customButtonDisabled = disabled;
35 this.customCallback = callback;
36 this.customCallbackString = callbackClicked;
37 return this;
38 }
pineafan4f164f32022-02-26 22:07:12 +000039
pineafan8b4b17f2022-02-27 20:42:52 +000040 async send(editOnly?: boolean) {
pineafan377794f2022-04-18 19:01:01 +010041 while (true) {
42 let object = {
43 embeds: [
44 new generateEmojiEmbed()
45 .setEmoji(this.emoji)
46 .setTitle(this.title)
47 .setDescription(this.description)
48 .setStatus(this.color)
49 .setFooter({text: this.customCallbackClicked ? this.customCallbackString : ""})
50 ],
51 components: [
52 new MessageActionRow().addComponents([
53 new Discord.MessageButton()
54 .setCustomId("yes")
55 .setLabel("Yes")
56 .setStyle("SUCCESS")
57 .setEmoji(getEmojiByName("CONTROL.TICK", "id")),
58 new Discord.MessageButton()
59 .setCustomId("no")
60 .setLabel("Cancel")
61 .setStyle("DANGER")
62 .setEmoji(getEmojiByName("CONTROL.CROSS", "id"))
63 ].concat(this.customButtonTitle ? [new Discord.MessageButton()
64 .setCustomId("custom")
65 .setLabel(this.customButtonTitle)
66 .setStyle("SECONDARY")
67 .setDisabled(this.customButtonDisabled)
68 .setEmoji(getEmojiByName("CONTROL.RIGHT", "id")) // TODO: add an emoji
69 ] : []))
70 ],
71 ephemeral: true,
72 fetchReply: true
73 }
74 let m;
75 if ( editOnly ) {
76 m = await this.interaction.editReply(object);
77 } else {
78 m = await this.interaction.reply(object)
79 }
80 let component;
81 try {
82 component = await (m as Message).awaitMessageComponent({filter: (m) => m.user.id === this.interaction.user.id, time: 2.5 * 60 * 1000});
83 } catch (e) {
84 return {success: false, buttonClicked: this.customCallbackClicked, response: this.customCallbackResponse}; // TODO: Check the type of the error; change the error message here
85 }
86 if (component.customId === "yes") {
87 component.deferUpdate();
88 return {success: true, buttonClicked: this.customCallbackClicked, response: this.customCallbackResponse};
89 } else if (component.customId === "no") {
90 component.deferUpdate();
91 return {success: false, buttonClicked: this.customCallbackClicked, response: this.customCallbackResponse};
92 } else if (component.customId === "custom") {
93 component.deferUpdate();
94 this.customCallbackResponse = this.customCallback();
95 this.customCallbackClicked = true;
96 this.customButtonDisabled = true;
97 editOnly = true;
98 }
pineafan8b4b17f2022-02-27 20:42:52 +000099 }
pineafan4f164f32022-02-26 22:07:12 +0000100 }
101}
102
103export default confirmationMessage;