pineafan | 6de4da5 | 2023-03-07 20:43:44 +0000 | [diff] [blame] | 1 | import { ButtonInteraction, TextInputBuilder } from "discord.js"; |
Skyler Grey | 75ea917 | 2022-08-06 10:22:23 +0100 | [diff] [blame] | 2 | import Discord, { |
| 3 | CommandInteraction, |
| 4 | Message, |
TheCodedProf | 21c0859 | 2022-09-13 14:14:43 -0400 | [diff] [blame] | 5 | ActionRowBuilder, |
| 6 | ButtonBuilder, |
Skyler Grey | 11236ba | 2022-08-08 21:13:33 +0100 | [diff] [blame] | 7 | ModalSubmitInteraction, |
PineaFan | 100df68 | 2023-01-02 13:26:08 +0000 | [diff] [blame] | 8 | ButtonStyle, |
| 9 | TextInputStyle |
Skyler Grey | 75ea917 | 2022-08-06 10:22:23 +0100 | [diff] [blame] | 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 | a34d04b | 2023-01-03 22:05:42 +0000 | [diff] [blame] | 19 | notValue: string | null; |
pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame] | 20 | emoji: string | undefined; |
pineafan | 02ba023 | 2022-07-24 22:16:15 +0100 | [diff] [blame] | 21 | active: boolean; |
| 22 | onClick: () => Promise<T>; |
| 23 | response: T | null; |
| 24 | } |
| 25 | |
pineafan | 4f164f3 | 2022-02-26 22:07:12 +0000 | [diff] [blame] | 26 | class confirmationMessage { |
pineafan | 6de4da5 | 2023-03-07 20:43:44 +0000 | [diff] [blame] | 27 | interaction: CommandInteraction | ButtonInteraction; |
pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame] | 28 | title = ""; |
| 29 | emoji = ""; |
pineafan | 62ce192 | 2022-08-25 20:34:45 +0100 | [diff] [blame] | 30 | redEmoji: string | null = null; |
PineaFan | 1dee28f | 2023-01-16 22:09:07 +0000 | [diff] [blame] | 31 | failedMessage: string | null = null; |
| 32 | failedEmoji: string | null = null; |
| 33 | failedStatus: "Success" | "Danger" | "Warning" | null = null; |
pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame] | 34 | description = ""; |
Skyler Grey | 75ea917 | 2022-08-06 10:22:23 +0100 | [diff] [blame] | 35 | color: "Danger" | "Warning" | "Success" = "Success"; |
| 36 | customButtons: Record<string, CustomBoolean<unknown>> = {}; |
pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame] | 37 | inverted = false; |
pineafan | 73a7c4a | 2022-07-24 10:38:04 +0100 | [diff] [blame] | 38 | reason: string | null = null; |
pineafan | 4f164f3 | 2022-02-26 22:07:12 +0000 | [diff] [blame] | 39 | |
pineafan | 1e462ab | 2023-03-07 21:34:06 +0000 | [diff] [blame] | 40 | modals: { |
| 41 | buttonText: string; |
| 42 | emoji: string; |
| 43 | customId: string; |
| 44 | modal: Discord.ModalBuilder; |
| 45 | value: string | undefined; |
| 46 | }[] = []; |
pineafan | 6de4da5 | 2023-03-07 20:43:44 +0000 | [diff] [blame] | 47 | |
| 48 | constructor(interaction: CommandInteraction | ButtonInteraction) { |
pineafan | 4f164f3 | 2022-02-26 22:07:12 +0000 | [diff] [blame] | 49 | this.interaction = interaction; |
pineafan | 4f164f3 | 2022-02-26 22:07:12 +0000 | [diff] [blame] | 50 | } |
| 51 | |
Skyler Grey | 75ea917 | 2022-08-06 10:22:23 +0100 | [diff] [blame] | 52 | setTitle(title: string) { |
| 53 | this.title = title; |
| 54 | return this; |
| 55 | } |
PineaFan | 1dee28f | 2023-01-16 22:09:07 +0000 | [diff] [blame] | 56 | setEmoji(emoji: string) { |
Skyler Grey | 75ea917 | 2022-08-06 10:22:23 +0100 | [diff] [blame] | 57 | this.emoji = emoji; |
| 58 | return this; |
| 59 | } |
pineafan | 62ce192 | 2022-08-25 20:34:45 +0100 | [diff] [blame] | 60 | setDescription(description: string, timedOut?: string) { |
Skyler Grey | 75ea917 | 2022-08-06 10:22:23 +0100 | [diff] [blame] | 61 | this.description = description; |
PineaFan | 1dee28f | 2023-01-16 22:09:07 +0000 | [diff] [blame] | 62 | if (timedOut) this.failedMessage = timedOut; |
Skyler Grey | 75ea917 | 2022-08-06 10:22:23 +0100 | [diff] [blame] | 63 | return this; |
| 64 | } |
| 65 | setColor(color: "Danger" | "Warning" | "Success") { |
| 66 | this.color = color; |
| 67 | return this; |
| 68 | } |
| 69 | setInverted(inverted: boolean) { |
| 70 | this.inverted = inverted; |
| 71 | return this; |
| 72 | } |
Skyler Grey | da16adf | 2023-03-05 10:22:12 +0000 | [diff] [blame] | 73 | setFailedMessage( |
| 74 | text: string, |
| 75 | failedStatus: "Success" | "Danger" | "Warning" | null, |
| 76 | failedEmoji: string | null = null |
| 77 | ) { |
PineaFan | 1dee28f | 2023-01-16 22:09:07 +0000 | [diff] [blame] | 78 | this.failedMessage = text; |
| 79 | this.failedStatus = failedStatus; |
| 80 | this.failedEmoji = failedEmoji; |
| 81 | return this; |
| 82 | } |
Skyler Grey | 75ea917 | 2022-08-06 10:22:23 +0100 | [diff] [blame] | 83 | addCustomBoolean( |
| 84 | customId: string, |
| 85 | title: string, |
| 86 | disabled: boolean, |
PineaFan | 100df68 | 2023-01-02 13:26:08 +0000 | [diff] [blame] | 87 | callback: (() => Promise<unknown>) | null = async () => null, |
Skyler Grey | 75ea917 | 2022-08-06 10:22:23 +0100 | [diff] [blame] | 88 | callbackClicked: string | null, |
PineaFan | a34d04b | 2023-01-03 22:05:42 +0000 | [diff] [blame] | 89 | callbackNotClicked: string | null, |
Skyler Grey | 75ea917 | 2022-08-06 10:22:23 +0100 | [diff] [blame] | 90 | emoji?: string, |
| 91 | initial?: boolean |
| 92 | ) { |
| 93 | this.customButtons[customId] = { |
| 94 | title: title, |
| 95 | disabled: disabled, |
| 96 | value: callbackClicked, |
PineaFan | a34d04b | 2023-01-03 22:05:42 +0000 | [diff] [blame] | 97 | notValue: callbackNotClicked, |
Skyler Grey | 75ea917 | 2022-08-06 10:22:23 +0100 | [diff] [blame] | 98 | emoji: emoji, |
| 99 | active: initial ?? false, |
PineaFan | 100df68 | 2023-01-02 13:26:08 +0000 | [diff] [blame] | 100 | onClick: callback ?? (async () => null), |
Skyler Grey | 75ea917 | 2022-08-06 10:22:23 +0100 | [diff] [blame] | 101 | response: null |
| 102 | }; |
| 103 | return this; |
pineafan | 6fb3e07 | 2022-05-20 19:27:23 +0100 | [diff] [blame] | 104 | } |
pineafan | 73a7c4a | 2022-07-24 10:38:04 +0100 | [diff] [blame] | 105 | addReasonButton(reason: string) { |
| 106 | this.reason = reason; |
| 107 | return this; |
| 108 | } |
pineafan | 6de4da5 | 2023-03-07 20:43:44 +0000 | [diff] [blame] | 109 | addModal(buttonText: string, emoji: string, customId: string, current: string, modal: Discord.ModalBuilder) { |
| 110 | modal.setCustomId(customId); |
pineafan | 1e462ab | 2023-03-07 21:34:06 +0000 | [diff] [blame] | 111 | this.modals.push({ buttonText, emoji, customId, modal, value: current }); |
pineafan | 6de4da5 | 2023-03-07 20:43:44 +0000 | [diff] [blame] | 112 | return this; |
| 113 | } |
Skyler Grey | 11236ba | 2022-08-08 21:13:33 +0100 | [diff] [blame] | 114 | async send(editOnly?: boolean): Promise<{ |
| 115 | success?: boolean; |
| 116 | cancelled?: boolean; |
| 117 | components?: Record<string, CustomBoolean<unknown>>; |
| 118 | newReason?: string; |
pineafan | 1e462ab | 2023-03-07 21:34:06 +0000 | [diff] [blame] | 119 | modals?: { |
| 120 | buttonText: string; |
| 121 | emoji: string; |
| 122 | customId: string; |
| 123 | modal: Discord.ModalBuilder; |
| 124 | value: string | undefined; |
| 125 | }[]; |
Skyler Grey | 11236ba | 2022-08-08 21:13:33 +0100 | [diff] [blame] | 126 | }> { |
Skyler Grey | a402d1c | 2022-08-13 23:18:16 +0100 | [diff] [blame] | 127 | let cancelled = false; |
| 128 | let success: boolean | undefined = undefined; |
| 129 | let returnComponents = false; |
| 130 | let newReason = undefined; |
| 131 | |
| 132 | while (!cancelled && success === undefined && !returnComponents && !newReason) { |
pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame] | 133 | const fullComponents = [ |
TheCodedProf | 21c0859 | 2022-09-13 14:14:43 -0400 | [diff] [blame] | 134 | new Discord.ButtonBuilder() |
pineafan | 02ba023 | 2022-07-24 22:16:15 +0100 | [diff] [blame] | 135 | .setCustomId("yes") |
| 136 | .setLabel("Confirm") |
TheCodedProf | 21c0859 | 2022-09-13 14:14:43 -0400 | [diff] [blame] | 137 | .setStyle(this.inverted ? ButtonStyle.Success : ButtonStyle.Danger) |
pineafan | 02ba023 | 2022-07-24 22:16:15 +0100 | [diff] [blame] | 138 | .setEmoji(getEmojiByName("CONTROL.TICK", "id")), |
TheCodedProf | 21c0859 | 2022-09-13 14:14:43 -0400 | [diff] [blame] | 139 | new Discord.ButtonBuilder() |
pineafan | 02ba023 | 2022-07-24 22:16:15 +0100 | [diff] [blame] | 140 | .setCustomId("no") |
| 141 | .setLabel("Cancel") |
TheCodedProf | 21c0859 | 2022-09-13 14:14:43 -0400 | [diff] [blame] | 142 | .setStyle(ButtonStyle.Secondary) |
pineafan | 02ba023 | 2022-07-24 22:16:15 +0100 | [diff] [blame] | 143 | .setEmoji(getEmojiByName("CONTROL.CROSS", "id")) |
pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame] | 144 | ]; |
pineafan | 02ba023 | 2022-07-24 22:16:15 +0100 | [diff] [blame] | 145 | Object.entries(this.customButtons).forEach(([k, v]) => { |
TheCodedProf | 21c0859 | 2022-09-13 14:14:43 -0400 | [diff] [blame] | 146 | const button = new Discord.ButtonBuilder() |
pineafan | 02ba023 | 2022-07-24 22:16:15 +0100 | [diff] [blame] | 147 | .setCustomId(k) |
| 148 | .setLabel(v.title) |
TheCodedProf | 21c0859 | 2022-09-13 14:14:43 -0400 | [diff] [blame] | 149 | .setStyle(v.active ? ButtonStyle.Success : ButtonStyle.Primary) |
Skyler Grey | 75ea917 | 2022-08-06 10:22:23 +0100 | [diff] [blame] | 150 | .setDisabled(v.disabled); |
Skyler Grey | 11236ba | 2022-08-08 21:13:33 +0100 | [diff] [blame] | 151 | if (v.emoji !== undefined) button.setEmoji(getEmojiByName(v.emoji, "id")); |
Skyler Grey | 75ea917 | 2022-08-06 10:22:23 +0100 | [diff] [blame] | 152 | fullComponents.push(button); |
pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame] | 153 | }); |
pineafan | 6de4da5 | 2023-03-07 20:43:44 +0000 | [diff] [blame] | 154 | for (const modal of this.modals) { |
| 155 | fullComponents.push( |
| 156 | new Discord.ButtonBuilder() |
| 157 | .setCustomId(modal.customId) |
| 158 | .setLabel(modal.buttonText) |
| 159 | .setStyle(ButtonStyle.Primary) |
| 160 | .setEmoji(getEmojiByName(modal.emoji, "id")) |
| 161 | .setDisabled(false) |
| 162 | ); |
| 163 | } |
Skyler Grey | 75ea917 | 2022-08-06 10:22:23 +0100 | [diff] [blame] | 164 | if (this.reason !== null) |
| 165 | fullComponents.push( |
TheCodedProf | 21c0859 | 2022-09-13 14:14:43 -0400 | [diff] [blame] | 166 | new Discord.ButtonBuilder() |
Skyler Grey | 75ea917 | 2022-08-06 10:22:23 +0100 | [diff] [blame] | 167 | .setCustomId("reason") |
| 168 | .setLabel("Edit Reason") |
TheCodedProf | 21c0859 | 2022-09-13 14:14:43 -0400 | [diff] [blame] | 169 | .setStyle(ButtonStyle.Primary) |
Skyler Grey | 75ea917 | 2022-08-06 10:22:23 +0100 | [diff] [blame] | 170 | .setEmoji(getEmojiByName("ICONS.EDIT", "id")) |
| 171 | .setDisabled(false) |
| 172 | ); |
pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame] | 173 | const components = []; |
pineafan | 02ba023 | 2022-07-24 22:16:15 +0100 | [diff] [blame] | 174 | for (let i = 0; i < fullComponents.length; i += 5) { |
Skyler Grey | da16adf | 2023-03-05 10:22:12 +0000 | [diff] [blame] | 175 | components.push( |
| 176 | new ActionRowBuilder< |
| 177 | | Discord.ButtonBuilder |
| 178 | | Discord.StringSelectMenuBuilder |
| 179 | | Discord.RoleSelectMenuBuilder |
| 180 | | Discord.UserSelectMenuBuilder |
| 181 | >().addComponents(fullComponents.slice(i, i + 5)) |
| 182 | ); |
pineafan | 02ba023 | 2022-07-24 22:16:15 +0100 | [diff] [blame] | 183 | } |
pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame] | 184 | const object = { |
pineafan | 377794f | 2022-04-18 19:01:01 +0100 | [diff] [blame] | 185 | embeds: [ |
pineafan | 4edb776 | 2022-06-26 19:21:04 +0100 | [diff] [blame] | 186 | new EmojiEmbed() |
pineafan | 377794f | 2022-04-18 19:01:01 +0100 | [diff] [blame] | 187 | .setEmoji(this.emoji) |
| 188 | .setTitle(this.title) |
Skyler Grey | 75ea917 | 2022-08-06 10:22:23 +0100 | [diff] [blame] | 189 | .setDescription( |
| 190 | this.description + |
| 191 | "\n\n" + |
| 192 | Object.values(this.customButtons) |
| 193 | .map((v) => { |
PineaFan | a34d04b | 2023-01-03 22:05:42 +0000 | [diff] [blame] | 194 | if (v.active) { |
| 195 | return v.value ? `*${v.value}*\n` : ""; |
| 196 | } else { |
| 197 | return v.notValue ? `*${v.notValue}*\n` : ""; |
| 198 | } |
Skyler Grey | da16adf | 2023-03-05 10:22:12 +0000 | [diff] [blame] | 199 | }) |
| 200 | .join("") |
Skyler Grey | 75ea917 | 2022-08-06 10:22:23 +0100 | [diff] [blame] | 201 | ) |
pineafan | 377794f | 2022-04-18 19:01:01 +0100 | [diff] [blame] | 202 | .setStatus(this.color) |
pineafan | 377794f | 2022-04-18 19:01:01 +0100 | [diff] [blame] | 203 | ], |
pineafan | 02ba023 | 2022-07-24 22:16:15 +0100 | [diff] [blame] | 204 | components: components, |
pineafan | 377794f | 2022-04-18 19:01:01 +0100 | [diff] [blame] | 205 | ephemeral: true, |
| 206 | fetchReply: true |
pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame] | 207 | }; |
Skyler Grey | 75ea917 | 2022-08-06 10:22:23 +0100 | [diff] [blame] | 208 | let m: Message; |
pineafan | 02ba023 | 2022-07-24 22:16:15 +0100 | [diff] [blame] | 209 | try { |
Skyler Grey | 75ea917 | 2022-08-06 10:22:23 +0100 | [diff] [blame] | 210 | if (editOnly) { |
PineaFan | 100df68 | 2023-01-02 13:26:08 +0000 | [diff] [blame] | 211 | m = (await this.interaction.editReply(object)) as unknown as Message; |
pineafan | 02ba023 | 2022-07-24 22:16:15 +0100 | [diff] [blame] | 212 | } else { |
Skyler Grey | 11236ba | 2022-08-08 21:13:33 +0100 | [diff] [blame] | 213 | m = (await this.interaction.reply(object)) as unknown as Message; |
pineafan | 02ba023 | 2022-07-24 22:16:15 +0100 | [diff] [blame] | 214 | } |
PineaFan | a34d04b | 2023-01-03 22:05:42 +0000 | [diff] [blame] | 215 | } catch (e) { |
Skyler Grey | a402d1c | 2022-08-13 23:18:16 +0100 | [diff] [blame] | 216 | cancelled = true; |
| 217 | continue; |
Skyler Grey | 75ea917 | 2022-08-06 10:22:23 +0100 | [diff] [blame] | 218 | } |
pineafan | 377794f | 2022-04-18 19:01:01 +0100 | [diff] [blame] | 219 | let component; |
| 220 | try { |
Skyler Grey | 75ea917 | 2022-08-06 10:22:23 +0100 | [diff] [blame] | 221 | component = await m.awaitMessageComponent({ |
Skyler Grey | da16adf | 2023-03-05 10:22:12 +0000 | [diff] [blame] | 222 | filter: (i) => |
| 223 | i.user.id === this.interaction.user.id && i.channel!.id === this.interaction.channel!.id, |
Skyler Grey | 75ea917 | 2022-08-06 10:22:23 +0100 | [diff] [blame] | 224 | time: 300000 |
| 225 | }); |
pineafan | 377794f | 2022-04-18 19:01:01 +0100 | [diff] [blame] | 226 | } catch (e) { |
pineafan | 1e462ab | 2023-03-07 21:34:06 +0000 | [diff] [blame] | 227 | success = false; |
TheCodedProf | 1c3ad3c | 2023-01-25 17:58:36 -0500 | [diff] [blame] | 228 | break; |
pineafan | 377794f | 2022-04-18 19:01:01 +0100 | [diff] [blame] | 229 | } |
| 230 | if (component.customId === "yes") { |
| 231 | component.deferUpdate(); |
pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame] | 232 | for (const v of Object.values(this.customButtons)) { |
| 233 | if (!v.active) continue; |
Skyler Grey | 75ea917 | 2022-08-06 10:22:23 +0100 | [diff] [blame] | 234 | try { |
| 235 | v.response = await v.onClick(); |
| 236 | } catch (e) { |
| 237 | console.log(e); |
| 238 | } |
pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame] | 239 | } |
Skyler Grey | a402d1c | 2022-08-13 23:18:16 +0100 | [diff] [blame] | 240 | success = true; |
| 241 | returnComponents = true; |
| 242 | continue; |
pineafan | 377794f | 2022-04-18 19:01:01 +0100 | [diff] [blame] | 243 | } else if (component.customId === "no") { |
| 244 | component.deferUpdate(); |
Skyler Grey | a402d1c | 2022-08-13 23:18:16 +0100 | [diff] [blame] | 245 | success = false; |
| 246 | returnComponents = true; |
| 247 | continue; |
pineafan | 73a7c4a | 2022-07-24 10:38:04 +0100 | [diff] [blame] | 248 | } else if (component.customId === "reason") { |
Skyler Grey | 75ea917 | 2022-08-06 10:22:23 +0100 | [diff] [blame] | 249 | await component.showModal( |
PineaFan | 100df68 | 2023-01-02 13:26:08 +0000 | [diff] [blame] | 250 | new Discord.ModalBuilder() |
Skyler Grey | 75ea917 | 2022-08-06 10:22:23 +0100 | [diff] [blame] | 251 | .setCustomId("modal") |
| 252 | .setTitle("Editing reason") |
| 253 | .addComponents( |
PineaFan | 100df68 | 2023-01-02 13:26:08 +0000 | [diff] [blame] | 254 | new ActionRowBuilder<TextInputBuilder>().addComponents( |
| 255 | new TextInputBuilder() |
Skyler Grey | 75ea917 | 2022-08-06 10:22:23 +0100 | [diff] [blame] | 256 | .setCustomId("reason") |
| 257 | .setLabel("Reason") |
| 258 | .setMaxLength(2000) |
| 259 | .setRequired(false) |
PineaFan | 100df68 | 2023-01-02 13:26:08 +0000 | [diff] [blame] | 260 | .setStyle(TextInputStyle.Paragraph) |
Skyler Grey | 75ea917 | 2022-08-06 10:22:23 +0100 | [diff] [blame] | 261 | .setPlaceholder("Spammed in #general") |
| 262 | .setValue(this.reason ? this.reason : "") |
| 263 | ) |
| 264 | ) |
| 265 | ); |
pineafan | 73a7c4a | 2022-07-24 10:38:04 +0100 | [diff] [blame] | 266 | await this.interaction.editReply({ |
Skyler Grey | 75ea917 | 2022-08-06 10:22:23 +0100 | [diff] [blame] | 267 | embeds: [ |
| 268 | new EmojiEmbed() |
| 269 | .setTitle(this.title) |
Skyler Grey | 11236ba | 2022-08-08 21:13:33 +0100 | [diff] [blame] | 270 | .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] | 271 | .setStatus(this.color) |
| 272 | .setEmoji(this.emoji) |
| 273 | ], |
| 274 | components: [ |
PineaFan | 100df68 | 2023-01-02 13:26:08 +0000 | [diff] [blame] | 275 | new ActionRowBuilder<Discord.ButtonBuilder>().addComponents( |
TheCodedProf | 21c0859 | 2022-09-13 14:14:43 -0400 | [diff] [blame] | 276 | new ButtonBuilder() |
Skyler Grey | 75ea917 | 2022-08-06 10:22:23 +0100 | [diff] [blame] | 277 | .setLabel("Back") |
| 278 | .setEmoji(getEmojiByName("CONTROL.LEFT", "id")) |
TheCodedProf | 21c0859 | 2022-09-13 14:14:43 -0400 | [diff] [blame] | 279 | .setStyle(ButtonStyle.Primary) |
Skyler Grey | 75ea917 | 2022-08-06 10:22:23 +0100 | [diff] [blame] | 280 | .setCustomId("back") |
PineaFan | 100df68 | 2023-01-02 13:26:08 +0000 | [diff] [blame] | 281 | ) |
Skyler Grey | 75ea917 | 2022-08-06 10:22:23 +0100 | [diff] [blame] | 282 | ] |
pineafan | 73a7c4a | 2022-07-24 10:38:04 +0100 | [diff] [blame] | 283 | }); |
| 284 | let out; |
| 285 | try { |
Skyler Grey | da16adf | 2023-03-05 10:22:12 +0000 | [diff] [blame] | 286 | out = (await modalInteractionCollector( |
| 287 | m, |
| 288 | this.interaction.user |
| 289 | )) as Discord.ModalSubmitInteraction | null; |
Skyler Grey | 75ea917 | 2022-08-06 10:22:23 +0100 | [diff] [blame] | 290 | } catch (e) { |
Skyler Grey | a402d1c | 2022-08-13 23:18:16 +0100 | [diff] [blame] | 291 | cancelled = true; |
| 292 | continue; |
Skyler Grey | 75ea917 | 2022-08-06 10:22:23 +0100 | [diff] [blame] | 293 | } |
Skyler Grey | da16adf | 2023-03-05 10:22:12 +0000 | [diff] [blame] | 294 | if (out === null || out.isButton()) { |
Skyler Grey | a402d1c | 2022-08-13 23:18:16 +0100 | [diff] [blame] | 295 | cancelled = true; |
| 296 | continue; |
Skyler Grey | 11236ba | 2022-08-08 21:13:33 +0100 | [diff] [blame] | 297 | } |
| 298 | if (out instanceof ModalSubmitInteraction) { |
Skyler Grey | a402d1c | 2022-08-13 23:18:16 +0100 | [diff] [blame] | 299 | newReason = out.fields.getTextInputValue("reason"); |
| 300 | continue; |
Skyler Grey | 75ea917 | 2022-08-06 10:22:23 +0100 | [diff] [blame] | 301 | } else { |
Skyler Grey | a402d1c | 2022-08-13 23:18:16 +0100 | [diff] [blame] | 302 | returnComponents = true; |
| 303 | continue; |
Skyler Grey | 75ea917 | 2022-08-06 10:22:23 +0100 | [diff] [blame] | 304 | } |
pineafan | 6de4da5 | 2023-03-07 20:43:44 +0000 | [diff] [blame] | 305 | } else if (this.modals.map((m) => m.customId).includes(component.customId)) { |
pineafan | 1e462ab | 2023-03-07 21:34:06 +0000 | [diff] [blame] | 306 | const chosenModal = this.modals.find( |
| 307 | ( |
| 308 | (component) => (m) => |
| 309 | m.customId === component.customId |
| 310 | )(component) |
| 311 | ); |
pineafan | 6de4da5 | 2023-03-07 20:43:44 +0000 | [diff] [blame] | 312 | await component.showModal(chosenModal!.modal); |
| 313 | await this.interaction.editReply({ |
| 314 | embeds: [ |
| 315 | new EmojiEmbed() |
| 316 | .setTitle(this.title) |
| 317 | .setDescription("Modal opened. If you can't see it, click back and try again.") |
| 318 | .setStatus(this.color) |
| 319 | .setEmoji(this.emoji) |
| 320 | ], |
| 321 | components: [ |
| 322 | new ActionRowBuilder<Discord.ButtonBuilder>().addComponents( |
| 323 | new ButtonBuilder() |
| 324 | .setLabel("Back") |
| 325 | .setEmoji(getEmojiByName("CONTROL.LEFT", "id")) |
| 326 | .setStyle(ButtonStyle.Primary) |
| 327 | .setCustomId("back") |
| 328 | ) |
| 329 | ] |
| 330 | }); |
| 331 | let out; |
| 332 | try { |
| 333 | out = (await modalInteractionCollector( |
| 334 | m, |
| 335 | this.interaction.user |
| 336 | )) as Discord.ModalSubmitInteraction | null; |
| 337 | } catch (e) { |
| 338 | console.log(e); |
| 339 | cancelled = true; |
| 340 | continue; |
| 341 | } |
| 342 | if (out === null || out.isButton()) { |
| 343 | continue; |
| 344 | } |
| 345 | if (out instanceof ModalSubmitInteraction) { |
| 346 | chosenModal!.value = out.fields.getTextInputValue("default"); |
| 347 | } |
| 348 | returnComponents = true; |
| 349 | continue; |
pineafan | 02ba023 | 2022-07-24 22:16:15 +0100 | [diff] [blame] | 350 | } else { |
| 351 | component.deferUpdate(); |
Skyler Grey | 11236ba | 2022-08-08 21:13:33 +0100 | [diff] [blame] | 352 | this.customButtons[component.customId]!.active = !this.customButtons[component.customId]!.active; |
Skyler Grey | a402d1c | 2022-08-13 23:18:16 +0100 | [diff] [blame] | 353 | returnComponents = true; |
| 354 | continue; |
pineafan | 377794f | 2022-04-18 19:01:01 +0100 | [diff] [blame] | 355 | } |
pineafan | 8b4b17f | 2022-02-27 20:42:52 +0000 | [diff] [blame] | 356 | } |
Skyler Grey | a402d1c | 2022-08-13 23:18:16 +0100 | [diff] [blame] | 357 | const returnValue: Awaited<ReturnType<typeof this.send>> = {}; |
| 358 | |
pineafan | 62ce192 | 2022-08-25 20:34:45 +0100 | [diff] [blame] | 359 | if (cancelled) { |
Skyler Grey | da16adf | 2023-03-05 10:22:12 +0000 | [diff] [blame] | 360 | await this.timeoutError(); |
pineafan | 62ce192 | 2022-08-25 20:34:45 +0100 | [diff] [blame] | 361 | returnValue.cancelled = true; |
| 362 | } |
TheCodedProf | f86ba09 | 2023-01-27 17:10:07 -0500 | [diff] [blame] | 363 | if (success === false) { |
PineaFan | 1dee28f | 2023-01-16 22:09:07 +0000 | [diff] [blame] | 364 | await this.interaction.editReply({ |
Skyler Grey | da16adf | 2023-03-05 10:22:12 +0000 | [diff] [blame] | 365 | embeds: [ |
| 366 | new EmojiEmbed() |
| 367 | .setTitle(this.title) |
| 368 | .setDescription(this.failedMessage ?? "*Message timed out*") |
| 369 | .setStatus(this.failedStatus ?? "Danger") |
| 370 | .setEmoji(this.failedEmoji ?? this.redEmoji ?? this.emoji) |
| 371 | ], |
| 372 | components: [] |
PineaFan | 1dee28f | 2023-01-16 22:09:07 +0000 | [diff] [blame] | 373 | }); |
pineafan | 6de4da5 | 2023-03-07 20:43:44 +0000 | [diff] [blame] | 374 | return { success: false, cancelled: returnValue.cancelled ?? false }; |
PineaFan | 1dee28f | 2023-01-16 22:09:07 +0000 | [diff] [blame] | 375 | } |
TheCodedProf | 1c3ad3c | 2023-01-25 17:58:36 -0500 | [diff] [blame] | 376 | if (returnComponents || success !== undefined) returnValue.components = this.customButtons; |
| 377 | if (success !== undefined) returnValue.success = success; |
Skyler Grey | a402d1c | 2022-08-13 23:18:16 +0100 | [diff] [blame] | 378 | if (newReason) returnValue.newReason = newReason; |
pineafan | 6de4da5 | 2023-03-07 20:43:44 +0000 | [diff] [blame] | 379 | returnValue.modals = this.modals; |
Skyler Grey | a402d1c | 2022-08-13 23:18:16 +0100 | [diff] [blame] | 380 | |
pineafan | 6de4da5 | 2023-03-07 20:43:44 +0000 | [diff] [blame] | 381 | const modals = this.modals; |
Skyler Grey | da16adf | 2023-03-05 10:22:12 +0000 | [diff] [blame] | 382 | const typedReturnValue = returnValue as |
| 383 | | { cancelled: true } |
pineafan | 1e462ab | 2023-03-07 21:34:06 +0000 | [diff] [blame] | 384 | | { |
| 385 | success: boolean; |
| 386 | components: Record<string, CustomBoolean<unknown>>; |
| 387 | modals: typeof modals; |
| 388 | newReason?: string; |
| 389 | } |
pineafan | 6de4da5 | 2023-03-07 20:43:44 +0000 | [diff] [blame] | 390 | | { newReason: string; components: Record<string, CustomBoolean<unknown>>; modals: typeof modals } |
| 391 | | { components: Record<string, CustomBoolean<unknown>>; modals: typeof modals }; |
PineaFan | 100df68 | 2023-01-02 13:26:08 +0000 | [diff] [blame] | 392 | |
| 393 | return typedReturnValue; |
pineafan | 4f164f3 | 2022-02-26 22:07:12 +0000 | [diff] [blame] | 394 | } |
pineafan | 62ce192 | 2022-08-25 20:34:45 +0100 | [diff] [blame] | 395 | |
| 396 | async timeoutError(): Promise<void> { |
| 397 | await this.interaction.editReply({ |
| 398 | embeds: [ |
| 399 | new EmojiEmbed() |
| 400 | .setTitle(this.title) |
| 401 | .setDescription("We closed this message because it was not used for a while.") |
| 402 | .setStatus("Danger") |
PineaFan | 1dee28f | 2023-01-16 22:09:07 +0000 | [diff] [blame] | 403 | .setEmoji("CONTROL.BLOCKCROSS") |
pineafan | 62ce192 | 2022-08-25 20:34:45 +0100 | [diff] [blame] | 404 | ], |
| 405 | components: [] |
Skyler Grey | da16adf | 2023-03-05 10:22:12 +0000 | [diff] [blame] | 406 | }); |
pineafan | 62ce192 | 2022-08-25 20:34:45 +0100 | [diff] [blame] | 407 | } |
pineafan | 4f164f3 | 2022-02-26 22:07:12 +0000 | [diff] [blame] | 408 | } |
| 409 | |
pineafan | 73a7c4a | 2022-07-24 10:38:04 +0100 | [diff] [blame] | 410 | export default confirmationMessage; |