Skyler Grey | 75ea917 | 2022-08-06 10:22:23 +0100 | [diff] [blame] | 1 | import Discord, { |
| 2 | CommandInteraction, |
Skyler Grey | 11236ba | 2022-08-08 21:13:33 +0100 | [diff] [blame] | 3 | Interaction, |
Skyler Grey | 75ea917 | 2022-08-06 10:22:23 +0100 | [diff] [blame] | 4 | Message, |
| 5 | MessageActionRow, |
| 6 | MessageButton, |
Skyler Grey | 11236ba | 2022-08-08 21:13:33 +0100 | [diff] [blame] | 7 | MessageComponentInteraction, |
| 8 | ModalSubmitInteraction, |
Skyler Grey | 75ea917 | 2022-08-06 10:22:23 +0100 | [diff] [blame] | 9 | TextInputComponent |
| 10 | } from "discord.js"; |
pineafan | 73a7c4a | 2022-07-24 10:38:04 +0100 | [diff] [blame] | 11 | import { modalInteractionCollector } from "./dualCollector.js"; |
pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame] | 12 | import EmojiEmbed from "./generateEmojiEmbed.js"; |
pineafan | 8b4b17f | 2022-02-27 20:42:52 +0000 | [diff] [blame] | 13 | import getEmojiByName from "./getEmojiByName.js"; |
pineafan | 4f164f3 | 2022-02-26 22:07:12 +0000 | [diff] [blame] | 14 | |
pineafan | 02ba023 | 2022-07-24 22:16:15 +0100 | [diff] [blame] | 15 | interface CustomBoolean<T> { |
| 16 | title: string; |
| 17 | disabled: boolean; |
| 18 | value: string | null; |
pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame] | 19 | emoji: string | undefined; |
pineafan | 02ba023 | 2022-07-24 22:16:15 +0100 | [diff] [blame] | 20 | active: boolean; |
| 21 | onClick: () => Promise<T>; |
| 22 | response: T | null; |
| 23 | } |
| 24 | |
pineafan | 4f164f3 | 2022-02-26 22:07:12 +0000 | [diff] [blame] | 25 | class confirmationMessage { |
| 26 | interaction: CommandInteraction; |
pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame] | 27 | title = ""; |
| 28 | emoji = ""; |
| 29 | description = ""; |
Skyler Grey | 75ea917 | 2022-08-06 10:22:23 +0100 | [diff] [blame] | 30 | color: "Danger" | "Warning" | "Success" = "Success"; |
| 31 | customButtons: Record<string, CustomBoolean<unknown>> = {}; |
pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame] | 32 | inverted = false; |
pineafan | 73a7c4a | 2022-07-24 10:38:04 +0100 | [diff] [blame] | 33 | reason: string | null = null; |
pineafan | 4f164f3 | 2022-02-26 22:07:12 +0000 | [diff] [blame] | 34 | |
| 35 | constructor(interaction: CommandInteraction) { |
| 36 | this.interaction = interaction; |
pineafan | 4f164f3 | 2022-02-26 22:07:12 +0000 | [diff] [blame] | 37 | } |
| 38 | |
Skyler Grey | 75ea917 | 2022-08-06 10:22:23 +0100 | [diff] [blame] | 39 | setTitle(title: string) { |
| 40 | this.title = title; |
| 41 | return this; |
| 42 | } |
| 43 | setEmoji(emoji: string) { |
| 44 | this.emoji = emoji; |
| 45 | return this; |
| 46 | } |
| 47 | setDescription(description: string) { |
| 48 | this.description = description; |
| 49 | return this; |
| 50 | } |
| 51 | setColor(color: "Danger" | "Warning" | "Success") { |
| 52 | this.color = color; |
| 53 | return this; |
| 54 | } |
| 55 | setInverted(inverted: boolean) { |
| 56 | this.inverted = inverted; |
| 57 | return this; |
| 58 | } |
| 59 | addCustomBoolean( |
| 60 | customId: string, |
| 61 | title: string, |
| 62 | disabled: boolean, |
| 63 | callback: () => Promise<unknown> = async () => null, |
| 64 | callbackClicked: string | null, |
| 65 | emoji?: string, |
| 66 | initial?: boolean |
| 67 | ) { |
| 68 | this.customButtons[customId] = { |
| 69 | title: title, |
| 70 | disabled: disabled, |
| 71 | value: callbackClicked, |
| 72 | emoji: emoji, |
| 73 | active: initial ?? false, |
| 74 | onClick: callback, |
| 75 | response: null |
| 76 | }; |
| 77 | return this; |
pineafan | 6fb3e07 | 2022-05-20 19:27:23 +0100 | [diff] [blame] | 78 | } |
pineafan | 73a7c4a | 2022-07-24 10:38:04 +0100 | [diff] [blame] | 79 | addReasonButton(reason: string) { |
| 80 | this.reason = reason; |
| 81 | return this; |
| 82 | } |
Skyler Grey | 11236ba | 2022-08-08 21:13:33 +0100 | [diff] [blame] | 83 | async send(editOnly?: boolean): Promise<{ |
| 84 | success?: boolean; |
| 85 | cancelled?: boolean; |
| 86 | components?: Record<string, CustomBoolean<unknown>>; |
| 87 | newReason?: string; |
| 88 | }> { |
pineafan | 377794f | 2022-04-18 19:01:01 +0100 | [diff] [blame] | 89 | while (true) { |
pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame] | 90 | const fullComponents = [ |
pineafan | 02ba023 | 2022-07-24 22:16:15 +0100 | [diff] [blame] | 91 | new Discord.MessageButton() |
| 92 | .setCustomId("yes") |
| 93 | .setLabel("Confirm") |
| 94 | .setStyle(this.inverted ? "SUCCESS" : "DANGER") |
| 95 | .setEmoji(getEmojiByName("CONTROL.TICK", "id")), |
| 96 | new Discord.MessageButton() |
| 97 | .setCustomId("no") |
| 98 | .setLabel("Cancel") |
| 99 | .setStyle("SECONDARY") |
| 100 | .setEmoji(getEmojiByName("CONTROL.CROSS", "id")) |
pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame] | 101 | ]; |
pineafan | 02ba023 | 2022-07-24 22:16:15 +0100 | [diff] [blame] | 102 | Object.entries(this.customButtons).forEach(([k, v]) => { |
Skyler Grey | 75ea917 | 2022-08-06 10:22:23 +0100 | [diff] [blame] | 103 | const button = new Discord.MessageButton() |
pineafan | 02ba023 | 2022-07-24 22:16:15 +0100 | [diff] [blame] | 104 | .setCustomId(k) |
| 105 | .setLabel(v.title) |
| 106 | .setStyle(v.active ? "SUCCESS" : "PRIMARY") |
Skyler Grey | 75ea917 | 2022-08-06 10:22:23 +0100 | [diff] [blame] | 107 | .setDisabled(v.disabled); |
Skyler Grey | 11236ba | 2022-08-08 21:13:33 +0100 | [diff] [blame] | 108 | if (v.emoji !== undefined) button.setEmoji(getEmojiByName(v.emoji, "id")); |
Skyler Grey | 75ea917 | 2022-08-06 10:22:23 +0100 | [diff] [blame] | 109 | fullComponents.push(button); |
pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame] | 110 | }); |
Skyler Grey | 75ea917 | 2022-08-06 10:22:23 +0100 | [diff] [blame] | 111 | if (this.reason !== null) |
| 112 | fullComponents.push( |
| 113 | new Discord.MessageButton() |
| 114 | .setCustomId("reason") |
| 115 | .setLabel("Edit Reason") |
| 116 | .setStyle("PRIMARY") |
| 117 | .setEmoji(getEmojiByName("ICONS.EDIT", "id")) |
| 118 | .setDisabled(false) |
| 119 | ); |
pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame] | 120 | const components = []; |
pineafan | 02ba023 | 2022-07-24 22:16:15 +0100 | [diff] [blame] | 121 | for (let i = 0; i < fullComponents.length; i += 5) { |
Skyler Grey | 11236ba | 2022-08-08 21:13:33 +0100 | [diff] [blame] | 122 | components.push(new MessageActionRow().addComponents(fullComponents.slice(i, i + 5))); |
pineafan | 02ba023 | 2022-07-24 22:16:15 +0100 | [diff] [blame] | 123 | } |
pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame] | 124 | const object = { |
pineafan | 377794f | 2022-04-18 19:01:01 +0100 | [diff] [blame] | 125 | embeds: [ |
pineafan | 4edb776 | 2022-06-26 19:21:04 +0100 | [diff] [blame] | 126 | new EmojiEmbed() |
pineafan | 377794f | 2022-04-18 19:01:01 +0100 | [diff] [blame] | 127 | .setEmoji(this.emoji) |
| 128 | .setTitle(this.title) |
Skyler Grey | 75ea917 | 2022-08-06 10:22:23 +0100 | [diff] [blame] | 129 | .setDescription( |
| 130 | this.description + |
| 131 | "\n\n" + |
| 132 | Object.values(this.customButtons) |
| 133 | .map((v) => { |
| 134 | if (v.value === null) return ""; |
| 135 | return v.active ? `*${v.value}*\n` : ""; |
| 136 | }) |
| 137 | .join("") |
| 138 | ) |
pineafan | 377794f | 2022-04-18 19:01:01 +0100 | [diff] [blame] | 139 | .setStatus(this.color) |
pineafan | 377794f | 2022-04-18 19:01:01 +0100 | [diff] [blame] | 140 | ], |
pineafan | 02ba023 | 2022-07-24 22:16:15 +0100 | [diff] [blame] | 141 | components: components, |
pineafan | 377794f | 2022-04-18 19:01:01 +0100 | [diff] [blame] | 142 | ephemeral: true, |
| 143 | fetchReply: true |
pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame] | 144 | }; |
Skyler Grey | 75ea917 | 2022-08-06 10:22:23 +0100 | [diff] [blame] | 145 | let m: Message; |
pineafan | 02ba023 | 2022-07-24 22:16:15 +0100 | [diff] [blame] | 146 | try { |
Skyler Grey | 75ea917 | 2022-08-06 10:22:23 +0100 | [diff] [blame] | 147 | if (editOnly) { |
| 148 | m = (await this.interaction.editReply(object)) as Message; |
pineafan | 02ba023 | 2022-07-24 22:16:15 +0100 | [diff] [blame] | 149 | } else { |
Skyler Grey | 11236ba | 2022-08-08 21:13:33 +0100 | [diff] [blame] | 150 | m = (await this.interaction.reply(object)) as unknown as Message; |
pineafan | 02ba023 | 2022-07-24 22:16:15 +0100 | [diff] [blame] | 151 | } |
Skyler Grey | 75ea917 | 2022-08-06 10:22:23 +0100 | [diff] [blame] | 152 | } catch { |
| 153 | return { cancelled: true }; |
| 154 | } |
pineafan | 377794f | 2022-04-18 19:01:01 +0100 | [diff] [blame] | 155 | let component; |
| 156 | try { |
Skyler Grey | 75ea917 | 2022-08-06 10:22:23 +0100 | [diff] [blame] | 157 | component = await m.awaitMessageComponent({ |
| 158 | filter: (m) => m.user.id === this.interaction.user.id, |
| 159 | time: 300000 |
| 160 | }); |
pineafan | 377794f | 2022-04-18 19:01:01 +0100 | [diff] [blame] | 161 | } catch (e) { |
pineafan | 02ba023 | 2022-07-24 22:16:15 +0100 | [diff] [blame] | 162 | return { success: false, components: this.customButtons }; |
pineafan | 377794f | 2022-04-18 19:01:01 +0100 | [diff] [blame] | 163 | } |
| 164 | if (component.customId === "yes") { |
| 165 | component.deferUpdate(); |
pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame] | 166 | for (const v of Object.values(this.customButtons)) { |
| 167 | if (!v.active) continue; |
Skyler Grey | 75ea917 | 2022-08-06 10:22:23 +0100 | [diff] [blame] | 168 | try { |
| 169 | v.response = await v.onClick(); |
| 170 | } catch (e) { |
| 171 | console.log(e); |
| 172 | } |
pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame] | 173 | } |
pineafan | 02ba023 | 2022-07-24 22:16:15 +0100 | [diff] [blame] | 174 | return { success: true, components: this.customButtons }; |
pineafan | 377794f | 2022-04-18 19:01:01 +0100 | [diff] [blame] | 175 | } else if (component.customId === "no") { |
| 176 | component.deferUpdate(); |
pineafan | 02ba023 | 2022-07-24 22:16:15 +0100 | [diff] [blame] | 177 | return { success: false, components: this.customButtons }; |
pineafan | 73a7c4a | 2022-07-24 10:38:04 +0100 | [diff] [blame] | 178 | } else if (component.customId === "reason") { |
Skyler Grey | 75ea917 | 2022-08-06 10:22:23 +0100 | [diff] [blame] | 179 | await component.showModal( |
| 180 | new Discord.Modal() |
| 181 | .setCustomId("modal") |
| 182 | .setTitle("Editing reason") |
| 183 | .addComponents( |
| 184 | new MessageActionRow<TextInputComponent>().addComponents( |
| 185 | new TextInputComponent() |
| 186 | .setCustomId("reason") |
| 187 | .setLabel("Reason") |
| 188 | .setMaxLength(2000) |
| 189 | .setRequired(false) |
| 190 | .setStyle("PARAGRAPH") |
| 191 | .setPlaceholder("Spammed in #general") |
| 192 | .setValue(this.reason ? this.reason : "") |
| 193 | ) |
| 194 | ) |
| 195 | ); |
pineafan | 73a7c4a | 2022-07-24 10:38:04 +0100 | [diff] [blame] | 196 | await this.interaction.editReply({ |
Skyler Grey | 75ea917 | 2022-08-06 10:22:23 +0100 | [diff] [blame] | 197 | embeds: [ |
| 198 | new EmojiEmbed() |
| 199 | .setTitle(this.title) |
Skyler Grey | 11236ba | 2022-08-08 21:13:33 +0100 | [diff] [blame] | 200 | .setDescription("Modal opened. If you can't see it, click back and try again.") |
Skyler Grey | 75ea917 | 2022-08-06 10:22:23 +0100 | [diff] [blame] | 201 | .setStatus(this.color) |
| 202 | .setEmoji(this.emoji) |
| 203 | ], |
| 204 | components: [ |
| 205 | new MessageActionRow().addComponents([ |
| 206 | new MessageButton() |
| 207 | .setLabel("Back") |
| 208 | .setEmoji(getEmojiByName("CONTROL.LEFT", "id")) |
| 209 | .setStyle("PRIMARY") |
| 210 | .setCustomId("back") |
| 211 | ]) |
| 212 | ] |
pineafan | 73a7c4a | 2022-07-24 10:38:04 +0100 | [diff] [blame] | 213 | }); |
| 214 | let out; |
| 215 | try { |
Skyler Grey | 75ea917 | 2022-08-06 10:22:23 +0100 | [diff] [blame] | 216 | out = await modalInteractionCollector( |
| 217 | m, |
Skyler Grey | 11236ba | 2022-08-08 21:13:33 +0100 | [diff] [blame] | 218 | (m: Interaction) => |
| 219 | (m as MessageComponentInteraction | ModalSubmitInteraction).channelId === |
| 220 | this.interaction.channelId, |
Skyler Grey | 75ea917 | 2022-08-06 10:22:23 +0100 | [diff] [blame] | 221 | (m) => m.customId === "reason" |
| 222 | ); |
| 223 | } catch (e) { |
Skyler Grey | 11236ba | 2022-08-08 21:13:33 +0100 | [diff] [blame] | 224 | return { cancelled: true }; |
Skyler Grey | 75ea917 | 2022-08-06 10:22:23 +0100 | [diff] [blame] | 225 | } |
Skyler Grey | 11236ba | 2022-08-08 21:13:33 +0100 | [diff] [blame] | 226 | if (out === null) { |
| 227 | return { cancelled: true }; |
| 228 | } |
| 229 | if (out instanceof ModalSubmitInteraction) { |
Skyler Grey | 75ea917 | 2022-08-06 10:22:23 +0100 | [diff] [blame] | 230 | return { |
Skyler Grey | 11236ba | 2022-08-08 21:13:33 +0100 | [diff] [blame] | 231 | newReason: out.fields.getTextInputValue("reason") |
Skyler Grey | 75ea917 | 2022-08-06 10:22:23 +0100 | [diff] [blame] | 232 | }; |
| 233 | } else { |
Skyler Grey | 11236ba | 2022-08-08 21:13:33 +0100 | [diff] [blame] | 234 | return { components: this.customButtons }; |
Skyler Grey | 75ea917 | 2022-08-06 10:22:23 +0100 | [diff] [blame] | 235 | } |
pineafan | 02ba023 | 2022-07-24 22:16:15 +0100 | [diff] [blame] | 236 | } else { |
| 237 | component.deferUpdate(); |
Skyler Grey | 11236ba | 2022-08-08 21:13:33 +0100 | [diff] [blame] | 238 | this.customButtons[component.customId]!.active = !this.customButtons[component.customId]!.active; |
pineafan | 02ba023 | 2022-07-24 22:16:15 +0100 | [diff] [blame] | 239 | return { components: this.customButtons }; |
pineafan | 377794f | 2022-04-18 19:01:01 +0100 | [diff] [blame] | 240 | } |
pineafan | 8b4b17f | 2022-02-27 20:42:52 +0000 | [diff] [blame] | 241 | } |
pineafan | 4f164f3 | 2022-02-26 22:07:12 +0000 | [diff] [blame] | 242 | } |
| 243 | } |
| 244 | |
pineafan | 73a7c4a | 2022-07-24 10:38:04 +0100 | [diff] [blame] | 245 | export default confirmationMessage; |