PineaFan | 0d06edc | 2023-01-17 22:10:31 +0000 | [diff] [blame] | 1 | import { LoadingEmbed } from "../../utils/defaults.js"; |
pineafan | 6702cef | 2022-06-13 17:52:37 +0100 | [diff] [blame] | 2 | import getEmojiByName from "../../utils/getEmojiByName.js"; |
pineafan | 4edb776 | 2022-06-26 19:21:04 +0100 | [diff] [blame] | 3 | import EmojiEmbed from "../../utils/generateEmojiEmbed.js"; |
Skyler Grey | 75ea917 | 2022-08-06 10:22:23 +0100 | [diff] [blame] | 4 | import Discord, { |
| 5 | CommandInteraction, |
Skyler Grey | 75ea917 | 2022-08-06 10:22:23 +0100 | [diff] [blame] | 6 | Message, |
TheCodedProf | 21c0859 | 2022-09-13 14:14:43 -0400 | [diff] [blame] | 7 | ActionRowBuilder, |
TheCodedProf | 21c0859 | 2022-09-13 14:14:43 -0400 | [diff] [blame] | 8 | ButtonBuilder, |
TheCodedProf | a16d167 | 2023-01-18 18:58:34 -0500 | [diff] [blame] | 9 | StringSelectMenuBuilder, |
TheCodedProf | 59772f8 | 2023-01-18 22:17:16 -0500 | [diff] [blame] | 10 | ButtonStyle, |
| 11 | TextInputBuilder, |
| 12 | ButtonComponent, |
TheCodedProf | 59772f8 | 2023-01-18 22:17:16 -0500 | [diff] [blame] | 13 | ModalSubmitInteraction, |
TheCodedProf | 1f67504 | 2023-02-16 17:01:29 -0500 | [diff] [blame^] | 14 | APIMessageComponentEmoji, |
| 15 | RoleSelectMenuBuilder, |
| 16 | ChannelSelectMenuBuilder, |
| 17 | RoleSelectMenuInteraction, |
| 18 | ButtonInteraction, |
| 19 | ChannelSelectMenuInteraction, |
| 20 | TextInputStyle, |
| 21 | ModalBuilder, |
| 22 | ChannelType |
Skyler Grey | 75ea917 | 2022-08-06 10:22:23 +0100 | [diff] [blame] | 23 | } from "discord.js"; |
TheCodedProf | f86ba09 | 2023-01-27 17:10:07 -0500 | [diff] [blame] | 24 | import { SlashCommandSubcommandBuilder, StringSelectMenuOptionBuilder } from "discord.js"; |
pineafan | 6702cef | 2022-06-13 17:52:37 +0100 | [diff] [blame] | 25 | import client from "../../utils/client.js"; |
Skyler Grey | 11236ba | 2022-08-08 21:13:33 +0100 | [diff] [blame] | 26 | import { toHexInteger, toHexArray, tickets as ticketTypes } from "../../utils/calculate.js"; |
pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame] | 27 | import { capitalize } from "../../utils/generateKeyValueList.js"; |
pineafan | 6702cef | 2022-06-13 17:52:37 +0100 | [diff] [blame] | 28 | import { modalInteractionCollector } from "../../utils/dualCollector.js"; |
pineafan | 3a02ea3 | 2022-08-11 21:35:04 +0100 | [diff] [blame] | 29 | import type { GuildConfig } from "../../utils/database.js"; |
TheCodedProf | 1f67504 | 2023-02-16 17:01:29 -0500 | [diff] [blame^] | 30 | import { LinkWarningFooter } from "../../utils/defaults.js"; |
pineafan | 4f164f3 | 2022-02-26 22:07:12 +0000 | [diff] [blame] | 31 | |
Skyler Grey | 75ea917 | 2022-08-06 10:22:23 +0100 | [diff] [blame] | 32 | const command = (builder: SlashCommandSubcommandBuilder) => |
| 33 | builder |
| 34 | .setName("tickets") |
TheCodedProf | 1f67504 | 2023-02-16 17:01:29 -0500 | [diff] [blame^] | 35 | .setDescription("Shows settings for tickets") |
pineafan | 4f164f3 | 2022-02-26 22:07:12 +0000 | [diff] [blame] | 36 | |
pineafan | 3a02ea3 | 2022-08-11 21:35:04 +0100 | [diff] [blame] | 37 | const callback = async (interaction: CommandInteraction): Promise<unknown> => { |
PineaFan | a00db1b | 2023-01-02 15:32:54 +0000 | [diff] [blame] | 38 | if (!interaction.guild) return; |
Skyler Grey | 75ea917 | 2022-08-06 10:22:23 +0100 | [diff] [blame] | 39 | let m = (await interaction.reply({ |
| 40 | embeds: LoadingEmbed, |
| 41 | ephemeral: true, |
| 42 | fetchReply: true |
| 43 | })) as Message; |
PineaFan | 638eb13 | 2023-01-19 10:41:22 +0000 | [diff] [blame] | 44 | const data = await client.database.guilds.read(interaction.guild.id); |
| 45 | data.tickets.customTypes = (data.tickets.customTypes ?? []).filter( |
Skyler Grey | 11236ba | 2022-08-08 21:13:33 +0100 | [diff] [blame] | 46 | (value: string, index: number, array: string[]) => array.indexOf(value) === index |
Skyler Grey | 75ea917 | 2022-08-06 10:22:23 +0100 | [diff] [blame] | 47 | ); |
TheCodedProf | 1f67504 | 2023-02-16 17:01:29 -0500 | [diff] [blame^] | 48 | let ticketData = (await client.database.guilds.read(interaction.guild.id)).tickets |
| 49 | let changesMade = false; |
Skyler Grey | ad00217 | 2022-08-16 18:48:26 +0100 | [diff] [blame] | 50 | let timedOut = false; |
TheCodedProf | 1f67504 | 2023-02-16 17:01:29 -0500 | [diff] [blame^] | 51 | let errorMessage = ""; |
Skyler Grey | ad00217 | 2022-08-16 18:48:26 +0100 | [diff] [blame] | 52 | while (!timedOut) { |
TheCodedProf | 1f67504 | 2023-02-16 17:01:29 -0500 | [diff] [blame^] | 53 | const embed: EmojiEmbed = new EmojiEmbed() |
pineafan | 6702cef | 2022-06-13 17:52:37 +0100 | [diff] [blame] | 54 | .setTitle("Tickets") |
| 55 | .setDescription( |
TheCodedProf | 1f67504 | 2023-02-16 17:01:29 -0500 | [diff] [blame^] | 56 | `${ticketData.enabled ? "" : getEmojiByName("TICKETS.REPORT")} **Enabled:** ${ |
| 57 | ticketData.enabled ? `${getEmojiByName("CONTROL.TICK")} Yes` : `${getEmojiByName("CONTROL.CROSS")} No` |
Skyler Grey | 75ea917 | 2022-08-06 10:22:23 +0100 | [diff] [blame] | 58 | }\n` + |
TheCodedProf | 1f67504 | 2023-02-16 17:01:29 -0500 | [diff] [blame^] | 59 | `${ticketData.category ? "" : getEmojiByName("TICKETS.REPORT")}` + |
| 60 | ((await interaction.guild.channels.fetch(ticketData.category!))!.type === ChannelType.GuildCategory ? |
| 61 | `**Category:** ` : `**Channel:** `) + // TODO: Notify if permissions are wrong |
| 62 | `${ticketData.category ? `<#${ticketData.category}>` : "*None set*"}\n` + |
| 63 | `**Max Tickets:** ${ticketData.maxTickets ? ticketData.maxTickets : "*No limit*"}\n` + |
| 64 | `**Support Ping:** ${ticketData.supportRole ? `<@&${ticketData.supportRole}>` : "*None set*"}\n\n` + |
| 65 | (ticketData.useCustom && ticketData.customTypes === null ? `${getEmojiByName("TICKETS.REPORT")} ` : "") + |
| 66 | `${ticketData.useCustom ? "Custom" : "Default"} types in use` + |
Skyler Grey | 75ea917 | 2022-08-06 10:22:23 +0100 | [diff] [blame] | 67 | "\n\n" + |
Skyler Grey | 11236ba | 2022-08-08 21:13:33 +0100 | [diff] [blame] | 68 | `${getEmojiByName("TICKETS.REPORT")} *Indicates a setting stopping tickets from being used*` |
pineafan | 6702cef | 2022-06-13 17:52:37 +0100 | [diff] [blame] | 69 | ) |
| 70 | .setStatus("Success") |
pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame] | 71 | .setEmoji("GUILD.TICKET.OPEN"); |
TheCodedProf | 1f67504 | 2023-02-16 17:01:29 -0500 | [diff] [blame^] | 72 | if (errorMessage) embed.setFooter({text: errorMessage, iconURL: LinkWarningFooter.iconURL}); |
Skyler Grey | 75ea917 | 2022-08-06 10:22:23 +0100 | [diff] [blame] | 73 | m = (await interaction.editReply({ |
| 74 | embeds: [embed], |
| 75 | components: [ |
TheCodedProf | 1f67504 | 2023-02-16 17:01:29 -0500 | [diff] [blame^] | 76 | new ActionRowBuilder<ButtonBuilder>().addComponents( |
TheCodedProf | 21c0859 | 2022-09-13 14:14:43 -0400 | [diff] [blame] | 77 | new ButtonBuilder() |
TheCodedProf | 1f67504 | 2023-02-16 17:01:29 -0500 | [diff] [blame^] | 78 | .setLabel("Tickets " + (ticketData.enabled ? "enabled" : "disabled")) |
| 79 | .setEmoji(getEmojiByName("CONTROL." + (ticketData.enabled ? "TICK" : "CROSS"), "id")) |
| 80 | .setStyle(ticketData.enabled ? ButtonStyle.Success : ButtonStyle.Danger) |
Skyler Grey | 75ea917 | 2022-08-06 10:22:23 +0100 | [diff] [blame] | 81 | .setCustomId("enabled"), |
TheCodedProf | 21c0859 | 2022-09-13 14:14:43 -0400 | [diff] [blame] | 82 | new ButtonBuilder() |
TheCodedProf | 1f67504 | 2023-02-16 17:01:29 -0500 | [diff] [blame^] | 83 | .setLabel("Set max tickets") |
| 84 | .setEmoji(getEmojiByName("CONTROL.TICKET", "id")) |
| 85 | .setStyle(ButtonStyle.Primary) |
| 86 | .setCustomId("setMaxTickets") |
| 87 | .setDisabled(!ticketData.enabled), |
TheCodedProf | 21c0859 | 2022-09-13 14:14:43 -0400 | [diff] [blame] | 88 | new ButtonBuilder() |
Skyler Grey | 75ea917 | 2022-08-06 10:22:23 +0100 | [diff] [blame] | 89 | .setLabel("Manage types") |
| 90 | .setEmoji(getEmojiByName("TICKETS.OTHER", "id")) |
TheCodedProf | 21c0859 | 2022-09-13 14:14:43 -0400 | [diff] [blame] | 91 | .setStyle(ButtonStyle.Secondary) |
TheCodedProf | 1f67504 | 2023-02-16 17:01:29 -0500 | [diff] [blame^] | 92 | .setCustomId("manageTypes") |
| 93 | .setDisabled(!ticketData.enabled), |
TheCodedProf | 21c0859 | 2022-09-13 14:14:43 -0400 | [diff] [blame] | 94 | new ButtonBuilder() |
TheCodedProf | 1f67504 | 2023-02-16 17:01:29 -0500 | [diff] [blame^] | 95 | .setLabel("Save") |
| 96 | .setEmoji(getEmojiByName("ICONS.SAVE", "id")) |
| 97 | .setStyle(ButtonStyle.Success) |
| 98 | .setCustomId("save") |
| 99 | .setDisabled(!changesMade) |
| 100 | ), |
| 101 | new ActionRowBuilder<RoleSelectMenuBuilder>().addComponents( |
| 102 | new RoleSelectMenuBuilder() |
| 103 | .setCustomId("supportRole") |
| 104 | .setPlaceholder("Select a support role") |
| 105 | .setDisabled(!ticketData.enabled) |
| 106 | ), |
| 107 | new ActionRowBuilder<ChannelSelectMenuBuilder>().addComponents( |
| 108 | new ChannelSelectMenuBuilder() |
| 109 | .setCustomId("category") |
| 110 | .setPlaceholder("Select a category or channel") |
| 111 | .setDisabled(!ticketData.enabled) |
| 112 | ) |
Skyler Grey | 75ea917 | 2022-08-06 10:22:23 +0100 | [diff] [blame] | 113 | ] |
TheCodedProf | 1f67504 | 2023-02-16 17:01:29 -0500 | [diff] [blame^] | 114 | })); |
| 115 | let i: RoleSelectMenuInteraction | ButtonInteraction | ChannelSelectMenuInteraction; |
pineafan | 6702cef | 2022-06-13 17:52:37 +0100 | [diff] [blame] | 116 | try { |
TheCodedProf | 1f67504 | 2023-02-16 17:01:29 -0500 | [diff] [blame^] | 117 | i = await m.awaitMessageComponent<2 | 6 | 8>({ |
PineaFan | 0d06edc | 2023-01-17 22:10:31 +0000 | [diff] [blame] | 118 | time: 300000, |
TheCodedProf | 267563a | 2023-01-21 17:00:57 -0500 | [diff] [blame] | 119 | filter: (i) => { return i.user.id === interaction.user.id && i.channel!.id === interaction.channel!.id && i.message.id === m.id } |
PineaFan | 0d06edc | 2023-01-17 22:10:31 +0000 | [diff] [blame] | 120 | }); |
Skyler Grey | 75ea917 | 2022-08-06 10:22:23 +0100 | [diff] [blame] | 121 | } catch (e) { |
Skyler Grey | ad00217 | 2022-08-16 18:48:26 +0100 | [diff] [blame] | 122 | timedOut = true; |
| 123 | continue; |
Skyler Grey | 75ea917 | 2022-08-06 10:22:23 +0100 | [diff] [blame] | 124 | } |
TheCodedProf | 1f67504 | 2023-02-16 17:01:29 -0500 | [diff] [blame^] | 125 | changesMade = true; |
| 126 | if (i.isRoleSelectMenu()) { |
| 127 | await i.deferUpdate(); |
| 128 | ticketData.supportRole = i.values[0] ?? null; |
| 129 | } else if (i.isChannelSelectMenu()) { |
| 130 | await i.deferUpdate(); |
| 131 | ticketData.category = i.values[0] ?? null; |
| 132 | } else { |
| 133 | switch(i.customId) { |
| 134 | case "save": { |
TheCodedProf | 267563a | 2023-01-21 17:00:57 -0500 | [diff] [blame] | 135 | await i.deferUpdate(); |
TheCodedProf | 1f67504 | 2023-02-16 17:01:29 -0500 | [diff] [blame^] | 136 | await client.database.guilds.write(interaction.guild.id, { tickets: ticketData }); |
| 137 | changesMade = false; |
| 138 | break; |
| 139 | } |
| 140 | case "enabled": { |
TheCodedProf | 267563a | 2023-01-21 17:00:57 -0500 | [diff] [blame] | 141 | await i.deferUpdate(); |
TheCodedProf | 1f67504 | 2023-02-16 17:01:29 -0500 | [diff] [blame^] | 142 | ticketData.enabled = !ticketData.enabled; |
| 143 | break; |
| 144 | } |
| 145 | case "setMaxTickets": { |
Skyler Grey | 75ea917 | 2022-08-06 10:22:23 +0100 | [diff] [blame] | 146 | await i.showModal( |
TheCodedProf | 1f67504 | 2023-02-16 17:01:29 -0500 | [diff] [blame^] | 147 | new ModalBuilder() |
| 148 | .setCustomId("maxTickets") |
| 149 | .setTitle("Set max tickets") |
Skyler Grey | 75ea917 | 2022-08-06 10:22:23 +0100 | [diff] [blame] | 150 | .addComponents( |
TheCodedProf | 1f67504 | 2023-02-16 17:01:29 -0500 | [diff] [blame^] | 151 | new ActionRowBuilder<TextInputBuilder>().setComponents( |
TheCodedProf | 59772f8 | 2023-01-18 22:17:16 -0500 | [diff] [blame] | 152 | new TextInputBuilder() |
TheCodedProf | 1f67504 | 2023-02-16 17:01:29 -0500 | [diff] [blame^] | 153 | .setLabel("Max tickets - Leave blank for no limit") |
| 154 | .setCustomId("maxTickets") |
| 155 | .setPlaceholder("Enter a number") |
| 156 | .setRequired(false) |
| 157 | .setValue(ticketData.maxTickets.toString() ?? "") |
| 158 | .setMinLength(1) |
| 159 | .setMaxLength(3) |
| 160 | .setStyle(TextInputStyle.Short) |
Skyler Grey | 75ea917 | 2022-08-06 10:22:23 +0100 | [diff] [blame] | 161 | ) |
| 162 | ) |
TheCodedProf | 1f67504 | 2023-02-16 17:01:29 -0500 | [diff] [blame^] | 163 | ) |
| 164 | await i.editReply({ |
Skyler Grey | 75ea917 | 2022-08-06 10:22:23 +0100 | [diff] [blame] | 165 | embeds: [ |
| 166 | new EmojiEmbed() |
TheCodedProf | 1f67504 | 2023-02-16 17:01:29 -0500 | [diff] [blame^] | 167 | .setTitle("Tickets") |
Skyler Grey | 11236ba | 2022-08-08 21:13:33 +0100 | [diff] [blame] | 168 | .setDescription("Modal opened. If you can't see it, click back and try again.") |
Skyler Grey | 75ea917 | 2022-08-06 10:22:23 +0100 | [diff] [blame] | 169 | .setStatus("Success") |
| 170 | .setEmoji("GUILD.TICKET.OPEN") |
| 171 | ], |
| 172 | components: [ |
TheCodedProf | 59772f8 | 2023-01-18 22:17:16 -0500 | [diff] [blame] | 173 | new ActionRowBuilder<ButtonBuilder>().addComponents([ |
TheCodedProf | 21c0859 | 2022-09-13 14:14:43 -0400 | [diff] [blame] | 174 | new ButtonBuilder() |
Skyler Grey | 75ea917 | 2022-08-06 10:22:23 +0100 | [diff] [blame] | 175 | .setLabel("Back") |
Skyler Grey | 11236ba | 2022-08-08 21:13:33 +0100 | [diff] [blame] | 176 | .setEmoji(getEmojiByName("CONTROL.LEFT", "id")) |
TheCodedProf | 21c0859 | 2022-09-13 14:14:43 -0400 | [diff] [blame] | 177 | .setStyle(ButtonStyle.Primary) |
Skyler Grey | 75ea917 | 2022-08-06 10:22:23 +0100 | [diff] [blame] | 178 | .setCustomId("back") |
| 179 | ]) |
| 180 | ] |
pineafan | 41d9356 | 2022-07-30 22:10:15 +0100 | [diff] [blame] | 181 | }); |
| 182 | let out; |
| 183 | try { |
Skyler Grey | 75ea917 | 2022-08-06 10:22:23 +0100 | [diff] [blame] | 184 | out = await modalInteractionCollector( |
| 185 | m, |
TheCodedProf | 1f67504 | 2023-02-16 17:01:29 -0500 | [diff] [blame^] | 186 | (m) => m.user.id === interaction.user.id, |
| 187 | (m) => m.customId === "back" |
Skyler Grey | 75ea917 | 2022-08-06 10:22:23 +0100 | [diff] [blame] | 188 | ); |
| 189 | } catch (e) { |
Skyler Grey | ad00217 | 2022-08-16 18:48:26 +0100 | [diff] [blame] | 190 | continue; |
Skyler Grey | 75ea917 | 2022-08-06 10:22:23 +0100 | [diff] [blame] | 191 | } |
TheCodedProf | 1f67504 | 2023-02-16 17:01:29 -0500 | [diff] [blame^] | 192 | if (!out || out.isButton()) continue; |
TheCodedProf | 59772f8 | 2023-01-18 22:17:16 -0500 | [diff] [blame] | 193 | out = out as ModalSubmitInteraction; |
TheCodedProf | 1f67504 | 2023-02-16 17:01:29 -0500 | [diff] [blame^] | 194 | let toAdd = out.fields.getTextInputValue("maxTickets"); |
| 195 | if(isNaN(parseInt(toAdd))) { |
| 196 | errorMessage = "You entered an invalid number - No changes were made"; |
| 197 | break; |
| 198 | } |
| 199 | ticketData.maxTickets = toAdd === "" ? 0 : parseInt(toAdd); |
| 200 | break; |
| 201 | } |
| 202 | case "manageTypes": { |
| 203 | await i.deferUpdate(); |
| 204 | ticketData = await manageTypes(interaction, data.tickets, m); |
| 205 | break; |
pineafan | 41d9356 | 2022-07-30 22:10:15 +0100 | [diff] [blame] | 206 | } |
| 207 | } |
pineafan | 6702cef | 2022-06-13 17:52:37 +0100 | [diff] [blame] | 208 | } |
| 209 | } |
pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame] | 210 | }; |
pineafan | 4f164f3 | 2022-02-26 22:07:12 +0000 | [diff] [blame] | 211 | |
TheCodedProf | 1f67504 | 2023-02-16 17:01:29 -0500 | [diff] [blame^] | 212 | |
| 213 | |
Skyler Grey | 11236ba | 2022-08-08 21:13:33 +0100 | [diff] [blame] | 214 | async function manageTypes(interaction: CommandInteraction, data: GuildConfig["tickets"], m: Message) { |
Skyler Grey | ad00217 | 2022-08-16 18:48:26 +0100 | [diff] [blame] | 215 | let timedOut = false; |
| 216 | let backPressed = false; |
| 217 | while (!timedOut && !backPressed) { |
pineafan | 6702cef | 2022-06-13 17:52:37 +0100 | [diff] [blame] | 218 | if (data.useCustom) { |
pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame] | 219 | const customTypes = data.customTypes; |
pineafan | c6158ab | 2022-06-17 16:34:07 +0100 | [diff] [blame] | 220 | await interaction.editReply({ |
Skyler Grey | 75ea917 | 2022-08-06 10:22:23 +0100 | [diff] [blame] | 221 | embeds: [ |
| 222 | new EmojiEmbed() |
| 223 | .setTitle("Tickets > Types") |
| 224 | .setDescription( |
| 225 | "**Custom types enabled**\n\n" + |
| 226 | "**Types in use:**\n" + |
Skyler Grey | 11236ba | 2022-08-08 21:13:33 +0100 | [diff] [blame] | 227 | (customTypes !== null ? customTypes.map((t) => `> ${t}`).join("\n") : "*None set*") + |
Skyler Grey | 75ea917 | 2022-08-06 10:22:23 +0100 | [diff] [blame] | 228 | "\n\n" + |
| 229 | (customTypes === null |
| 230 | ? `${getEmojiByName( |
| 231 | "TICKETS.REPORT" |
| 232 | )} Having no types will disable tickets. Please add at least 1 type or use default types` |
| 233 | : "") |
pineafan | 6702cef | 2022-06-13 17:52:37 +0100 | [diff] [blame] | 234 | ) |
Skyler Grey | 75ea917 | 2022-08-06 10:22:23 +0100 | [diff] [blame] | 235 | .setStatus("Success") |
| 236 | .setEmoji("GUILD.TICKET.OPEN") |
| 237 | ], |
TheCodedProf | 1f67504 | 2023-02-16 17:01:29 -0500 | [diff] [blame^] | 238 | components: (customTypes && customTypes.length > 0 |
Skyler Grey | 75ea917 | 2022-08-06 10:22:23 +0100 | [diff] [blame] | 239 | ? [ |
TheCodedProf | 59772f8 | 2023-01-18 22:17:16 -0500 | [diff] [blame] | 240 | new ActionRowBuilder<StringSelectMenuBuilder | ButtonBuilder>().addComponents([ |
TheCodedProf | a16d167 | 2023-01-18 18:58:34 -0500 | [diff] [blame] | 241 | new Discord.StringSelectMenuBuilder() |
Skyler Grey | 75ea917 | 2022-08-06 10:22:23 +0100 | [diff] [blame] | 242 | .setCustomId("removeTypes") |
| 243 | .setPlaceholder("Select types to remove") |
| 244 | .setMaxValues(customTypes.length) |
| 245 | .setMinValues(1) |
| 246 | .addOptions( |
| 247 | customTypes.map((t) => ({ |
| 248 | label: t, |
| 249 | value: t |
| 250 | })) |
| 251 | ) |
| 252 | ]) |
| 253 | ] |
| 254 | : [] |
| 255 | ).concat([ |
TheCodedProf | 59772f8 | 2023-01-18 22:17:16 -0500 | [diff] [blame] | 256 | new ActionRowBuilder<ButtonBuilder>().addComponents([ |
TheCodedProf | 21c0859 | 2022-09-13 14:14:43 -0400 | [diff] [blame] | 257 | new ButtonBuilder() |
pineafan | 6702cef | 2022-06-13 17:52:37 +0100 | [diff] [blame] | 258 | .setLabel("Back") |
| 259 | .setEmoji(getEmojiByName("CONTROL.LEFT", "id")) |
TheCodedProf | 21c0859 | 2022-09-13 14:14:43 -0400 | [diff] [blame] | 260 | .setStyle(ButtonStyle.Primary) |
pineafan | 6702cef | 2022-06-13 17:52:37 +0100 | [diff] [blame] | 261 | .setCustomId("back"), |
TheCodedProf | 21c0859 | 2022-09-13 14:14:43 -0400 | [diff] [blame] | 262 | new ButtonBuilder() |
pineafan | 6702cef | 2022-06-13 17:52:37 +0100 | [diff] [blame] | 263 | .setLabel("Add new type") |
Skyler Grey | 11236ba | 2022-08-08 21:13:33 +0100 | [diff] [blame] | 264 | .setEmoji(getEmojiByName("TICKETS.SUGGESTION", "id")) |
TheCodedProf | 21c0859 | 2022-09-13 14:14:43 -0400 | [diff] [blame] | 265 | .setStyle(ButtonStyle.Primary) |
pineafan | 6702cef | 2022-06-13 17:52:37 +0100 | [diff] [blame] | 266 | .setCustomId("addType") |
Skyler Grey | 11236ba | 2022-08-08 21:13:33 +0100 | [diff] [blame] | 267 | .setDisabled(customTypes !== null && customTypes.length >= 25), |
TheCodedProf | 21c0859 | 2022-09-13 14:14:43 -0400 | [diff] [blame] | 268 | new ButtonBuilder() |
pineafan | 6702cef | 2022-06-13 17:52:37 +0100 | [diff] [blame] | 269 | .setLabel("Switch to default types") |
TheCodedProf | 21c0859 | 2022-09-13 14:14:43 -0400 | [diff] [blame] | 270 | .setStyle(ButtonStyle.Secondary) |
pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame] | 271 | .setCustomId("switchToDefault") |
pineafan | 6702cef | 2022-06-13 17:52:37 +0100 | [diff] [blame] | 272 | ]) |
| 273 | ]) |
| 274 | }); |
| 275 | } else { |
pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame] | 276 | const inUse = toHexArray(data.types, ticketTypes); |
TheCodedProf | 59772f8 | 2023-01-18 22:17:16 -0500 | [diff] [blame] | 277 | const options: StringSelectMenuOptionBuilder[] = []; |
Skyler Grey | 75ea917 | 2022-08-06 10:22:23 +0100 | [diff] [blame] | 278 | ticketTypes.forEach((type) => { |
| 279 | options.push( |
TheCodedProf | 59772f8 | 2023-01-18 22:17:16 -0500 | [diff] [blame] | 280 | new StringSelectMenuOptionBuilder({ |
Skyler Grey | 75ea917 | 2022-08-06 10:22:23 +0100 | [diff] [blame] | 281 | label: capitalize(type), |
| 282 | value: type, |
TheCodedProf | 59772f8 | 2023-01-18 22:17:16 -0500 | [diff] [blame] | 283 | emoji: client.emojis.cache.get(getEmojiByName(`TICKETS.${type.toUpperCase()}`, "id")) as APIMessageComponentEmoji, |
Skyler Grey | 75ea917 | 2022-08-06 10:22:23 +0100 | [diff] [blame] | 284 | default: inUse.includes(type) |
| 285 | }) |
| 286 | ); |
pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame] | 287 | }); |
TheCodedProf | 59772f8 | 2023-01-18 22:17:16 -0500 | [diff] [blame] | 288 | const selectPane = new ActionRowBuilder<StringSelectMenuBuilder>().addComponents([ |
TheCodedProf | a16d167 | 2023-01-18 18:58:34 -0500 | [diff] [blame] | 289 | new Discord.StringSelectMenuBuilder() |
pineafan | 6702cef | 2022-06-13 17:52:37 +0100 | [diff] [blame] | 290 | .addOptions(options) |
| 291 | .setCustomId("types") |
| 292 | .setMaxValues(ticketTypes.length) |
| 293 | .setMinValues(1) |
| 294 | .setPlaceholder("Select types to use") |
pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame] | 295 | ]); |
pineafan | c6158ab | 2022-06-17 16:34:07 +0100 | [diff] [blame] | 296 | await interaction.editReply({ |
Skyler Grey | 75ea917 | 2022-08-06 10:22:23 +0100 | [diff] [blame] | 297 | embeds: [ |
| 298 | new EmojiEmbed() |
| 299 | .setTitle("Tickets > Types") |
| 300 | .setDescription( |
| 301 | "**Default types enabled**\n\n" + |
| 302 | "**Types in use:**\n" + |
| 303 | inUse |
Skyler Grey | 11236ba | 2022-08-08 21:13:33 +0100 | [diff] [blame] | 304 | .map((t) => `> ${getEmojiByName("TICKETS." + t.toUpperCase())} ${capitalize(t)}`) |
Skyler Grey | 75ea917 | 2022-08-06 10:22:23 +0100 | [diff] [blame] | 305 | .join("\n") |
| 306 | ) |
| 307 | .setStatus("Success") |
| 308 | .setEmoji("GUILD.TICKET.OPEN") |
| 309 | ], |
| 310 | components: [ |
pineafan | 6702cef | 2022-06-13 17:52:37 +0100 | [diff] [blame] | 311 | selectPane, |
TheCodedProf | 59772f8 | 2023-01-18 22:17:16 -0500 | [diff] [blame] | 312 | new ActionRowBuilder<ButtonBuilder>().addComponents([ |
TheCodedProf | 21c0859 | 2022-09-13 14:14:43 -0400 | [diff] [blame] | 313 | new ButtonBuilder() |
pineafan | 6702cef | 2022-06-13 17:52:37 +0100 | [diff] [blame] | 314 | .setLabel("Back") |
| 315 | .setEmoji(getEmojiByName("CONTROL.LEFT", "id")) |
TheCodedProf | 21c0859 | 2022-09-13 14:14:43 -0400 | [diff] [blame] | 316 | .setStyle(ButtonStyle.Primary) |
pineafan | 6702cef | 2022-06-13 17:52:37 +0100 | [diff] [blame] | 317 | .setCustomId("back"), |
TheCodedProf | 21c0859 | 2022-09-13 14:14:43 -0400 | [diff] [blame] | 318 | new ButtonBuilder() |
pineafan | 6702cef | 2022-06-13 17:52:37 +0100 | [diff] [blame] | 319 | .setLabel("Switch to custom types") |
TheCodedProf | 21c0859 | 2022-09-13 14:14:43 -0400 | [diff] [blame] | 320 | .setStyle(ButtonStyle.Secondary) |
pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame] | 321 | .setCustomId("switchToCustom") |
pineafan | 6702cef | 2022-06-13 17:52:37 +0100 | [diff] [blame] | 322 | ]) |
| 323 | ] |
| 324 | }); |
| 325 | } |
| 326 | let i; |
| 327 | try { |
PineaFan | 0d06edc | 2023-01-17 22:10:31 +0000 | [diff] [blame] | 328 | i = await m.awaitMessageComponent({ |
| 329 | time: 300000, |
TheCodedProf | 267563a | 2023-01-21 17:00:57 -0500 | [diff] [blame] | 330 | filter: (i) => { return i.user.id === interaction.user.id && i.channel!.id === interaction.channel!.id && i.message.id === m.id } |
PineaFan | 0d06edc | 2023-01-17 22:10:31 +0000 | [diff] [blame] | 331 | }); |
Skyler Grey | 75ea917 | 2022-08-06 10:22:23 +0100 | [diff] [blame] | 332 | } catch (e) { |
Skyler Grey | ad00217 | 2022-08-16 18:48:26 +0100 | [diff] [blame] | 333 | timedOut = true; |
| 334 | continue; |
Skyler Grey | 75ea917 | 2022-08-06 10:22:23 +0100 | [diff] [blame] | 335 | } |
TheCodedProf | 4a6d571 | 2023-01-19 15:54:40 -0500 | [diff] [blame] | 336 | if (i.isStringSelectMenu() && i.customId === "types") { |
TheCodedProf | 267563a | 2023-01-21 17:00:57 -0500 | [diff] [blame] | 337 | await i.deferUpdate(); |
TheCodedProf | 4a6d571 | 2023-01-19 15:54:40 -0500 | [diff] [blame] | 338 | const types = toHexInteger(i.values, ticketTypes); |
pineafan | 6702cef | 2022-06-13 17:52:37 +0100 | [diff] [blame] | 339 | data.types = types; |
TheCodedProf | 4a6d571 | 2023-01-19 15:54:40 -0500 | [diff] [blame] | 340 | } else if (i.isStringSelectMenu() && i.customId === "removeTypes") { |
TheCodedProf | 267563a | 2023-01-21 17:00:57 -0500 | [diff] [blame] | 341 | await i.deferUpdate(); |
TheCodedProf | 4a6d571 | 2023-01-19 15:54:40 -0500 | [diff] [blame] | 342 | const types = i.values; |
pineafan | 6702cef | 2022-06-13 17:52:37 +0100 | [diff] [blame] | 343 | let customTypes = data.customTypes; |
| 344 | if (customTypes) { |
| 345 | customTypes = customTypes.filter((t) => !types.includes(t)); |
| 346 | customTypes = customTypes.length > 0 ? customTypes : null; |
pineafan | 6702cef | 2022-06-13 17:52:37 +0100 | [diff] [blame] | 347 | data.customTypes = customTypes; |
| 348 | } |
TheCodedProf | 59772f8 | 2023-01-18 22:17:16 -0500 | [diff] [blame] | 349 | } else if ((i.component as ButtonComponent).customId === "addType") { |
Skyler Grey | 75ea917 | 2022-08-06 10:22:23 +0100 | [diff] [blame] | 350 | await i.showModal( |
TheCodedProf | 59772f8 | 2023-01-18 22:17:16 -0500 | [diff] [blame] | 351 | new Discord.ModalBuilder() |
Skyler Grey | 75ea917 | 2022-08-06 10:22:23 +0100 | [diff] [blame] | 352 | .setCustomId("modal") |
| 353 | .setTitle("Enter a name for the new type") |
| 354 | .addComponents( |
TheCodedProf | 59772f8 | 2023-01-18 22:17:16 -0500 | [diff] [blame] | 355 | new ActionRowBuilder<TextInputBuilder>().addComponents( |
| 356 | new TextInputBuilder() |
Skyler Grey | 75ea917 | 2022-08-06 10:22:23 +0100 | [diff] [blame] | 357 | .setCustomId("type") |
| 358 | .setLabel("Name") |
| 359 | .setMaxLength(100) |
| 360 | .setMinLength(1) |
| 361 | .setPlaceholder('E.g. "Server Idea"') |
| 362 | .setRequired(true) |
TheCodedProf | 59772f8 | 2023-01-18 22:17:16 -0500 | [diff] [blame] | 363 | .setStyle(Discord.TextInputStyle.Short) |
Skyler Grey | 75ea917 | 2022-08-06 10:22:23 +0100 | [diff] [blame] | 364 | ) |
| 365 | ) |
| 366 | ); |
TheCodedProf | 1f67504 | 2023-02-16 17:01:29 -0500 | [diff] [blame^] | 367 | await i.editReply({ |
Skyler Grey | 75ea917 | 2022-08-06 10:22:23 +0100 | [diff] [blame] | 368 | embeds: [ |
| 369 | new EmojiEmbed() |
| 370 | .setTitle("Tickets > Types") |
Skyler Grey | 11236ba | 2022-08-08 21:13:33 +0100 | [diff] [blame] | 371 | .setDescription("Modal opened. If you can't see it, click back and try again.") |
Skyler Grey | 75ea917 | 2022-08-06 10:22:23 +0100 | [diff] [blame] | 372 | .setStatus("Success") |
| 373 | .setEmoji("GUILD.TICKET.OPEN") |
| 374 | ], |
| 375 | components: [ |
TheCodedProf | 59772f8 | 2023-01-18 22:17:16 -0500 | [diff] [blame] | 376 | new ActionRowBuilder<ButtonBuilder>().addComponents([ |
TheCodedProf | 21c0859 | 2022-09-13 14:14:43 -0400 | [diff] [blame] | 377 | new ButtonBuilder() |
Skyler Grey | 75ea917 | 2022-08-06 10:22:23 +0100 | [diff] [blame] | 378 | .setLabel("Back") |
| 379 | .setEmoji(getEmojiByName("CONTROL.LEFT", "id")) |
TheCodedProf | 21c0859 | 2022-09-13 14:14:43 -0400 | [diff] [blame] | 380 | .setStyle(ButtonStyle.Primary) |
Skyler Grey | 75ea917 | 2022-08-06 10:22:23 +0100 | [diff] [blame] | 381 | .setCustomId("back") |
| 382 | ]) |
| 383 | ] |
pineafan | 6702cef | 2022-06-13 17:52:37 +0100 | [diff] [blame] | 384 | }); |
pineafan | 4edb776 | 2022-06-26 19:21:04 +0100 | [diff] [blame] | 385 | let out; |
pineafan | 6702cef | 2022-06-13 17:52:37 +0100 | [diff] [blame] | 386 | try { |
Skyler Grey | 75ea917 | 2022-08-06 10:22:23 +0100 | [diff] [blame] | 387 | out = await modalInteractionCollector( |
| 388 | m, |
TheCodedProf | 1f67504 | 2023-02-16 17:01:29 -0500 | [diff] [blame^] | 389 | (m) => m.user.id === interaction.user.id, |
| 390 | (m) => m.customId === "back" |
Skyler Grey | 75ea917 | 2022-08-06 10:22:23 +0100 | [diff] [blame] | 391 | ); |
| 392 | } catch (e) { |
| 393 | continue; |
| 394 | } |
TheCodedProf | 4a6d571 | 2023-01-19 15:54:40 -0500 | [diff] [blame] | 395 | if (!out || out.isButton()) continue; |
TheCodedProf | 59772f8 | 2023-01-18 22:17:16 -0500 | [diff] [blame] | 396 | out = out as ModalSubmitInteraction; |
PineaFan | 638eb13 | 2023-01-19 10:41:22 +0000 | [diff] [blame] | 397 | let toAdd = out.fields.getTextInputValue("type"); |
| 398 | if (!toAdd) { |
Skyler Grey | 75ea917 | 2022-08-06 10:22:23 +0100 | [diff] [blame] | 399 | continue; |
| 400 | } |
PineaFan | 638eb13 | 2023-01-19 10:41:22 +0000 | [diff] [blame] | 401 | toAdd = toAdd.substring(0, 80); |
| 402 | try { |
TheCodedProf | 1f67504 | 2023-02-16 17:01:29 -0500 | [diff] [blame^] | 403 | if(!data.customTypes) data.customTypes = []; |
| 404 | data.customTypes?.push(toAdd); |
PineaFan | 638eb13 | 2023-01-19 10:41:22 +0000 | [diff] [blame] | 405 | } catch { |
| 406 | continue; |
| 407 | } |
| 408 | data.customTypes = data.customTypes ?? []; |
| 409 | if (!data.customTypes.includes(toAdd)) { |
| 410 | data.customTypes.push(toAdd); |
| 411 | } |
TheCodedProf | 59772f8 | 2023-01-18 22:17:16 -0500 | [diff] [blame] | 412 | } else if ((i.component as ButtonComponent).customId === "switchToDefault") { |
PineaFan | b0d0c24 | 2023-02-05 10:59:45 +0000 | [diff] [blame] | 413 | await i.deferUpdate(); |
TheCodedProf | 59772f8 | 2023-01-18 22:17:16 -0500 | [diff] [blame] | 414 | await client.database.guilds.write(interaction.guild!.id, { "tickets.useCustom": false }, []); |
pineafan | 6702cef | 2022-06-13 17:52:37 +0100 | [diff] [blame] | 415 | data.useCustom = false; |
TheCodedProf | 59772f8 | 2023-01-18 22:17:16 -0500 | [diff] [blame] | 416 | } else if ((i.component as ButtonComponent).customId === "switchToCustom") { |
TheCodedProf | 267563a | 2023-01-21 17:00:57 -0500 | [diff] [blame] | 417 | await i.deferUpdate(); |
TheCodedProf | 59772f8 | 2023-01-18 22:17:16 -0500 | [diff] [blame] | 418 | await client.database.guilds.write(interaction.guild!.id, { "tickets.useCustom": true }, []); |
pineafan | 6702cef | 2022-06-13 17:52:37 +0100 | [diff] [blame] | 419 | data.useCustom = true; |
| 420 | } else { |
TheCodedProf | 267563a | 2023-01-21 17:00:57 -0500 | [diff] [blame] | 421 | await i.deferUpdate(); |
Skyler Grey | ad00217 | 2022-08-16 18:48:26 +0100 | [diff] [blame] | 422 | backPressed = true; |
pineafan | 6702cef | 2022-06-13 17:52:37 +0100 | [diff] [blame] | 423 | } |
| 424 | } |
pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame] | 425 | return data; |
pineafan | 6702cef | 2022-06-13 17:52:37 +0100 | [diff] [blame] | 426 | } |
| 427 | |
TheCodedProf | f86ba09 | 2023-01-27 17:10:07 -0500 | [diff] [blame] | 428 | const check = (interaction: CommandInteraction, _partial: boolean = false) => { |
Skyler Grey | 75ea917 | 2022-08-06 10:22:23 +0100 | [diff] [blame] | 429 | const member = interaction.member as Discord.GuildMember; |
PineaFan | a00db1b | 2023-01-02 15:32:54 +0000 | [diff] [blame] | 430 | if (!member.permissions.has("ManageGuild")) |
PineaFan | 0d06edc | 2023-01-17 22:10:31 +0000 | [diff] [blame] | 431 | return "You must have the *Manage Server* permission to use this command"; |
pineafan | 6702cef | 2022-06-13 17:52:37 +0100 | [diff] [blame] | 432 | return true; |
pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame] | 433 | }; |
pineafan | 4f164f3 | 2022-02-26 22:07:12 +0000 | [diff] [blame] | 434 | |
| 435 | export { command }; |
| 436 | export { callback }; |
Skyler Grey | 1a67e18 | 2022-08-04 23:05:44 +0100 | [diff] [blame] | 437 | export { check }; |