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 | 6fb3e07 | 2022-05-20 19:27:23 +0100 | [diff] [blame] | 17 | customBoolean: () => any; |
| 18 | customBooleanClicked: boolean = null; |
PineappleFan | 19b002b | 2022-05-19 11:54:01 +0100 | [diff] [blame] | 19 | inverted: boolean; |
pineafan | 4f164f3 | 2022-02-26 22:07:12 +0000 | [diff] [blame] | 20 | |
| 21 | constructor(interaction: CommandInteraction) { |
| 22 | this.interaction = interaction; |
| 23 | |
| 24 | this.title = ""; |
| 25 | this.emoji = ""; |
| 26 | this.description = ""; |
| 27 | this.color = ""; |
pineafan | 6fb3e07 | 2022-05-20 19:27:23 +0100 | [diff] [blame] | 28 | this.inverted = false; |
| 29 | this.customCallback = () => {}; |
| 30 | this.customBoolean = () => {}; |
pineafan | 4f164f3 | 2022-02-26 22:07:12 +0000 | [diff] [blame] | 31 | } |
| 32 | |
| 33 | setTitle(title: string) { this.title = title; return this } |
| 34 | setEmoji(emoji: string) { this.emoji = emoji; return this } |
| 35 | setDescription(description: string) { this.description = description; return this } |
| 36 | setColor(color: string) { this.color = color; return this } |
pineafan | 167bde3 | 2022-05-19 19:33:46 +0100 | [diff] [blame] | 37 | setInverted(inverted: boolean) { this.inverted = inverted; return this } |
pineafan | 377794f | 2022-04-18 19:01:01 +0100 | [diff] [blame] | 38 | addCustomCallback(title: string, disabled: boolean, callback: () => any, callbackClicked: string) { |
pineafan | 6fb3e07 | 2022-05-20 19:27:23 +0100 | [diff] [blame] | 39 | if (this.customButtonTitle) return this |
pineafan | 377794f | 2022-04-18 19:01:01 +0100 | [diff] [blame] | 40 | this.customButtonTitle = title; |
| 41 | this.customButtonDisabled = disabled; |
| 42 | this.customCallback = callback; |
| 43 | this.customCallbackString = callbackClicked; |
| 44 | return this; |
| 45 | } |
pineafan | 6fb3e07 | 2022-05-20 19:27:23 +0100 | [diff] [blame] | 46 | addCustomBoolean(title: string, disabled: boolean, callback: () => any, callbackClicked: string) { |
| 47 | if (this.customButtonTitle) return this |
| 48 | this.customButtonTitle = title; |
| 49 | this.customButtonDisabled = disabled; |
| 50 | this.customBoolean = callback; |
| 51 | this.customCallbackString = callbackClicked; |
| 52 | this.customBooleanClicked = false; |
| 53 | return this; |
| 54 | } |
| 55 | |
pineafan | 4f164f3 | 2022-02-26 22:07:12 +0000 | [diff] [blame] | 56 | |
pineafan | 8b4b17f | 2022-02-27 20:42:52 +0000 | [diff] [blame] | 57 | async send(editOnly?: boolean) { |
pineafan | 377794f | 2022-04-18 19:01:01 +0100 | [diff] [blame] | 58 | while (true) { |
| 59 | let object = { |
| 60 | embeds: [ |
| 61 | new generateEmojiEmbed() |
| 62 | .setEmoji(this.emoji) |
| 63 | .setTitle(this.title) |
| 64 | .setDescription(this.description) |
| 65 | .setStatus(this.color) |
pineafan | 6fb3e07 | 2022-05-20 19:27:23 +0100 | [diff] [blame] | 66 | .setFooter({text: (this.customBooleanClicked ?? this.customCallbackClicked) ? this.customCallbackString : ""}) |
pineafan | 377794f | 2022-04-18 19:01:01 +0100 | [diff] [blame] | 67 | ], |
| 68 | components: [ |
| 69 | new MessageActionRow().addComponents([ |
| 70 | new Discord.MessageButton() |
| 71 | .setCustomId("yes") |
PineappleFan | 19b002b | 2022-05-19 11:54:01 +0100 | [diff] [blame] | 72 | .setLabel("Confirm") |
| 73 | .setStyle(this.inverted ? "SUCCESS" : "DANGER") |
pineafan | 377794f | 2022-04-18 19:01:01 +0100 | [diff] [blame] | 74 | .setEmoji(getEmojiByName("CONTROL.TICK", "id")), |
| 75 | new Discord.MessageButton() |
| 76 | .setCustomId("no") |
| 77 | .setLabel("Cancel") |
pineafan | 17aba6d | 2022-05-19 20:27:22 +0100 | [diff] [blame] | 78 | .setStyle("SECONDARY") |
pineafan | 377794f | 2022-04-18 19:01:01 +0100 | [diff] [blame] | 79 | .setEmoji(getEmojiByName("CONTROL.CROSS", "id")) |
| 80 | ].concat(this.customButtonTitle ? [new Discord.MessageButton() |
| 81 | .setCustomId("custom") |
| 82 | .setLabel(this.customButtonTitle) |
pineafan | 6fb3e07 | 2022-05-20 19:27:23 +0100 | [diff] [blame] | 83 | .setStyle(this.customBooleanClicked !== null ? |
| 84 | ( this.customBooleanClicked ? "SUCCESS" : "PRIMARY" ) : |
| 85 | "PRIMARY" |
| 86 | ) |
pineafan | 377794f | 2022-04-18 19:01:01 +0100 | [diff] [blame] | 87 | .setDisabled(this.customButtonDisabled) |
pineafan | e625d78 | 2022-05-09 18:04:32 +0100 | [diff] [blame] | 88 | .setEmoji(getEmojiByName("CONTROL.TICKET", "id")) |
pineafan | 377794f | 2022-04-18 19:01:01 +0100 | [diff] [blame] | 89 | ] : [])) |
| 90 | ], |
| 91 | ephemeral: true, |
| 92 | fetchReply: true |
| 93 | } |
| 94 | let m; |
| 95 | if ( editOnly ) { |
| 96 | m = await this.interaction.editReply(object); |
| 97 | } else { |
| 98 | m = await this.interaction.reply(object) |
| 99 | } |
| 100 | let component; |
| 101 | try { |
pineafan | c6158ab | 2022-06-17 16:34:07 +0100 | [diff] [blame] | 102 | 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] | 103 | } catch (e) { |
pineafan | 6fb3e07 | 2022-05-20 19:27:23 +0100 | [diff] [blame] | 104 | return { |
| 105 | success: false, |
| 106 | buttonClicked: this.customBooleanClicked ?? this.customCallbackClicked, |
| 107 | response: this.customCallbackResponse |
| 108 | }; |
pineafan | 377794f | 2022-04-18 19:01:01 +0100 | [diff] [blame] | 109 | } |
| 110 | if (component.customId === "yes") { |
| 111 | component.deferUpdate(); |
pineafan | 6fb3e07 | 2022-05-20 19:27:23 +0100 | [diff] [blame] | 112 | if (this.customBooleanClicked === true) this.customCallbackResponse = await this.customBoolean(); |
| 113 | return { |
| 114 | success: true, |
| 115 | buttonClicked: this.customBooleanClicked ?? this.customCallbackClicked, |
| 116 | response: this.customCallbackResponse |
| 117 | }; |
pineafan | 377794f | 2022-04-18 19:01:01 +0100 | [diff] [blame] | 118 | } else if (component.customId === "no") { |
| 119 | component.deferUpdate(); |
pineafan | 6fb3e07 | 2022-05-20 19:27:23 +0100 | [diff] [blame] | 120 | return { |
| 121 | success: false, |
| 122 | buttonClicked: this.customBooleanClicked ?? this.customCallbackClicked, |
| 123 | response: this.customCallbackResponse |
| 124 | }; |
pineafan | 377794f | 2022-04-18 19:01:01 +0100 | [diff] [blame] | 125 | } else if (component.customId === "custom") { |
| 126 | component.deferUpdate(); |
pineafan | 6fb3e07 | 2022-05-20 19:27:23 +0100 | [diff] [blame] | 127 | if (this.customBooleanClicked !== null) { |
| 128 | this.customBooleanClicked = !this.customBooleanClicked; |
| 129 | } else { |
| 130 | this.customCallbackResponse = await this.customCallback(); |
| 131 | this.customCallbackClicked = true; |
| 132 | this.customButtonDisabled = true; |
| 133 | } |
pineafan | 377794f | 2022-04-18 19:01:01 +0100 | [diff] [blame] | 134 | editOnly = true; |
| 135 | } |
pineafan | 8b4b17f | 2022-02-27 20:42:52 +0000 | [diff] [blame] | 136 | } |
pineafan | 4f164f3 | 2022-02-26 22:07:12 +0000 | [diff] [blame] | 137 | } |
| 138 | } |
| 139 | |
| 140 | export default confirmationMessage; |