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