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