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 | 4edb776 | 2022-06-26 19:21:04 +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 | |
| 6 | class confirmationMessage { |
| 7 | interaction: CommandInteraction; |
pineafan | 73a7c4a | 2022-07-24 10:38:04 +0100 | [diff] [blame^] | 8 | title: string = ""; |
| 9 | emoji: string = ""; |
| 10 | description: string = ""; |
| 11 | color: string = ""; |
| 12 | customCallback: () => any = () => {}; |
pineafan | 377794f | 2022-04-18 19:01:01 +0100 | [diff] [blame] | 13 | customButtonTitle: string; |
| 14 | customButtonDisabled: boolean; |
| 15 | customCallbackString: string = ""; |
| 16 | customCallbackClicked: boolean = false; |
| 17 | customCallbackResponse: any = null; |
pineafan | 73a7c4a | 2022-07-24 10:38:04 +0100 | [diff] [blame^] | 18 | customBoolean: () => any = () => {}; // allow multiple booleans |
pineafan | 6fb3e07 | 2022-05-20 19:27:23 +0100 | [diff] [blame] | 19 | customBooleanClicked: boolean = null; |
pineafan | 73a7c4a | 2022-07-24 10:38:04 +0100 | [diff] [blame^] | 20 | inverted: boolean = false; |
| 21 | reason: string | null = null; |
pineafan | 4f164f3 | 2022-02-26 22:07:12 +0000 | [diff] [blame] | 22 | |
| 23 | constructor(interaction: CommandInteraction) { |
| 24 | this.interaction = interaction; |
pineafan | 4f164f3 | 2022-02-26 22:07:12 +0000 | [diff] [blame] | 25 | } |
| 26 | |
| 27 | setTitle(title: string) { this.title = title; return this } |
| 28 | setEmoji(emoji: string) { this.emoji = emoji; return this } |
| 29 | setDescription(description: string) { this.description = description; return this } |
| 30 | setColor(color: string) { this.color = color; return this } |
pineafan | 167bde3 | 2022-05-19 19:33:46 +0100 | [diff] [blame] | 31 | setInverted(inverted: boolean) { this.inverted = inverted; return this } |
pineafan | 377794f | 2022-04-18 19:01:01 +0100 | [diff] [blame] | 32 | addCustomCallback(title: string, disabled: boolean, callback: () => any, callbackClicked: string) { |
pineafan | 6fb3e07 | 2022-05-20 19:27:23 +0100 | [diff] [blame] | 33 | if (this.customButtonTitle) return this |
pineafan | 377794f | 2022-04-18 19:01:01 +0100 | [diff] [blame] | 34 | this.customButtonTitle = title; |
| 35 | this.customButtonDisabled = disabled; |
| 36 | this.customCallback = callback; |
| 37 | this.customCallbackString = callbackClicked; |
| 38 | return this; |
| 39 | } |
pineafan | 6fb3e07 | 2022-05-20 19:27:23 +0100 | [diff] [blame] | 40 | addCustomBoolean(title: string, disabled: boolean, callback: () => any, callbackClicked: string) { |
| 41 | if (this.customButtonTitle) return this |
| 42 | this.customButtonTitle = title; |
| 43 | this.customButtonDisabled = disabled; |
| 44 | this.customBoolean = callback; |
| 45 | this.customCallbackString = callbackClicked; |
| 46 | this.customBooleanClicked = false; |
| 47 | return this; |
| 48 | } |
pineafan | 73a7c4a | 2022-07-24 10:38:04 +0100 | [diff] [blame^] | 49 | addReasonButton(reason: string) { |
| 50 | this.reason = reason; |
| 51 | return this; |
| 52 | } |
pineafan | 8b4b17f | 2022-02-27 20:42:52 +0000 | [diff] [blame] | 53 | async send(editOnly?: boolean) { |
pineafan | 377794f | 2022-04-18 19:01:01 +0100 | [diff] [blame] | 54 | while (true) { |
| 55 | let object = { |
| 56 | embeds: [ |
pineafan | 4edb776 | 2022-06-26 19:21:04 +0100 | [diff] [blame] | 57 | new EmojiEmbed() |
pineafan | 377794f | 2022-04-18 19:01:01 +0100 | [diff] [blame] | 58 | .setEmoji(this.emoji) |
| 59 | .setTitle(this.title) |
| 60 | .setDescription(this.description) |
| 61 | .setStatus(this.color) |
pineafan | 6fb3e07 | 2022-05-20 19:27:23 +0100 | [diff] [blame] | 62 | .setFooter({text: (this.customBooleanClicked ?? this.customCallbackClicked) ? this.customCallbackString : ""}) |
pineafan | 377794f | 2022-04-18 19:01:01 +0100 | [diff] [blame] | 63 | ], |
| 64 | components: [ |
| 65 | new MessageActionRow().addComponents([ |
| 66 | new Discord.MessageButton() |
| 67 | .setCustomId("yes") |
PineappleFan | 19b002b | 2022-05-19 11:54:01 +0100 | [diff] [blame] | 68 | .setLabel("Confirm") |
| 69 | .setStyle(this.inverted ? "SUCCESS" : "DANGER") |
pineafan | 377794f | 2022-04-18 19:01:01 +0100 | [diff] [blame] | 70 | .setEmoji(getEmojiByName("CONTROL.TICK", "id")), |
| 71 | new Discord.MessageButton() |
| 72 | .setCustomId("no") |
| 73 | .setLabel("Cancel") |
pineafan | 17aba6d | 2022-05-19 20:27:22 +0100 | [diff] [blame] | 74 | .setStyle("SECONDARY") |
pineafan | 377794f | 2022-04-18 19:01:01 +0100 | [diff] [blame] | 75 | .setEmoji(getEmojiByName("CONTROL.CROSS", "id")) |
| 76 | ].concat(this.customButtonTitle ? [new Discord.MessageButton() |
| 77 | .setCustomId("custom") |
| 78 | .setLabel(this.customButtonTitle) |
pineafan | 6fb3e07 | 2022-05-20 19:27:23 +0100 | [diff] [blame] | 79 | .setStyle(this.customBooleanClicked !== null ? |
| 80 | ( this.customBooleanClicked ? "SUCCESS" : "PRIMARY" ) : |
| 81 | "PRIMARY" |
| 82 | ) |
pineafan | 377794f | 2022-04-18 19:01:01 +0100 | [diff] [blame] | 83 | .setDisabled(this.customButtonDisabled) |
pineafan | e625d78 | 2022-05-09 18:04:32 +0100 | [diff] [blame] | 84 | .setEmoji(getEmojiByName("CONTROL.TICKET", "id")) |
pineafan | 73a7c4a | 2022-07-24 10:38:04 +0100 | [diff] [blame^] | 85 | ] : []) |
| 86 | .concat(this.reason !== null ? [new Discord.MessageButton() |
| 87 | .setCustomId("reason") |
| 88 | .setLabel(`Edit Reason`) |
| 89 | .setStyle("PRIMARY") |
| 90 | .setEmoji(getEmojiByName("ICONS.EDIT", "id")) |
pineafan | 377794f | 2022-04-18 19:01:01 +0100 | [diff] [blame] | 91 | ] : [])) |
| 92 | ], |
| 93 | ephemeral: true, |
| 94 | fetchReply: true |
| 95 | } |
| 96 | let m; |
| 97 | if ( editOnly ) { |
| 98 | m = await this.interaction.editReply(object); |
| 99 | } else { |
| 100 | m = await this.interaction.reply(object) |
| 101 | } |
| 102 | let component; |
| 103 | try { |
pineafan | c6158ab | 2022-06-17 16:34:07 +0100 | [diff] [blame] | 104 | 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] | 105 | } catch (e) { |
pineafan | 6fb3e07 | 2022-05-20 19:27:23 +0100 | [diff] [blame] | 106 | return { |
| 107 | success: false, |
| 108 | buttonClicked: this.customBooleanClicked ?? this.customCallbackClicked, |
| 109 | response: this.customCallbackResponse |
| 110 | }; |
pineafan | 377794f | 2022-04-18 19:01:01 +0100 | [diff] [blame] | 111 | } |
| 112 | if (component.customId === "yes") { |
| 113 | component.deferUpdate(); |
pineafan | 6fb3e07 | 2022-05-20 19:27:23 +0100 | [diff] [blame] | 114 | if (this.customBooleanClicked === true) this.customCallbackResponse = await this.customBoolean(); |
| 115 | return { |
| 116 | success: true, |
| 117 | buttonClicked: this.customBooleanClicked ?? this.customCallbackClicked, |
| 118 | response: this.customCallbackResponse |
| 119 | }; |
pineafan | 377794f | 2022-04-18 19:01:01 +0100 | [diff] [blame] | 120 | } else if (component.customId === "no") { |
| 121 | component.deferUpdate(); |
pineafan | 6fb3e07 | 2022-05-20 19:27:23 +0100 | [diff] [blame] | 122 | return { |
| 123 | success: false, |
| 124 | buttonClicked: this.customBooleanClicked ?? this.customCallbackClicked, |
| 125 | response: this.customCallbackResponse |
| 126 | }; |
pineafan | 377794f | 2022-04-18 19:01:01 +0100 | [diff] [blame] | 127 | } else if (component.customId === "custom") { |
| 128 | component.deferUpdate(); |
pineafan | 6fb3e07 | 2022-05-20 19:27:23 +0100 | [diff] [blame] | 129 | if (this.customBooleanClicked !== null) { |
| 130 | this.customBooleanClicked = !this.customBooleanClicked; |
| 131 | } else { |
| 132 | this.customCallbackResponse = await this.customCallback(); |
| 133 | this.customCallbackClicked = true; |
| 134 | this.customButtonDisabled = true; |
| 135 | } |
pineafan | 377794f | 2022-04-18 19:01:01 +0100 | [diff] [blame] | 136 | editOnly = true; |
pineafan | 73a7c4a | 2022-07-24 10:38:04 +0100 | [diff] [blame^] | 137 | } else if (component.customId === "reason") { |
| 138 | await component.showModal(new Discord.Modal().setCustomId("modal").setTitle(`Editing reason`).addComponents( |
| 139 | // @ts-ignore |
| 140 | new MessageActionRow().addComponents(new TextInputComponent() |
| 141 | .setCustomId("reason") |
| 142 | .setLabel("Reason") |
| 143 | .setMaxLength(2000) |
| 144 | .setRequired(false) |
| 145 | .setStyle("PARAGRAPH") |
| 146 | .setPlaceholder("Spammed in #general") |
| 147 | .setValue(this.reason ? this.reason : "") |
| 148 | ) |
| 149 | )) |
| 150 | await this.interaction.editReply({ |
| 151 | embeds: [new EmojiEmbed() |
| 152 | .setTitle(this.title) |
| 153 | .setDescription("Modal opened. If you can't see it, click back and try again.") |
| 154 | .setStatus(this.color) |
| 155 | .setEmoji(this.emoji) |
| 156 | ], components: [new MessageActionRow().addComponents([new MessageButton() |
| 157 | .setLabel("Back") |
| 158 | .setEmoji(getEmojiByName("CONTROL.LEFT", "id")) |
| 159 | .setStyle("PRIMARY") |
| 160 | .setCustomId("back") |
| 161 | ])] |
| 162 | }); |
| 163 | let out; |
| 164 | try { |
| 165 | out = await modalInteractionCollector(m, (m) => m.channel.id == this.interaction.channel.id, (m) => m.customId == "reason") |
| 166 | } catch (e) { continue } |
| 167 | if (out.fields) { |
| 168 | return {newReason: out.fields.getTextInputValue("reason") ?? ""}; |
| 169 | } else { return { newReason: this.reason } } |
pineafan | 377794f | 2022-04-18 19:01:01 +0100 | [diff] [blame] | 170 | } |
pineafan | 8b4b17f | 2022-02-27 20:42:52 +0000 | [diff] [blame] | 171 | } |
pineafan | 4f164f3 | 2022-02-26 22:07:12 +0000 | [diff] [blame] | 172 | } |
| 173 | } |
| 174 | |
pineafan | 73a7c4a | 2022-07-24 10:38:04 +0100 | [diff] [blame^] | 175 | export default confirmationMessage; |