Skyler Grey | 75ea917 | 2022-08-06 10:22:23 +0100 | [diff] [blame^] | 1 | import Discord, { |
| 2 | CommandInteraction, |
| 3 | Message, |
| 4 | MessageActionRow, |
| 5 | MessageButton, |
| 6 | TextInputComponent |
| 7 | } from "discord.js"; |
pineafan | 73a7c4a | 2022-07-24 10:38:04 +0100 | [diff] [blame] | 8 | import { modalInteractionCollector } from "./dualCollector.js"; |
pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame] | 9 | import EmojiEmbed from "./generateEmojiEmbed.js"; |
pineafan | 8b4b17f | 2022-02-27 20:42:52 +0000 | [diff] [blame] | 10 | import getEmojiByName from "./getEmojiByName.js"; |
pineafan | 4f164f3 | 2022-02-26 22:07:12 +0000 | [diff] [blame] | 11 | |
pineafan | 02ba023 | 2022-07-24 22:16:15 +0100 | [diff] [blame] | 12 | interface CustomBoolean<T> { |
| 13 | title: string; |
| 14 | disabled: boolean; |
| 15 | value: string | null; |
pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame] | 16 | emoji: string | undefined; |
pineafan | 02ba023 | 2022-07-24 22:16:15 +0100 | [diff] [blame] | 17 | active: boolean; |
| 18 | onClick: () => Promise<T>; |
| 19 | response: T | null; |
| 20 | } |
| 21 | |
pineafan | 4f164f3 | 2022-02-26 22:07:12 +0000 | [diff] [blame] | 22 | class confirmationMessage { |
| 23 | interaction: CommandInteraction; |
pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame] | 24 | title = ""; |
| 25 | emoji = ""; |
| 26 | description = ""; |
Skyler Grey | 75ea917 | 2022-08-06 10:22:23 +0100 | [diff] [blame^] | 27 | color: "Danger" | "Warning" | "Success" = "Success"; |
| 28 | customButtons: Record<string, CustomBoolean<unknown>> = {}; |
pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame] | 29 | inverted = false; |
pineafan | 73a7c4a | 2022-07-24 10:38:04 +0100 | [diff] [blame] | 30 | reason: string | null = null; |
pineafan | 4f164f3 | 2022-02-26 22:07:12 +0000 | [diff] [blame] | 31 | |
| 32 | constructor(interaction: CommandInteraction) { |
| 33 | this.interaction = interaction; |
pineafan | 4f164f3 | 2022-02-26 22:07:12 +0000 | [diff] [blame] | 34 | } |
| 35 | |
Skyler Grey | 75ea917 | 2022-08-06 10:22:23 +0100 | [diff] [blame^] | 36 | setTitle(title: string) { |
| 37 | this.title = title; |
| 38 | return this; |
| 39 | } |
| 40 | setEmoji(emoji: string) { |
| 41 | this.emoji = emoji; |
| 42 | return this; |
| 43 | } |
| 44 | setDescription(description: string) { |
| 45 | this.description = description; |
| 46 | return this; |
| 47 | } |
| 48 | setColor(color: "Danger" | "Warning" | "Success") { |
| 49 | this.color = color; |
| 50 | return this; |
| 51 | } |
| 52 | setInverted(inverted: boolean) { |
| 53 | this.inverted = inverted; |
| 54 | return this; |
| 55 | } |
| 56 | addCustomBoolean( |
| 57 | customId: string, |
| 58 | title: string, |
| 59 | disabled: boolean, |
| 60 | callback: () => Promise<unknown> = async () => null, |
| 61 | callbackClicked: string | null, |
| 62 | emoji?: string, |
| 63 | initial?: boolean |
| 64 | ) { |
| 65 | this.customButtons[customId] = { |
| 66 | title: title, |
| 67 | disabled: disabled, |
| 68 | value: callbackClicked, |
| 69 | emoji: emoji, |
| 70 | active: initial ?? false, |
| 71 | onClick: callback, |
| 72 | response: null |
| 73 | }; |
| 74 | return this; |
pineafan | 6fb3e07 | 2022-05-20 19:27:23 +0100 | [diff] [blame] | 75 | } |
pineafan | 73a7c4a | 2022-07-24 10:38:04 +0100 | [diff] [blame] | 76 | addReasonButton(reason: string) { |
| 77 | this.reason = reason; |
| 78 | return this; |
| 79 | } |
pineafan | 8b4b17f | 2022-02-27 20:42:52 +0000 | [diff] [blame] | 80 | async send(editOnly?: boolean) { |
pineafan | 377794f | 2022-04-18 19:01:01 +0100 | [diff] [blame] | 81 | while (true) { |
pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame] | 82 | const fullComponents = [ |
pineafan | 02ba023 | 2022-07-24 22:16:15 +0100 | [diff] [blame] | 83 | new Discord.MessageButton() |
| 84 | .setCustomId("yes") |
| 85 | .setLabel("Confirm") |
| 86 | .setStyle(this.inverted ? "SUCCESS" : "DANGER") |
| 87 | .setEmoji(getEmojiByName("CONTROL.TICK", "id")), |
| 88 | new Discord.MessageButton() |
| 89 | .setCustomId("no") |
| 90 | .setLabel("Cancel") |
| 91 | .setStyle("SECONDARY") |
| 92 | .setEmoji(getEmojiByName("CONTROL.CROSS", "id")) |
pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame] | 93 | ]; |
pineafan | 02ba023 | 2022-07-24 22:16:15 +0100 | [diff] [blame] | 94 | Object.entries(this.customButtons).forEach(([k, v]) => { |
Skyler Grey | 75ea917 | 2022-08-06 10:22:23 +0100 | [diff] [blame^] | 95 | const button = new Discord.MessageButton() |
pineafan | 02ba023 | 2022-07-24 22:16:15 +0100 | [diff] [blame] | 96 | .setCustomId(k) |
| 97 | .setLabel(v.title) |
| 98 | .setStyle(v.active ? "SUCCESS" : "PRIMARY") |
Skyler Grey | 75ea917 | 2022-08-06 10:22:23 +0100 | [diff] [blame^] | 99 | .setDisabled(v.disabled); |
| 100 | if (v.emoji !== undefined) |
| 101 | button.setEmoji(getEmojiByName(v.emoji, "id")); |
| 102 | fullComponents.push(button); |
pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame] | 103 | }); |
Skyler Grey | 75ea917 | 2022-08-06 10:22:23 +0100 | [diff] [blame^] | 104 | if (this.reason !== null) |
| 105 | fullComponents.push( |
| 106 | new Discord.MessageButton() |
| 107 | .setCustomId("reason") |
| 108 | .setLabel("Edit Reason") |
| 109 | .setStyle("PRIMARY") |
| 110 | .setEmoji(getEmojiByName("ICONS.EDIT", "id")) |
| 111 | .setDisabled(false) |
| 112 | ); |
pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame] | 113 | const components = []; |
pineafan | 02ba023 | 2022-07-24 22:16:15 +0100 | [diff] [blame] | 114 | for (let i = 0; i < fullComponents.length; i += 5) { |
Skyler Grey | 75ea917 | 2022-08-06 10:22:23 +0100 | [diff] [blame^] | 115 | components.push( |
| 116 | new MessageActionRow().addComponents( |
| 117 | fullComponents.slice(i, i + 5) |
| 118 | ) |
| 119 | ); |
pineafan | 02ba023 | 2022-07-24 22:16:15 +0100 | [diff] [blame] | 120 | } |
pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame] | 121 | const object = { |
pineafan | 377794f | 2022-04-18 19:01:01 +0100 | [diff] [blame] | 122 | embeds: [ |
pineafan | 4edb776 | 2022-06-26 19:21:04 +0100 | [diff] [blame] | 123 | new EmojiEmbed() |
pineafan | 377794f | 2022-04-18 19:01:01 +0100 | [diff] [blame] | 124 | .setEmoji(this.emoji) |
| 125 | .setTitle(this.title) |
Skyler Grey | 75ea917 | 2022-08-06 10:22:23 +0100 | [diff] [blame^] | 126 | .setDescription( |
| 127 | this.description + |
| 128 | "\n\n" + |
| 129 | Object.values(this.customButtons) |
| 130 | .map((v) => { |
| 131 | if (v.value === null) return ""; |
| 132 | return v.active ? `*${v.value}*\n` : ""; |
| 133 | }) |
| 134 | .join("") |
| 135 | ) |
pineafan | 377794f | 2022-04-18 19:01:01 +0100 | [diff] [blame] | 136 | .setStatus(this.color) |
pineafan | 377794f | 2022-04-18 19:01:01 +0100 | [diff] [blame] | 137 | ], |
pineafan | 02ba023 | 2022-07-24 22:16:15 +0100 | [diff] [blame] | 138 | components: components, |
pineafan | 377794f | 2022-04-18 19:01:01 +0100 | [diff] [blame] | 139 | ephemeral: true, |
| 140 | fetchReply: true |
pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame] | 141 | }; |
Skyler Grey | 75ea917 | 2022-08-06 10:22:23 +0100 | [diff] [blame^] | 142 | let m: Message; |
pineafan | 02ba023 | 2022-07-24 22:16:15 +0100 | [diff] [blame] | 143 | try { |
Skyler Grey | 75ea917 | 2022-08-06 10:22:23 +0100 | [diff] [blame^] | 144 | if (editOnly) { |
| 145 | m = (await this.interaction.editReply(object)) as Message; |
pineafan | 02ba023 | 2022-07-24 22:16:15 +0100 | [diff] [blame] | 146 | } else { |
Skyler Grey | 75ea917 | 2022-08-06 10:22:23 +0100 | [diff] [blame^] | 147 | m = (await this.interaction.reply( |
| 148 | object |
| 149 | )) as unknown as Message; |
pineafan | 02ba023 | 2022-07-24 22:16:15 +0100 | [diff] [blame] | 150 | } |
Skyler Grey | 75ea917 | 2022-08-06 10:22:23 +0100 | [diff] [blame^] | 151 | } catch { |
| 152 | return { cancelled: true }; |
| 153 | } |
pineafan | 377794f | 2022-04-18 19:01:01 +0100 | [diff] [blame] | 154 | let component; |
| 155 | try { |
Skyler Grey | 75ea917 | 2022-08-06 10:22:23 +0100 | [diff] [blame^] | 156 | component = await m.awaitMessageComponent({ |
| 157 | filter: (m) => m.user.id === this.interaction.user.id, |
| 158 | time: 300000 |
| 159 | }); |
pineafan | 377794f | 2022-04-18 19:01:01 +0100 | [diff] [blame] | 160 | } catch (e) { |
pineafan | 02ba023 | 2022-07-24 22:16:15 +0100 | [diff] [blame] | 161 | return { success: false, components: this.customButtons }; |
pineafan | 377794f | 2022-04-18 19:01:01 +0100 | [diff] [blame] | 162 | } |
| 163 | if (component.customId === "yes") { |
| 164 | component.deferUpdate(); |
pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame] | 165 | for (const v of Object.values(this.customButtons)) { |
| 166 | if (!v.active) continue; |
Skyler Grey | 75ea917 | 2022-08-06 10:22:23 +0100 | [diff] [blame^] | 167 | try { |
| 168 | v.response = await v.onClick(); |
| 169 | } catch (e) { |
| 170 | console.log(e); |
| 171 | } |
pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame] | 172 | } |
pineafan | 02ba023 | 2022-07-24 22:16:15 +0100 | [diff] [blame] | 173 | return { success: true, components: this.customButtons }; |
pineafan | 377794f | 2022-04-18 19:01:01 +0100 | [diff] [blame] | 174 | } else if (component.customId === "no") { |
| 175 | component.deferUpdate(); |
pineafan | 02ba023 | 2022-07-24 22:16:15 +0100 | [diff] [blame] | 176 | return { success: false, components: this.customButtons }; |
pineafan | 73a7c4a | 2022-07-24 10:38:04 +0100 | [diff] [blame] | 177 | } else if (component.customId === "reason") { |
Skyler Grey | 75ea917 | 2022-08-06 10:22:23 +0100 | [diff] [blame^] | 178 | await component.showModal( |
| 179 | new Discord.Modal() |
| 180 | .setCustomId("modal") |
| 181 | .setTitle("Editing reason") |
| 182 | .addComponents( |
| 183 | new MessageActionRow<TextInputComponent>().addComponents( |
| 184 | new TextInputComponent() |
| 185 | .setCustomId("reason") |
| 186 | .setLabel("Reason") |
| 187 | .setMaxLength(2000) |
| 188 | .setRequired(false) |
| 189 | .setStyle("PARAGRAPH") |
| 190 | .setPlaceholder("Spammed in #general") |
| 191 | .setValue(this.reason ? this.reason : "") |
| 192 | ) |
| 193 | ) |
| 194 | ); |
pineafan | 73a7c4a | 2022-07-24 10:38:04 +0100 | [diff] [blame] | 195 | await this.interaction.editReply({ |
Skyler Grey | 75ea917 | 2022-08-06 10:22:23 +0100 | [diff] [blame^] | 196 | embeds: [ |
| 197 | new EmojiEmbed() |
| 198 | .setTitle(this.title) |
| 199 | .setDescription( |
| 200 | "Modal opened. If you can't see it, click back and try again." |
| 201 | ) |
| 202 | .setStatus(this.color) |
| 203 | .setEmoji(this.emoji) |
| 204 | ], |
| 205 | components: [ |
| 206 | new MessageActionRow().addComponents([ |
| 207 | new MessageButton() |
| 208 | .setLabel("Back") |
| 209 | .setEmoji(getEmojiByName("CONTROL.LEFT", "id")) |
| 210 | .setStyle("PRIMARY") |
| 211 | .setCustomId("back") |
| 212 | ]) |
| 213 | ] |
pineafan | 73a7c4a | 2022-07-24 10:38:04 +0100 | [diff] [blame] | 214 | }); |
| 215 | let out; |
| 216 | try { |
Skyler Grey | 75ea917 | 2022-08-06 10:22:23 +0100 | [diff] [blame^] | 217 | out = await modalInteractionCollector( |
| 218 | m, |
| 219 | (m) => m.channel.id === this.interaction.channel.id, |
| 220 | (m) => m.customId === "reason" |
| 221 | ); |
| 222 | } catch (e) { |
| 223 | return {}; |
| 224 | } |
| 225 | if (out.fields) { |
| 226 | return { |
| 227 | newReason: out.fields.getTextInputValue("reason") ?? "" |
| 228 | }; |
| 229 | } else { |
| 230 | return { newReason: this.reason }; |
| 231 | } |
pineafan | 02ba023 | 2022-07-24 22:16:15 +0100 | [diff] [blame] | 232 | } else { |
| 233 | component.deferUpdate(); |
Skyler Grey | 75ea917 | 2022-08-06 10:22:23 +0100 | [diff] [blame^] | 234 | this.customButtons[component.customId].active = |
| 235 | !this.customButtons[component.customId].active; |
pineafan | 02ba023 | 2022-07-24 22:16:15 +0100 | [diff] [blame] | 236 | return { components: this.customButtons }; |
pineafan | 377794f | 2022-04-18 19:01:01 +0100 | [diff] [blame] | 237 | } |
pineafan | 8b4b17f | 2022-02-27 20:42:52 +0000 | [diff] [blame] | 238 | } |
pineafan | 4f164f3 | 2022-02-26 22:07:12 +0000 | [diff] [blame] | 239 | } |
| 240 | } |
| 241 | |
pineafan | 73a7c4a | 2022-07-24 10:38:04 +0100 | [diff] [blame] | 242 | export default confirmationMessage; |