stats channels
diff --git a/src/utils/confirmationMessage.ts b/src/utils/confirmationMessage.ts
index da10cfb..988fd37 100644
--- a/src/utils/confirmationMessage.ts
+++ b/src/utils/confirmationMessage.ts
@@ -3,20 +3,24 @@
import EmojiEmbed from "./generateEmojiEmbed.js"
import getEmojiByName from "./getEmojiByName.js";
+
+interface CustomBoolean<T> {
+ title: string;
+ disabled: boolean;
+ value: string | null;
+ emoji: string | null;
+ active: boolean;
+ onClick: () => Promise<T>;
+ response: T | null;
+}
+
class confirmationMessage {
interaction: CommandInteraction;
title: string = "";
emoji: string = "";
description: string = "";
color: string = "";
- customCallback: () => any = () => {};
- customButtonTitle: string;
- customButtonDisabled: boolean;
- customCallbackString: string = "";
- customCallbackClicked: boolean = false;
- customCallbackResponse: any = null;
- customBoolean: () => any = () => {}; // allow multiple booleans
- customBooleanClicked: boolean = null;
+ customButtons: {[index:string]: CustomBoolean<unknown>} = {};
inverted: boolean = false;
reason: string | null = null;
@@ -29,21 +33,15 @@
setDescription(description: string) { this.description = description; return this }
setColor(color: string) { this.color = color; return this }
setInverted(inverted: boolean) { this.inverted = inverted; return this }
- addCustomCallback(title: string, disabled: boolean, callback: () => any, callbackClicked: string) {
- if (this.customButtonTitle) return this
- this.customButtonTitle = title;
- this.customButtonDisabled = disabled;
- this.customCallback = callback;
- this.customCallbackString = callbackClicked;
- return this;
- }
- addCustomBoolean(title: string, disabled: boolean, callback: () => any, callbackClicked: string) {
- if (this.customButtonTitle) return this
- this.customButtonTitle = title;
- this.customButtonDisabled = disabled;
- this.customBoolean = callback;
- this.customCallbackString = callbackClicked;
- this.customBooleanClicked = false;
+ addCustomBoolean(customId: string, title: string, disabled: boolean, callback: () => Promise<unknown> | null, callbackClicked: string | null, emoji?: string, initial?: boolean) { this.customButtons[customId] = {
+ title: title,
+ disabled: disabled,
+ value: callbackClicked,
+ emoji: emoji,
+ active: initial ?? false,
+ onClick: callback ?? (() => null),
+ response: null,
+ }
return this;
}
addReasonButton(reason: string) {
@@ -52,92 +50,80 @@
}
async send(editOnly?: boolean) {
while (true) {
+ let fullComponents = [
+ new Discord.MessageButton()
+ .setCustomId("yes")
+ .setLabel("Confirm")
+ .setStyle(this.inverted ? "SUCCESS" : "DANGER")
+ .setEmoji(getEmojiByName("CONTROL.TICK", "id")),
+ new Discord.MessageButton()
+ .setCustomId("no")
+ .setLabel("Cancel")
+ .setStyle("SECONDARY")
+ .setEmoji(getEmojiByName("CONTROL.CROSS", "id"))
+ ]
+ Object.entries(this.customButtons).forEach(([k, v]) => {
+ fullComponents.push(new Discord.MessageButton()
+ .setCustomId(k)
+ .setLabel(v.title)
+ .setStyle(v.active ? "SUCCESS" : "PRIMARY")
+ .setEmoji(getEmojiByName(v.emoji, "id"))
+ .setDisabled(v.disabled))
+ })
+ if (this.reason !== null) fullComponents.push(new Discord.MessageButton()
+ .setCustomId("reason")
+ .setLabel(`Edit Reason`)
+ .setStyle("PRIMARY")
+ .setEmoji(getEmojiByName("ICONS.EDIT", "id"))
+ .setDisabled(false)
+ )
+ let components = []
+ for (let i = 0; i < fullComponents.length; i += 5) {
+ components.push(new MessageActionRow().addComponents(fullComponents.slice(i, i + 5)));
+ }
let object = {
embeds: [
new EmojiEmbed()
.setEmoji(this.emoji)
.setTitle(this.title)
- .setDescription(this.description)
+ .setDescription(this.description + "\n\n" + Object.values(this.customButtons).map(v => {
+ if (v.value === null) return "";
+ return v.active ? `*${v.value}*\n` : "";
+ }).join(""))
.setStatus(this.color)
- .setFooter({text: (this.customBooleanClicked ?? this.customCallbackClicked) ? this.customCallbackString : ""})
],
- components: [
- new MessageActionRow().addComponents([
- new Discord.MessageButton()
- .setCustomId("yes")
- .setLabel("Confirm")
- .setStyle(this.inverted ? "SUCCESS" : "DANGER")
- .setEmoji(getEmojiByName("CONTROL.TICK", "id")),
- new Discord.MessageButton()
- .setCustomId("no")
- .setLabel("Cancel")
- .setStyle("SECONDARY")
- .setEmoji(getEmojiByName("CONTROL.CROSS", "id"))
- ].concat(this.customButtonTitle ? [new Discord.MessageButton()
- .setCustomId("custom")
- .setLabel(this.customButtonTitle)
- .setStyle(this.customBooleanClicked !== null ?
- ( this.customBooleanClicked ? "SUCCESS" : "PRIMARY" ) :
- "PRIMARY"
- )
- .setDisabled(this.customButtonDisabled)
- .setEmoji(getEmojiByName("CONTROL.TICKET", "id"))
- ] : [])
- .concat(this.reason !== null ? [new Discord.MessageButton()
- .setCustomId("reason")
- .setLabel(`Edit Reason`)
- .setStyle("PRIMARY")
- .setEmoji(getEmojiByName("ICONS.EDIT", "id"))
- ] : []))
- ],
+ components: components,
ephemeral: true,
fetchReply: true
}
let m;
- if ( editOnly ) {
- m = await this.interaction.editReply(object);
- } else {
- m = await this.interaction.reply(object)
- }
+ try {
+ if ( editOnly ) {
+ m = await this.interaction.editReply(object);
+ } else {
+ m = await this.interaction.reply(object)
+ }
+ } catch { return { cancelled: true } }
let component;
try {
component = await (m as Message).awaitMessageComponent({filter: (m) => m.user.id === this.interaction.user.id, time: 300000});
} catch (e) {
- return {
- success: false,
- buttonClicked: this.customBooleanClicked ?? this.customCallbackClicked,
- response: this.customCallbackResponse
- };
+ return { success: false, components: this.customButtons };
}
if (component.customId === "yes") {
component.deferUpdate();
- if (this.customBooleanClicked === true) this.customCallbackResponse = await this.customBoolean();
- return {
- success: true,
- buttonClicked: this.customBooleanClicked ?? this.customCallbackClicked,
- response: this.customCallbackResponse
+ for (let [k, v] of Object.entries(this.customButtons)) {
+ if (!v.active) continue
+ try { v.response = await v.onClick(); }
+ catch (e) { console.log(e) }
};
+ return { success: true, components: this.customButtons };
} else if (component.customId === "no") {
component.deferUpdate();
- return {
- success: false,
- buttonClicked: this.customBooleanClicked ?? this.customCallbackClicked,
- response: this.customCallbackResponse
- };
- } else if (component.customId === "custom") {
- component.deferUpdate();
- if (this.customBooleanClicked !== null) {
- this.customBooleanClicked = !this.customBooleanClicked;
- } else {
- this.customCallbackResponse = await this.customCallback();
- this.customCallbackClicked = true;
- this.customButtonDisabled = true;
- }
- editOnly = true;
+ return { success: false, components: this.customButtons };
} else if (component.customId === "reason") {
await component.showModal(new Discord.Modal().setCustomId("modal").setTitle(`Editing reason`).addComponents(
- // @ts-ignore
- new MessageActionRow().addComponents(new TextInputComponent()
+ new MessageActionRow<TextInputComponent>().addComponents(new TextInputComponent()
.setCustomId("reason")
.setLabel("Reason")
.setMaxLength(2000)
@@ -163,10 +149,13 @@
let out;
try {
out = await modalInteractionCollector(m, (m) => m.channel.id == this.interaction.channel.id, (m) => m.customId == "reason")
- } catch (e) { continue }
- if (out.fields) {
- return {newReason: out.fields.getTextInputValue("reason") ?? ""};
- } else { return { newReason: this.reason } }
+ } catch (e) { return {} }
+ if (out.fields) { return { newReason: out.fields.getTextInputValue("reason") ?? "" }; }
+ else { return { newReason: this.reason } }
+ } else {
+ component.deferUpdate();
+ this.customButtons[component.customId].active = !this.customButtons[component.customId].active;
+ return { components: this.customButtons };
}
}
}