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