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