pineafan | 4f164f3 | 2022-02-26 22:07:12 +0000 | [diff] [blame] | 1 | import Discord, { CommandInteraction, MessageActionRow, Message } from "discord.js"; |
pineafan | 377794f | 2022-04-18 19:01:01 +0100 | [diff] [blame] | 2 | import generateEmojiEmbed from "./generateEmojiEmbed.js" |
pineafan | 8b4b17f | 2022-02-27 20:42:52 +0000 | [diff] [blame] | 3 | import getEmojiByName from "./getEmojiByName.js"; |
pineafan | 4f164f3 | 2022-02-26 22:07:12 +0000 | [diff] [blame] | 4 | |
| 5 | class confirmationMessage { |
| 6 | interaction: CommandInteraction; |
| 7 | title: string; |
| 8 | emoji: string; |
| 9 | description: string; |
| 10 | color: string; |
pineafan | 377794f | 2022-04-18 19:01:01 +0100 | [diff] [blame] | 11 | customCallback: () => any; |
| 12 | customButtonTitle: string; |
| 13 | customButtonDisabled: boolean; |
| 14 | customCallbackString: string = ""; |
| 15 | customCallbackClicked: boolean = false; |
| 16 | customCallbackResponse: any = null; |
PineappleFan | 19b002b | 2022-05-19 11:54:01 +0100 | [diff] [blame] | 17 | inverted: boolean; |
pineafan | 4f164f3 | 2022-02-26 22:07:12 +0000 | [diff] [blame] | 18 | |
| 19 | constructor(interaction: CommandInteraction) { |
| 20 | this.interaction = interaction; |
| 21 | |
| 22 | this.title = ""; |
| 23 | this.emoji = ""; |
| 24 | this.description = ""; |
| 25 | this.color = ""; |
PineappleFan | 19b002b | 2022-05-19 11:54:01 +0100 | [diff] [blame] | 26 | this.inverted = false |
pineafan | 377794f | 2022-04-18 19:01:01 +0100 | [diff] [blame] | 27 | this.customCallback = () => {} |
pineafan | 4f164f3 | 2022-02-26 22:07:12 +0000 | [diff] [blame] | 28 | } |
| 29 | |
| 30 | setTitle(title: string) { this.title = title; return this } |
| 31 | setEmoji(emoji: string) { this.emoji = emoji; return this } |
| 32 | setDescription(description: string) { this.description = description; return this } |
| 33 | setColor(color: string) { this.color = color; return this } |
pineafan | 167bde3 | 2022-05-19 19:33:46 +0100 | [diff] [blame] | 34 | setInverted(inverted: boolean) { this.inverted = inverted; return this } |
pineafan | 377794f | 2022-04-18 19:01:01 +0100 | [diff] [blame] | 35 | addCustomCallback(title: string, disabled: boolean, callback: () => any, callbackClicked: string) { |
| 36 | this.customButtonTitle = title; |
| 37 | this.customButtonDisabled = disabled; |
| 38 | this.customCallback = callback; |
| 39 | this.customCallbackString = callbackClicked; |
| 40 | return this; |
| 41 | } |
pineafan | 4f164f3 | 2022-02-26 22:07:12 +0000 | [diff] [blame] | 42 | |
pineafan | 8b4b17f | 2022-02-27 20:42:52 +0000 | [diff] [blame] | 43 | async send(editOnly?: boolean) { |
pineafan | 377794f | 2022-04-18 19:01:01 +0100 | [diff] [blame] | 44 | while (true) { |
| 45 | let object = { |
| 46 | embeds: [ |
| 47 | new generateEmojiEmbed() |
| 48 | .setEmoji(this.emoji) |
| 49 | .setTitle(this.title) |
| 50 | .setDescription(this.description) |
| 51 | .setStatus(this.color) |
| 52 | .setFooter({text: this.customCallbackClicked ? this.customCallbackString : ""}) |
| 53 | ], |
| 54 | components: [ |
| 55 | new MessageActionRow().addComponents([ |
| 56 | new Discord.MessageButton() |
| 57 | .setCustomId("yes") |
PineappleFan | 19b002b | 2022-05-19 11:54:01 +0100 | [diff] [blame] | 58 | .setLabel("Confirm") |
| 59 | .setStyle(this.inverted ? "SUCCESS" : "DANGER") |
pineafan | 377794f | 2022-04-18 19:01:01 +0100 | [diff] [blame] | 60 | .setEmoji(getEmojiByName("CONTROL.TICK", "id")), |
| 61 | new Discord.MessageButton() |
| 62 | .setCustomId("no") |
| 63 | .setLabel("Cancel") |
pineafan | 17aba6d | 2022-05-19 20:27:22 +0100 | [diff] [blame^] | 64 | .setStyle("SECONDARY") |
pineafan | 377794f | 2022-04-18 19:01:01 +0100 | [diff] [blame] | 65 | .setEmoji(getEmojiByName("CONTROL.CROSS", "id")) |
| 66 | ].concat(this.customButtonTitle ? [new Discord.MessageButton() |
| 67 | .setCustomId("custom") |
| 68 | .setLabel(this.customButtonTitle) |
pineafan | 17aba6d | 2022-05-19 20:27:22 +0100 | [diff] [blame^] | 69 | .setStyle("PRIMARY") |
pineafan | 377794f | 2022-04-18 19:01:01 +0100 | [diff] [blame] | 70 | .setDisabled(this.customButtonDisabled) |
pineafan | e625d78 | 2022-05-09 18:04:32 +0100 | [diff] [blame] | 71 | .setEmoji(getEmojiByName("CONTROL.TICKET", "id")) |
pineafan | 377794f | 2022-04-18 19:01:01 +0100 | [diff] [blame] | 72 | ] : [])) |
| 73 | ], |
| 74 | ephemeral: true, |
| 75 | fetchReply: true |
| 76 | } |
| 77 | let m; |
| 78 | if ( editOnly ) { |
| 79 | m = await this.interaction.editReply(object); |
| 80 | } else { |
| 81 | m = await this.interaction.reply(object) |
| 82 | } |
| 83 | let component; |
| 84 | try { |
| 85 | component = await (m as Message).awaitMessageComponent({filter: (m) => m.user.id === this.interaction.user.id, time: 2.5 * 60 * 1000}); |
| 86 | } catch (e) { |
PineappleFan | 19b002b | 2022-05-19 11:54:01 +0100 | [diff] [blame] | 87 | return { success: false, buttonClicked: this.customCallbackClicked, response: this.customCallbackResponse }; |
pineafan | 377794f | 2022-04-18 19:01:01 +0100 | [diff] [blame] | 88 | } |
| 89 | if (component.customId === "yes") { |
| 90 | component.deferUpdate(); |
PineappleFan | 19b002b | 2022-05-19 11:54:01 +0100 | [diff] [blame] | 91 | return { success: true, buttonClicked: this.customCallbackClicked, response: this.customCallbackResponse }; |
pineafan | 377794f | 2022-04-18 19:01:01 +0100 | [diff] [blame] | 92 | } else if (component.customId === "no") { |
| 93 | component.deferUpdate(); |
PineappleFan | 19b002b | 2022-05-19 11:54:01 +0100 | [diff] [blame] | 94 | return { success: false, buttonClicked: this.customCallbackClicked, response: this.customCallbackResponse }; |
pineafan | 377794f | 2022-04-18 19:01:01 +0100 | [diff] [blame] | 95 | } else if (component.customId === "custom") { |
| 96 | component.deferUpdate(); |
| 97 | this.customCallbackResponse = this.customCallback(); |
| 98 | this.customCallbackClicked = true; |
| 99 | this.customButtonDisabled = true; |
| 100 | editOnly = true; |
| 101 | } |
pineafan | 8b4b17f | 2022-02-27 20:42:52 +0000 | [diff] [blame] | 102 | } |
pineafan | 4f164f3 | 2022-02-26 22:07:12 +0000 | [diff] [blame] | 103 | } |
| 104 | } |
| 105 | |
| 106 | export default confirmationMessage; |