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