blob: 8281ead498eed99ab21e6903ad2bb9e4ce886bf1 [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 }
pineafan377794f2022-04-18 19:01:01 +010034 addCustomCallback(title: string, disabled: boolean, callback: () => any, callbackClicked: string) {
35 this.customButtonTitle = title;
36 this.customButtonDisabled = disabled;
37 this.customCallback = callback;
38 this.customCallbackString = callbackClicked;
39 return this;
40 }
pineafan4f164f32022-02-26 22:07:12 +000041
pineafan8b4b17f2022-02-27 20:42:52 +000042 async send(editOnly?: boolean) {
pineafan377794f2022-04-18 19:01:01 +010043 while (true) {
44 let object = {
45 embeds: [
46 new generateEmojiEmbed()
47 .setEmoji(this.emoji)
48 .setTitle(this.title)
49 .setDescription(this.description)
50 .setStatus(this.color)
51 .setFooter({text: this.customCallbackClicked ? this.customCallbackString : ""})
52 ],
53 components: [
54 new MessageActionRow().addComponents([
55 new Discord.MessageButton()
56 .setCustomId("yes")
PineappleFan19b002b2022-05-19 11:54:01 +010057 .setLabel("Confirm")
58 .setStyle(this.inverted ? "SUCCESS" : "DANGER")
pineafan377794f2022-04-18 19:01:01 +010059 .setEmoji(getEmojiByName("CONTROL.TICK", "id")),
60 new Discord.MessageButton()
61 .setCustomId("no")
62 .setLabel("Cancel")
PineappleFan19b002b2022-05-19 11:54:01 +010063 .setStyle(this.inverted ? "DANGER" : "SUCCESS")
pineafan377794f2022-04-18 19:01:01 +010064 .setEmoji(getEmojiByName("CONTROL.CROSS", "id"))
65 ].concat(this.customButtonTitle ? [new Discord.MessageButton()
66 .setCustomId("custom")
67 .setLabel(this.customButtonTitle)
68 .setStyle("SECONDARY")
69 .setDisabled(this.customButtonDisabled)
pineafane625d782022-05-09 18:04:32 +010070 .setEmoji(getEmojiByName("CONTROL.TICKET", "id"))
pineafan377794f2022-04-18 19:01:01 +010071 ] : []))
72 ],
73 ephemeral: true,
74 fetchReply: true
75 }
76 let m;
77 if ( editOnly ) {
78 m = await this.interaction.editReply(object);
79 } else {
80 m = await this.interaction.reply(object)
81 }
82 let component;
83 try {
84 component = await (m as Message).awaitMessageComponent({filter: (m) => m.user.id === this.interaction.user.id, time: 2.5 * 60 * 1000});
85 } catch (e) {
PineappleFan19b002b2022-05-19 11:54:01 +010086 return { success: false, buttonClicked: this.customCallbackClicked, response: this.customCallbackResponse };
pineafan377794f2022-04-18 19:01:01 +010087 }
88 if (component.customId === "yes") {
89 component.deferUpdate();
PineappleFan19b002b2022-05-19 11:54:01 +010090 return { success: true, buttonClicked: this.customCallbackClicked, response: this.customCallbackResponse };
pineafan377794f2022-04-18 19:01:01 +010091 } else if (component.customId === "no") {
92 component.deferUpdate();
PineappleFan19b002b2022-05-19 11:54:01 +010093 return { success: false, buttonClicked: this.customCallbackClicked, response: this.customCallbackResponse };
pineafan377794f2022-04-18 19:01:01 +010094 } else if (component.customId === "custom") {
95 component.deferUpdate();
96 this.customCallbackResponse = this.customCallback();
97 this.customCallbackClicked = true;
98 this.customButtonDisabled = true;
99 editOnly = true;
100 }
pineafan8b4b17f2022-02-27 20:42:52 +0000101 }
pineafan4f164f32022-02-26 22:07:12 +0000102 }
103}
104
105export default confirmationMessage;