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