blob: 7a06d8388eed99d0abe745df43711aa69c0ea77a [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;
pineafan6fb3e072022-05-20 19:27:23 +010017 customBoolean: () => any;
18 customBooleanClicked: boolean = null;
PineappleFan19b002b2022-05-19 11:54:01 +010019 inverted: boolean;
pineafan4f164f32022-02-26 22:07:12 +000020
21 constructor(interaction: CommandInteraction) {
22 this.interaction = interaction;
23
24 this.title = "";
25 this.emoji = "";
26 this.description = "";
27 this.color = "";
pineafan6fb3e072022-05-20 19:27:23 +010028 this.inverted = false;
29 this.customCallback = () => {};
30 this.customBoolean = () => {};
pineafan4f164f32022-02-26 22:07:12 +000031 }
32
33 setTitle(title: string) { this.title = title; return this }
34 setEmoji(emoji: string) { this.emoji = emoji; return this }
35 setDescription(description: string) { this.description = description; return this }
36 setColor(color: string) { this.color = color; return this }
pineafan167bde32022-05-19 19:33:46 +010037 setInverted(inverted: boolean) { this.inverted = inverted; return this }
pineafan377794f2022-04-18 19:01:01 +010038 addCustomCallback(title: string, disabled: boolean, callback: () => any, callbackClicked: string) {
pineafan6fb3e072022-05-20 19:27:23 +010039 if (this.customButtonTitle) return this
pineafan377794f2022-04-18 19:01:01 +010040 this.customButtonTitle = title;
41 this.customButtonDisabled = disabled;
42 this.customCallback = callback;
43 this.customCallbackString = callbackClicked;
44 return this;
45 }
pineafan6fb3e072022-05-20 19:27:23 +010046 addCustomBoolean(title: string, disabled: boolean, callback: () => any, callbackClicked: string) {
47 if (this.customButtonTitle) return this
48 this.customButtonTitle = title;
49 this.customButtonDisabled = disabled;
50 this.customBoolean = callback;
51 this.customCallbackString = callbackClicked;
52 this.customBooleanClicked = false;
53 return this;
54 }
55
pineafan4f164f32022-02-26 22:07:12 +000056
pineafan8b4b17f2022-02-27 20:42:52 +000057 async send(editOnly?: boolean) {
pineafan377794f2022-04-18 19:01:01 +010058 while (true) {
59 let object = {
60 embeds: [
61 new generateEmojiEmbed()
62 .setEmoji(this.emoji)
63 .setTitle(this.title)
64 .setDescription(this.description)
65 .setStatus(this.color)
pineafan6fb3e072022-05-20 19:27:23 +010066 .setFooter({text: (this.customBooleanClicked ?? this.customCallbackClicked) ? this.customCallbackString : ""})
pineafan377794f2022-04-18 19:01:01 +010067 ],
68 components: [
69 new MessageActionRow().addComponents([
70 new Discord.MessageButton()
71 .setCustomId("yes")
PineappleFan19b002b2022-05-19 11:54:01 +010072 .setLabel("Confirm")
73 .setStyle(this.inverted ? "SUCCESS" : "DANGER")
pineafan377794f2022-04-18 19:01:01 +010074 .setEmoji(getEmojiByName("CONTROL.TICK", "id")),
75 new Discord.MessageButton()
76 .setCustomId("no")
77 .setLabel("Cancel")
pineafan17aba6d2022-05-19 20:27:22 +010078 .setStyle("SECONDARY")
pineafan377794f2022-04-18 19:01:01 +010079 .setEmoji(getEmojiByName("CONTROL.CROSS", "id"))
80 ].concat(this.customButtonTitle ? [new Discord.MessageButton()
81 .setCustomId("custom")
82 .setLabel(this.customButtonTitle)
pineafan6fb3e072022-05-20 19:27:23 +010083 .setStyle(this.customBooleanClicked !== null ?
84 ( this.customBooleanClicked ? "SUCCESS" : "PRIMARY" ) :
85 "PRIMARY"
86 )
pineafan377794f2022-04-18 19:01:01 +010087 .setDisabled(this.customButtonDisabled)
pineafane625d782022-05-09 18:04:32 +010088 .setEmoji(getEmojiByName("CONTROL.TICKET", "id"))
pineafan377794f2022-04-18 19:01:01 +010089 ] : []))
90 ],
91 ephemeral: true,
92 fetchReply: true
93 }
94 let m;
95 if ( editOnly ) {
96 m = await this.interaction.editReply(object);
97 } else {
98 m = await this.interaction.reply(object)
99 }
100 let component;
101 try {
102 component = await (m as Message).awaitMessageComponent({filter: (m) => m.user.id === this.interaction.user.id, time: 2.5 * 60 * 1000});
103 } catch (e) {
pineafan6fb3e072022-05-20 19:27:23 +0100104 return {
105 success: false,
106 buttonClicked: this.customBooleanClicked ?? this.customCallbackClicked,
107 response: this.customCallbackResponse
108 };
pineafan377794f2022-04-18 19:01:01 +0100109 }
110 if (component.customId === "yes") {
111 component.deferUpdate();
pineafan6fb3e072022-05-20 19:27:23 +0100112 if (this.customBooleanClicked === true) this.customCallbackResponse = await this.customBoolean();
113 return {
114 success: true,
115 buttonClicked: this.customBooleanClicked ?? this.customCallbackClicked,
116 response: this.customCallbackResponse
117 };
pineafan377794f2022-04-18 19:01:01 +0100118 } else if (component.customId === "no") {
119 component.deferUpdate();
pineafan6fb3e072022-05-20 19:27:23 +0100120 return {
121 success: false,
122 buttonClicked: this.customBooleanClicked ?? this.customCallbackClicked,
123 response: this.customCallbackResponse
124 };
pineafan377794f2022-04-18 19:01:01 +0100125 } else if (component.customId === "custom") {
126 component.deferUpdate();
pineafan6fb3e072022-05-20 19:27:23 +0100127 if (this.customBooleanClicked !== null) {
128 this.customBooleanClicked = !this.customBooleanClicked;
129 } else {
130 this.customCallbackResponse = await this.customCallback();
131 this.customCallbackClicked = true;
132 this.customButtonDisabled = true;
133 }
pineafan377794f2022-04-18 19:01:01 +0100134 editOnly = true;
135 }
pineafan8b4b17f2022-02-27 20:42:52 +0000136 }
pineafan4f164f32022-02-26 22:07:12 +0000137 }
138}
139
140export default confirmationMessage;