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 | def38a7 | 2023-05-28 12:47:36 +0100 | [diff] [blame^] | 229 | i.user.id === this.interaction.user.id && (i.channel ? (i.channel!.id === this.interaction.channel!.id) : true), |
Skyler Grey | 75ea917 | 2022-08-06 10:22:23 +0100 | [diff] [blame] | 230 | time: 300000 |
| 231 | }); |
pineafan | 377794f | 2022-04-18 19:01:01 +0100 | [diff] [blame] | 232 | } catch (e) { |
pineafan | 1e462ab | 2023-03-07 21:34:06 +0000 | [diff] [blame] | 233 | success = false; |
TheCodedProf | 1c3ad3c | 2023-01-25 17:58:36 -0500 | [diff] [blame] | 234 | break; |
pineafan | 377794f | 2022-04-18 19:01:01 +0100 | [diff] [blame] | 235 | } |
| 236 | if (component.customId === "yes") { |
Skyler Grey | f4f21c4 | 2023-03-08 14:36:29 +0000 | [diff] [blame] | 237 | await component.deferUpdate(); |
pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame] | 238 | for (const v of Object.values(this.customButtons)) { |
| 239 | if (!v.active) continue; |
Skyler Grey | 75ea917 | 2022-08-06 10:22:23 +0100 | [diff] [blame] | 240 | try { |
| 241 | v.response = await v.onClick(); |
| 242 | } catch (e) { |
| 243 | console.log(e); |
| 244 | } |
pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame] | 245 | } |
Skyler Grey | a402d1c | 2022-08-13 23:18:16 +0100 | [diff] [blame] | 246 | success = true; |
| 247 | returnComponents = true; |
| 248 | continue; |
pineafan | 377794f | 2022-04-18 19:01:01 +0100 | [diff] [blame] | 249 | } else if (component.customId === "no") { |
Skyler Grey | f4f21c4 | 2023-03-08 14:36:29 +0000 | [diff] [blame] | 250 | await component.deferUpdate(); |
Skyler Grey | a402d1c | 2022-08-13 23:18:16 +0100 | [diff] [blame] | 251 | success = false; |
| 252 | returnComponents = true; |
| 253 | continue; |
pineafan | 73a7c4a | 2022-07-24 10:38:04 +0100 | [diff] [blame] | 254 | } else if (component.customId === "reason") { |
Skyler Grey | 75ea917 | 2022-08-06 10:22:23 +0100 | [diff] [blame] | 255 | await component.showModal( |
PineaFan | 100df68 | 2023-01-02 13:26:08 +0000 | [diff] [blame] | 256 | new Discord.ModalBuilder() |
Skyler Grey | 75ea917 | 2022-08-06 10:22:23 +0100 | [diff] [blame] | 257 | .setCustomId("modal") |
| 258 | .setTitle("Editing reason") |
| 259 | .addComponents( |
PineaFan | 100df68 | 2023-01-02 13:26:08 +0000 | [diff] [blame] | 260 | new ActionRowBuilder<TextInputBuilder>().addComponents( |
| 261 | new TextInputBuilder() |
Skyler Grey | 75ea917 | 2022-08-06 10:22:23 +0100 | [diff] [blame] | 262 | .setCustomId("reason") |
| 263 | .setLabel("Reason") |
| 264 | .setMaxLength(2000) |
| 265 | .setRequired(false) |
PineaFan | 100df68 | 2023-01-02 13:26:08 +0000 | [diff] [blame] | 266 | .setStyle(TextInputStyle.Paragraph) |
Skyler Grey | 75ea917 | 2022-08-06 10:22:23 +0100 | [diff] [blame] | 267 | .setPlaceholder("Spammed in #general") |
| 268 | .setValue(this.reason ? this.reason : "") |
| 269 | ) |
| 270 | ) |
| 271 | ); |
pineafan | 73a7c4a | 2022-07-24 10:38:04 +0100 | [diff] [blame] | 272 | await this.interaction.editReply({ |
Skyler Grey | 75ea917 | 2022-08-06 10:22:23 +0100 | [diff] [blame] | 273 | embeds: [ |
| 274 | new EmojiEmbed() |
| 275 | .setTitle(this.title) |
Skyler Grey | 11236ba | 2022-08-08 21:13:33 +0100 | [diff] [blame] | 276 | .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] | 277 | .setStatus(this.color) |
| 278 | .setEmoji(this.emoji) |
| 279 | ], |
| 280 | components: [ |
TheCodedProf | 35e7371 | 2023-03-10 17:35:35 -0500 | [diff] [blame] | 281 | new ActionRowBuilder<ButtonBuilder>().addComponents( |
TheCodedProf | 21c0859 | 2022-09-13 14:14:43 -0400 | [diff] [blame] | 282 | new ButtonBuilder() |
Skyler Grey | 75ea917 | 2022-08-06 10:22:23 +0100 | [diff] [blame] | 283 | .setLabel("Back") |
| 284 | .setEmoji(getEmojiByName("CONTROL.LEFT", "id")) |
TheCodedProf | 21c0859 | 2022-09-13 14:14:43 -0400 | [diff] [blame] | 285 | .setStyle(ButtonStyle.Primary) |
Skyler Grey | 75ea917 | 2022-08-06 10:22:23 +0100 | [diff] [blame] | 286 | .setCustomId("back") |
PineaFan | 100df68 | 2023-01-02 13:26:08 +0000 | [diff] [blame] | 287 | ) |
Skyler Grey | 75ea917 | 2022-08-06 10:22:23 +0100 | [diff] [blame] | 288 | ] |
pineafan | 73a7c4a | 2022-07-24 10:38:04 +0100 | [diff] [blame] | 289 | }); |
| 290 | let out; |
| 291 | try { |
Skyler Grey | da16adf | 2023-03-05 10:22:12 +0000 | [diff] [blame] | 292 | out = (await modalInteractionCollector( |
| 293 | m, |
| 294 | this.interaction.user |
| 295 | )) as Discord.ModalSubmitInteraction | null; |
Skyler Grey | 75ea917 | 2022-08-06 10:22:23 +0100 | [diff] [blame] | 296 | } catch (e) { |
Skyler Grey | a402d1c | 2022-08-13 23:18:16 +0100 | [diff] [blame] | 297 | cancelled = true; |
| 298 | continue; |
Skyler Grey | 75ea917 | 2022-08-06 10:22:23 +0100 | [diff] [blame] | 299 | } |
Skyler Grey | 0d88522 | 2023-03-08 21:46:37 +0000 | [diff] [blame] | 300 | if (out === null) { |
Skyler Grey | a402d1c | 2022-08-13 23:18:16 +0100 | [diff] [blame] | 301 | cancelled = true; |
| 302 | continue; |
Skyler Grey | 11236ba | 2022-08-08 21:13:33 +0100 | [diff] [blame] | 303 | } |
Skyler Grey | 0d88522 | 2023-03-08 21:46:37 +0000 | [diff] [blame] | 304 | if (out.isButton()) { |
| 305 | continue; |
| 306 | } |
Skyler Grey | 11236ba | 2022-08-08 21:13:33 +0100 | [diff] [blame] | 307 | if (out instanceof ModalSubmitInteraction) { |
Skyler Grey | a402d1c | 2022-08-13 23:18:16 +0100 | [diff] [blame] | 308 | newReason = out.fields.getTextInputValue("reason"); |
| 309 | continue; |
Skyler Grey | 75ea917 | 2022-08-06 10:22:23 +0100 | [diff] [blame] | 310 | } else { |
Skyler Grey | a402d1c | 2022-08-13 23:18:16 +0100 | [diff] [blame] | 311 | returnComponents = true; |
| 312 | continue; |
Skyler Grey | 75ea917 | 2022-08-06 10:22:23 +0100 | [diff] [blame] | 313 | } |
pineafan | 6de4da5 | 2023-03-07 20:43:44 +0000 | [diff] [blame] | 314 | } else if (this.modals.map((m) => m.customId).includes(component.customId)) { |
pineafan | 1e462ab | 2023-03-07 21:34:06 +0000 | [diff] [blame] | 315 | const chosenModal = this.modals.find( |
| 316 | ( |
| 317 | (component) => (m) => |
| 318 | m.customId === component.customId |
| 319 | )(component) |
| 320 | ); |
pineafan | 6de4da5 | 2023-03-07 20:43:44 +0000 | [diff] [blame] | 321 | await component.showModal(chosenModal!.modal); |
| 322 | await this.interaction.editReply({ |
| 323 | embeds: [ |
| 324 | new EmojiEmbed() |
| 325 | .setTitle(this.title) |
| 326 | .setDescription("Modal opened. If you can't see it, click back and try again.") |
| 327 | .setStatus(this.color) |
| 328 | .setEmoji(this.emoji) |
| 329 | ], |
| 330 | components: [ |
TheCodedProf | 35e7371 | 2023-03-10 17:35:35 -0500 | [diff] [blame] | 331 | new ActionRowBuilder<ButtonBuilder>().addComponents( |
pineafan | 6de4da5 | 2023-03-07 20:43:44 +0000 | [diff] [blame] | 332 | new ButtonBuilder() |
| 333 | .setLabel("Back") |
| 334 | .setEmoji(getEmojiByName("CONTROL.LEFT", "id")) |
| 335 | .setStyle(ButtonStyle.Primary) |
| 336 | .setCustomId("back") |
| 337 | ) |
| 338 | ] |
| 339 | }); |
| 340 | let out; |
| 341 | try { |
| 342 | out = (await modalInteractionCollector( |
| 343 | m, |
| 344 | this.interaction.user |
| 345 | )) as Discord.ModalSubmitInteraction | null; |
| 346 | } catch (e) { |
| 347 | console.log(e); |
| 348 | cancelled = true; |
| 349 | continue; |
| 350 | } |
Skyler Grey | 0d88522 | 2023-03-08 21:46:37 +0000 | [diff] [blame] | 351 | if (out === null) { |
| 352 | cancelled = true; |
| 353 | continue; |
| 354 | } |
| 355 | if (out.isButton()) { |
pineafan | 6de4da5 | 2023-03-07 20:43:44 +0000 | [diff] [blame] | 356 | continue; |
| 357 | } |
| 358 | if (out instanceof ModalSubmitInteraction) { |
TheCodedProf | 35e7371 | 2023-03-10 17:35:35 -0500 | [diff] [blame] | 359 | out.fields.fields.forEach((f, k) => { |
| 360 | chosenModal!.values[k] = f.value; |
| 361 | }); |
pineafan | 6de4da5 | 2023-03-07 20:43:44 +0000 | [diff] [blame] | 362 | } |
| 363 | returnComponents = true; |
| 364 | continue; |
pineafan | 02ba023 | 2022-07-24 22:16:15 +0100 | [diff] [blame] | 365 | } else { |
Skyler Grey | f4f21c4 | 2023-03-08 14:36:29 +0000 | [diff] [blame] | 366 | await component.deferUpdate(); |
Skyler Grey | 11236ba | 2022-08-08 21:13:33 +0100 | [diff] [blame] | 367 | this.customButtons[component.customId]!.active = !this.customButtons[component.customId]!.active; |
Skyler Grey | a402d1c | 2022-08-13 23:18:16 +0100 | [diff] [blame] | 368 | returnComponents = true; |
| 369 | continue; |
pineafan | 377794f | 2022-04-18 19:01:01 +0100 | [diff] [blame] | 370 | } |
pineafan | 8b4b17f | 2022-02-27 20:42:52 +0000 | [diff] [blame] | 371 | } |
Skyler Grey | a402d1c | 2022-08-13 23:18:16 +0100 | [diff] [blame] | 372 | const returnValue: Awaited<ReturnType<typeof this.send>> = {}; |
| 373 | |
pineafan | 62ce192 | 2022-08-25 20:34:45 +0100 | [diff] [blame] | 374 | if (cancelled) { |
Skyler Grey | da16adf | 2023-03-05 10:22:12 +0000 | [diff] [blame] | 375 | await this.timeoutError(); |
pineafan | 62ce192 | 2022-08-25 20:34:45 +0100 | [diff] [blame] | 376 | returnValue.cancelled = true; |
| 377 | } |
TheCodedProf | f86ba09 | 2023-01-27 17:10:07 -0500 | [diff] [blame] | 378 | if (success === false) { |
PineaFan | 1dee28f | 2023-01-16 22:09:07 +0000 | [diff] [blame] | 379 | await this.interaction.editReply({ |
Skyler Grey | da16adf | 2023-03-05 10:22:12 +0000 | [diff] [blame] | 380 | embeds: [ |
| 381 | new EmojiEmbed() |
| 382 | .setTitle(this.title) |
| 383 | .setDescription(this.failedMessage ?? "*Message timed out*") |
| 384 | .setStatus(this.failedStatus ?? "Danger") |
| 385 | .setEmoji(this.failedEmoji ?? this.redEmoji ?? this.emoji) |
| 386 | ], |
| 387 | components: [] |
PineaFan | 1dee28f | 2023-01-16 22:09:07 +0000 | [diff] [blame] | 388 | }); |
pineafan | 6de4da5 | 2023-03-07 20:43:44 +0000 | [diff] [blame] | 389 | return { success: false, cancelled: returnValue.cancelled ?? false }; |
PineaFan | 1dee28f | 2023-01-16 22:09:07 +0000 | [diff] [blame] | 390 | } |
TheCodedProf | 1c3ad3c | 2023-01-25 17:58:36 -0500 | [diff] [blame] | 391 | if (returnComponents || success !== undefined) returnValue.components = this.customButtons; |
| 392 | if (success !== undefined) returnValue.success = success; |
Skyler Grey | a402d1c | 2022-08-13 23:18:16 +0100 | [diff] [blame] | 393 | if (newReason) returnValue.newReason = newReason; |
pineafan | 6de4da5 | 2023-03-07 20:43:44 +0000 | [diff] [blame] | 394 | returnValue.modals = this.modals; |
Skyler Grey | a402d1c | 2022-08-13 23:18:16 +0100 | [diff] [blame] | 395 | |
pineafan | 6de4da5 | 2023-03-07 20:43:44 +0000 | [diff] [blame] | 396 | const modals = this.modals; |
Skyler Grey | da16adf | 2023-03-05 10:22:12 +0000 | [diff] [blame] | 397 | const typedReturnValue = returnValue as |
| 398 | | { cancelled: true } |
pineafan | 1e462ab | 2023-03-07 21:34:06 +0000 | [diff] [blame] | 399 | | { |
| 400 | success: boolean; |
| 401 | components: Record<string, CustomBoolean<unknown>>; |
| 402 | modals: typeof modals; |
| 403 | newReason?: string; |
| 404 | } |
pineafan | 6de4da5 | 2023-03-07 20:43:44 +0000 | [diff] [blame] | 405 | | { newReason: string; components: Record<string, CustomBoolean<unknown>>; modals: typeof modals } |
| 406 | | { components: Record<string, CustomBoolean<unknown>>; modals: typeof modals }; |
PineaFan | 100df68 | 2023-01-02 13:26:08 +0000 | [diff] [blame] | 407 | |
| 408 | return typedReturnValue; |
pineafan | 4f164f3 | 2022-02-26 22:07:12 +0000 | [diff] [blame] | 409 | } |
pineafan | 62ce192 | 2022-08-25 20:34:45 +0100 | [diff] [blame] | 410 | |
| 411 | async timeoutError(): Promise<void> { |
| 412 | await this.interaction.editReply({ |
| 413 | embeds: [ |
| 414 | new EmojiEmbed() |
| 415 | .setTitle(this.title) |
| 416 | .setDescription("We closed this message because it was not used for a while.") |
| 417 | .setStatus("Danger") |
PineaFan | 1dee28f | 2023-01-16 22:09:07 +0000 | [diff] [blame] | 418 | .setEmoji("CONTROL.BLOCKCROSS") |
pineafan | 62ce192 | 2022-08-25 20:34:45 +0100 | [diff] [blame] | 419 | ], |
| 420 | components: [] |
Skyler Grey | da16adf | 2023-03-05 10:22:12 +0000 | [diff] [blame] | 421 | }); |
pineafan | 62ce192 | 2022-08-25 20:34:45 +0100 | [diff] [blame] | 422 | } |
pineafan | 4f164f3 | 2022-02-26 22:07:12 +0000 | [diff] [blame] | 423 | } |
| 424 | |
pineafan | 73a7c4a | 2022-07-24 10:38:04 +0100 | [diff] [blame] | 425 | export default confirmationMessage; |