pineafan | 73a7c4a | 2022-07-24 10:38:04 +0100 | [diff] [blame] | 1 | import Discord, { CommandInteraction, MessageActionRow, Message, MessageButton, TextInputComponent } from "discord.js"; |
| 2 | import { modalInteractionCollector } from "./dualCollector.js"; |
pineafan | 4edb776 | 2022-06-26 19:21:04 +0100 | [diff] [blame] | 3 | import EmojiEmbed from "./generateEmojiEmbed.js" |
pineafan | 8b4b17f | 2022-02-27 20:42:52 +0000 | [diff] [blame] | 4 | import getEmojiByName from "./getEmojiByName.js"; |
pineafan | 4f164f3 | 2022-02-26 22:07:12 +0000 | [diff] [blame] | 5 | |
pineafan | 02ba023 | 2022-07-24 22:16:15 +0100 | [diff] [blame] | 6 | |
| 7 | interface CustomBoolean<T> { |
| 8 | title: string; |
| 9 | disabled: boolean; |
| 10 | value: string | null; |
| 11 | emoji: string | null; |
| 12 | active: boolean; |
| 13 | onClick: () => Promise<T>; |
| 14 | response: T | null; |
| 15 | } |
| 16 | |
pineafan | 4f164f3 | 2022-02-26 22:07:12 +0000 | [diff] [blame] | 17 | class confirmationMessage { |
| 18 | interaction: CommandInteraction; |
pineafan | 73a7c4a | 2022-07-24 10:38:04 +0100 | [diff] [blame] | 19 | title: string = ""; |
| 20 | emoji: string = ""; |
| 21 | description: string = ""; |
| 22 | color: string = ""; |
pineafan | 02ba023 | 2022-07-24 22:16:15 +0100 | [diff] [blame] | 23 | customButtons: {[index:string]: CustomBoolean<unknown>} = {}; |
pineafan | 73a7c4a | 2022-07-24 10:38:04 +0100 | [diff] [blame] | 24 | inverted: boolean = false; |
| 25 | reason: string | null = null; |
pineafan | 4f164f3 | 2022-02-26 22:07:12 +0000 | [diff] [blame] | 26 | |
| 27 | constructor(interaction: CommandInteraction) { |
| 28 | this.interaction = interaction; |
pineafan | 4f164f3 | 2022-02-26 22:07:12 +0000 | [diff] [blame] | 29 | } |
| 30 | |
| 31 | setTitle(title: string) { this.title = title; return this } |
| 32 | setEmoji(emoji: string) { this.emoji = emoji; return this } |
| 33 | setDescription(description: string) { this.description = description; return this } |
| 34 | setColor(color: string) { this.color = color; return this } |
pineafan | 167bde3 | 2022-05-19 19:33:46 +0100 | [diff] [blame] | 35 | setInverted(inverted: boolean) { this.inverted = inverted; return this } |
pineafan | 02ba023 | 2022-07-24 22:16:15 +0100 | [diff] [blame] | 36 | addCustomBoolean(customId: string, title: string, disabled: boolean, callback: () => Promise<unknown> | null, callbackClicked: string | null, emoji?: string, initial?: boolean) { this.customButtons[customId] = { |
| 37 | title: title, |
| 38 | disabled: disabled, |
| 39 | value: callbackClicked, |
| 40 | emoji: emoji, |
| 41 | active: initial ?? false, |
| 42 | onClick: callback ?? (() => null), |
| 43 | response: null, |
| 44 | } |
pineafan | 6fb3e07 | 2022-05-20 19:27:23 +0100 | [diff] [blame] | 45 | return this; |
| 46 | } |
pineafan | 73a7c4a | 2022-07-24 10:38:04 +0100 | [diff] [blame] | 47 | addReasonButton(reason: string) { |
| 48 | this.reason = reason; |
| 49 | return this; |
| 50 | } |
pineafan | 8b4b17f | 2022-02-27 20:42:52 +0000 | [diff] [blame] | 51 | async send(editOnly?: boolean) { |
pineafan | 377794f | 2022-04-18 19:01:01 +0100 | [diff] [blame] | 52 | while (true) { |
pineafan | 02ba023 | 2022-07-24 22:16:15 +0100 | [diff] [blame] | 53 | let fullComponents = [ |
| 54 | new Discord.MessageButton() |
| 55 | .setCustomId("yes") |
| 56 | .setLabel("Confirm") |
| 57 | .setStyle(this.inverted ? "SUCCESS" : "DANGER") |
| 58 | .setEmoji(getEmojiByName("CONTROL.TICK", "id")), |
| 59 | new Discord.MessageButton() |
| 60 | .setCustomId("no") |
| 61 | .setLabel("Cancel") |
| 62 | .setStyle("SECONDARY") |
| 63 | .setEmoji(getEmojiByName("CONTROL.CROSS", "id")) |
| 64 | ] |
| 65 | Object.entries(this.customButtons).forEach(([k, v]) => { |
| 66 | fullComponents.push(new Discord.MessageButton() |
| 67 | .setCustomId(k) |
| 68 | .setLabel(v.title) |
| 69 | .setStyle(v.active ? "SUCCESS" : "PRIMARY") |
| 70 | .setEmoji(getEmojiByName(v.emoji, "id")) |
| 71 | .setDisabled(v.disabled)) |
| 72 | }) |
| 73 | if (this.reason !== null) fullComponents.push(new Discord.MessageButton() |
| 74 | .setCustomId("reason") |
| 75 | .setLabel(`Edit Reason`) |
| 76 | .setStyle("PRIMARY") |
| 77 | .setEmoji(getEmojiByName("ICONS.EDIT", "id")) |
| 78 | .setDisabled(false) |
| 79 | ) |
| 80 | let components = [] |
| 81 | for (let i = 0; i < fullComponents.length; i += 5) { |
| 82 | components.push(new MessageActionRow().addComponents(fullComponents.slice(i, i + 5))); |
| 83 | } |
pineafan | 377794f | 2022-04-18 19:01:01 +0100 | [diff] [blame] | 84 | let object = { |
| 85 | embeds: [ |
pineafan | 4edb776 | 2022-06-26 19:21:04 +0100 | [diff] [blame] | 86 | new EmojiEmbed() |
pineafan | 377794f | 2022-04-18 19:01:01 +0100 | [diff] [blame] | 87 | .setEmoji(this.emoji) |
| 88 | .setTitle(this.title) |
pineafan | 02ba023 | 2022-07-24 22:16:15 +0100 | [diff] [blame] | 89 | .setDescription(this.description + "\n\n" + Object.values(this.customButtons).map(v => { |
| 90 | if (v.value === null) return ""; |
| 91 | return v.active ? `*${v.value}*\n` : ""; |
| 92 | }).join("")) |
pineafan | 377794f | 2022-04-18 19:01:01 +0100 | [diff] [blame] | 93 | .setStatus(this.color) |
pineafan | 377794f | 2022-04-18 19:01:01 +0100 | [diff] [blame] | 94 | ], |
pineafan | 02ba023 | 2022-07-24 22:16:15 +0100 | [diff] [blame] | 95 | components: components, |
pineafan | 377794f | 2022-04-18 19:01:01 +0100 | [diff] [blame] | 96 | ephemeral: true, |
| 97 | fetchReply: true |
| 98 | } |
| 99 | let m; |
pineafan | 02ba023 | 2022-07-24 22:16:15 +0100 | [diff] [blame] | 100 | try { |
| 101 | if ( editOnly ) { |
| 102 | m = await this.interaction.editReply(object); |
| 103 | } else { |
| 104 | m = await this.interaction.reply(object) |
| 105 | } |
| 106 | } catch { return { cancelled: true } } |
pineafan | 377794f | 2022-04-18 19:01:01 +0100 | [diff] [blame] | 107 | let component; |
| 108 | try { |
pineafan | c6158ab | 2022-06-17 16:34:07 +0100 | [diff] [blame] | 109 | component = await (m as Message).awaitMessageComponent({filter: (m) => m.user.id === this.interaction.user.id, time: 300000}); |
pineafan | 377794f | 2022-04-18 19:01:01 +0100 | [diff] [blame] | 110 | } catch (e) { |
pineafan | 02ba023 | 2022-07-24 22:16:15 +0100 | [diff] [blame] | 111 | return { success: false, components: this.customButtons }; |
pineafan | 377794f | 2022-04-18 19:01:01 +0100 | [diff] [blame] | 112 | } |
| 113 | if (component.customId === "yes") { |
| 114 | component.deferUpdate(); |
pineafan | 02ba023 | 2022-07-24 22:16:15 +0100 | [diff] [blame] | 115 | for (let [k, v] of Object.entries(this.customButtons)) { |
| 116 | if (!v.active) continue |
| 117 | try { v.response = await v.onClick(); } |
| 118 | catch (e) { console.log(e) } |
pineafan | 6fb3e07 | 2022-05-20 19:27:23 +0100 | [diff] [blame] | 119 | }; |
pineafan | 02ba023 | 2022-07-24 22:16:15 +0100 | [diff] [blame] | 120 | return { success: true, components: this.customButtons }; |
pineafan | 377794f | 2022-04-18 19:01:01 +0100 | [diff] [blame] | 121 | } else if (component.customId === "no") { |
| 122 | component.deferUpdate(); |
pineafan | 02ba023 | 2022-07-24 22:16:15 +0100 | [diff] [blame] | 123 | return { success: false, components: this.customButtons }; |
pineafan | 73a7c4a | 2022-07-24 10:38:04 +0100 | [diff] [blame] | 124 | } else if (component.customId === "reason") { |
| 125 | await component.showModal(new Discord.Modal().setCustomId("modal").setTitle(`Editing reason`).addComponents( |
pineafan | 02ba023 | 2022-07-24 22:16:15 +0100 | [diff] [blame] | 126 | new MessageActionRow<TextInputComponent>().addComponents(new TextInputComponent() |
pineafan | 73a7c4a | 2022-07-24 10:38:04 +0100 | [diff] [blame] | 127 | .setCustomId("reason") |
| 128 | .setLabel("Reason") |
| 129 | .setMaxLength(2000) |
| 130 | .setRequired(false) |
| 131 | .setStyle("PARAGRAPH") |
| 132 | .setPlaceholder("Spammed in #general") |
| 133 | .setValue(this.reason ? this.reason : "") |
| 134 | ) |
| 135 | )) |
| 136 | await this.interaction.editReply({ |
| 137 | embeds: [new EmojiEmbed() |
| 138 | .setTitle(this.title) |
| 139 | .setDescription("Modal opened. If you can't see it, click back and try again.") |
| 140 | .setStatus(this.color) |
| 141 | .setEmoji(this.emoji) |
| 142 | ], components: [new MessageActionRow().addComponents([new MessageButton() |
| 143 | .setLabel("Back") |
| 144 | .setEmoji(getEmojiByName("CONTROL.LEFT", "id")) |
| 145 | .setStyle("PRIMARY") |
| 146 | .setCustomId("back") |
| 147 | ])] |
| 148 | }); |
| 149 | let out; |
| 150 | try { |
pineafan | e23c4ec | 2022-07-27 21:56:27 +0100 | [diff] [blame^] | 151 | out = await modalInteractionCollector(m, (m) => m.channel.id === this.interaction.channel.id, (m) => m.customId === "reason") |
pineafan | 02ba023 | 2022-07-24 22:16:15 +0100 | [diff] [blame] | 152 | } catch (e) { return {} } |
| 153 | if (out.fields) { return { newReason: out.fields.getTextInputValue("reason") ?? "" }; } |
| 154 | else { return { newReason: this.reason } } |
| 155 | } else { |
| 156 | component.deferUpdate(); |
| 157 | this.customButtons[component.customId].active = !this.customButtons[component.customId].active; |
| 158 | return { components: this.customButtons }; |
pineafan | 377794f | 2022-04-18 19:01:01 +0100 | [diff] [blame] | 159 | } |
pineafan | 8b4b17f | 2022-02-27 20:42:52 +0000 | [diff] [blame] | 160 | } |
pineafan | 4f164f3 | 2022-02-26 22:07:12 +0000 | [diff] [blame] | 161 | } |
| 162 | } |
| 163 | |
pineafan | 73a7c4a | 2022-07-24 10:38:04 +0100 | [diff] [blame] | 164 | export default confirmationMessage; |