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; |
Skyler Grey | 16ecb17 | 2023-03-05 07:30:32 +0000 | [diff] [blame] | 138 | await client.memory.forceUpdate(interaction.guild.id); |
TheCodedProf | 1f67504 | 2023-02-16 17:01:29 -0500 | [diff] [blame] | 139 | break; |
| 140 | } |
| 141 | case "enabled": { |
TheCodedProf | 267563a | 2023-01-21 17:00:57 -0500 | [diff] [blame] | 142 | await i.deferUpdate(); |
TheCodedProf | 1f67504 | 2023-02-16 17:01:29 -0500 | [diff] [blame] | 143 | ticketData.enabled = !ticketData.enabled; |
| 144 | break; |
| 145 | } |
| 146 | case "setMaxTickets": { |
Skyler Grey | 75ea917 | 2022-08-06 10:22:23 +0100 | [diff] [blame] | 147 | await i.showModal( |
TheCodedProf | 1f67504 | 2023-02-16 17:01:29 -0500 | [diff] [blame] | 148 | new ModalBuilder() |
| 149 | .setCustomId("maxTickets") |
| 150 | .setTitle("Set max tickets") |
Skyler Grey | 75ea917 | 2022-08-06 10:22:23 +0100 | [diff] [blame] | 151 | .addComponents( |
TheCodedProf | 1f67504 | 2023-02-16 17:01:29 -0500 | [diff] [blame] | 152 | new ActionRowBuilder<TextInputBuilder>().setComponents( |
TheCodedProf | 59772f8 | 2023-01-18 22:17:16 -0500 | [diff] [blame] | 153 | new TextInputBuilder() |
TheCodedProf | 1f67504 | 2023-02-16 17:01:29 -0500 | [diff] [blame] | 154 | .setLabel("Max tickets - Leave blank for no limit") |
| 155 | .setCustomId("maxTickets") |
| 156 | .setPlaceholder("Enter a number") |
| 157 | .setRequired(false) |
TheCodedProf | 1807fb3 | 2023-02-20 14:33:48 -0500 | [diff] [blame] | 158 | .setValue(ticketData.maxTickets.toString()) |
TheCodedProf | 1f67504 | 2023-02-16 17:01:29 -0500 | [diff] [blame] | 159 | .setMinLength(1) |
| 160 | .setMaxLength(3) |
| 161 | .setStyle(TextInputStyle.Short) |
Skyler Grey | 75ea917 | 2022-08-06 10:22:23 +0100 | [diff] [blame] | 162 | ) |
| 163 | ) |
TheCodedProf | 1f67504 | 2023-02-16 17:01:29 -0500 | [diff] [blame] | 164 | ) |
| 165 | await i.editReply({ |
Skyler Grey | 75ea917 | 2022-08-06 10:22:23 +0100 | [diff] [blame] | 166 | embeds: [ |
| 167 | new EmojiEmbed() |
TheCodedProf | 1f67504 | 2023-02-16 17:01:29 -0500 | [diff] [blame] | 168 | .setTitle("Tickets") |
Skyler Grey | 11236ba | 2022-08-08 21:13:33 +0100 | [diff] [blame] | 169 | .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] | 170 | .setStatus("Success") |
| 171 | .setEmoji("GUILD.TICKET.OPEN") |
| 172 | ], |
| 173 | components: [ |
TheCodedProf | 59772f8 | 2023-01-18 22:17:16 -0500 | [diff] [blame] | 174 | new ActionRowBuilder<ButtonBuilder>().addComponents([ |
TheCodedProf | 21c0859 | 2022-09-13 14:14:43 -0400 | [diff] [blame] | 175 | new ButtonBuilder() |
Skyler Grey | 75ea917 | 2022-08-06 10:22:23 +0100 | [diff] [blame] | 176 | .setLabel("Back") |
Skyler Grey | 11236ba | 2022-08-08 21:13:33 +0100 | [diff] [blame] | 177 | .setEmoji(getEmojiByName("CONTROL.LEFT", "id")) |
TheCodedProf | 21c0859 | 2022-09-13 14:14:43 -0400 | [diff] [blame] | 178 | .setStyle(ButtonStyle.Primary) |
Skyler Grey | 75ea917 | 2022-08-06 10:22:23 +0100 | [diff] [blame] | 179 | .setCustomId("back") |
| 180 | ]) |
| 181 | ] |
pineafan | 41d9356 | 2022-07-30 22:10:15 +0100 | [diff] [blame] | 182 | }); |
| 183 | let out; |
| 184 | try { |
TheCodedProf | 01cba76 | 2023-02-18 15:55:05 -0500 | [diff] [blame] | 185 | out = await modalInteractionCollector(m, interaction.user); |
Skyler Grey | 75ea917 | 2022-08-06 10:22:23 +0100 | [diff] [blame] | 186 | } catch (e) { |
Skyler Grey | ad00217 | 2022-08-16 18:48:26 +0100 | [diff] [blame] | 187 | continue; |
Skyler Grey | 75ea917 | 2022-08-06 10:22:23 +0100 | [diff] [blame] | 188 | } |
TheCodedProf | 1f67504 | 2023-02-16 17:01:29 -0500 | [diff] [blame] | 189 | if (!out || out.isButton()) continue; |
TheCodedProf | 59772f8 | 2023-01-18 22:17:16 -0500 | [diff] [blame] | 190 | out = out as ModalSubmitInteraction; |
TheCodedProf | 1807fb3 | 2023-02-20 14:33:48 -0500 | [diff] [blame] | 191 | const toAdd = out.fields.getTextInputValue("maxTickets"); |
TheCodedProf | 1f67504 | 2023-02-16 17:01:29 -0500 | [diff] [blame] | 192 | if(isNaN(parseInt(toAdd))) { |
| 193 | errorMessage = "You entered an invalid number - No changes were made"; |
| 194 | break; |
| 195 | } |
| 196 | ticketData.maxTickets = toAdd === "" ? 0 : parseInt(toAdd); |
| 197 | break; |
| 198 | } |
| 199 | case "manageTypes": { |
| 200 | await i.deferUpdate(); |
| 201 | ticketData = await manageTypes(interaction, data.tickets, m); |
| 202 | break; |
pineafan | 41d9356 | 2022-07-30 22:10:15 +0100 | [diff] [blame] | 203 | } |
| 204 | } |
pineafan | 6702cef | 2022-06-13 17:52:37 +0100 | [diff] [blame] | 205 | } |
| 206 | } |
TheCodedProf | 01cba76 | 2023-02-18 15:55:05 -0500 | [diff] [blame] | 207 | await interaction.deleteReply() |
pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame] | 208 | }; |
pineafan | 4f164f3 | 2022-02-26 22:07:12 +0000 | [diff] [blame] | 209 | |
TheCodedProf | 1f67504 | 2023-02-16 17:01:29 -0500 | [diff] [blame] | 210 | |
| 211 | |
Skyler Grey | 11236ba | 2022-08-08 21:13:33 +0100 | [diff] [blame] | 212 | async function manageTypes(interaction: CommandInteraction, data: GuildConfig["tickets"], m: Message) { |
Skyler Grey | ad00217 | 2022-08-16 18:48:26 +0100 | [diff] [blame] | 213 | let timedOut = false; |
| 214 | let backPressed = false; |
| 215 | while (!timedOut && !backPressed) { |
pineafan | 6702cef | 2022-06-13 17:52:37 +0100 | [diff] [blame] | 216 | if (data.useCustom) { |
pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame] | 217 | const customTypes = data.customTypes; |
pineafan | c6158ab | 2022-06-17 16:34:07 +0100 | [diff] [blame] | 218 | await interaction.editReply({ |
Skyler Grey | 75ea917 | 2022-08-06 10:22:23 +0100 | [diff] [blame] | 219 | embeds: [ |
| 220 | new EmojiEmbed() |
| 221 | .setTitle("Tickets > Types") |
| 222 | .setDescription( |
| 223 | "**Custom types enabled**\n\n" + |
| 224 | "**Types in use:**\n" + |
Skyler Grey | 11236ba | 2022-08-08 21:13:33 +0100 | [diff] [blame] | 225 | (customTypes !== null ? customTypes.map((t) => `> ${t}`).join("\n") : "*None set*") + |
Skyler Grey | 75ea917 | 2022-08-06 10:22:23 +0100 | [diff] [blame] | 226 | "\n\n" + |
| 227 | (customTypes === null |
| 228 | ? `${getEmojiByName( |
| 229 | "TICKETS.REPORT" |
| 230 | )} Having no types will disable tickets. Please add at least 1 type or use default types` |
| 231 | : "") |
pineafan | 6702cef | 2022-06-13 17:52:37 +0100 | [diff] [blame] | 232 | ) |
Skyler Grey | 75ea917 | 2022-08-06 10:22:23 +0100 | [diff] [blame] | 233 | .setStatus("Success") |
| 234 | .setEmoji("GUILD.TICKET.OPEN") |
| 235 | ], |
TheCodedProf | 1f67504 | 2023-02-16 17:01:29 -0500 | [diff] [blame] | 236 | components: (customTypes && customTypes.length > 0 |
Skyler Grey | 75ea917 | 2022-08-06 10:22:23 +0100 | [diff] [blame] | 237 | ? [ |
TheCodedProf | 59772f8 | 2023-01-18 22:17:16 -0500 | [diff] [blame] | 238 | new ActionRowBuilder<StringSelectMenuBuilder | ButtonBuilder>().addComponents([ |
TheCodedProf | a16d167 | 2023-01-18 18:58:34 -0500 | [diff] [blame] | 239 | new Discord.StringSelectMenuBuilder() |
Skyler Grey | 75ea917 | 2022-08-06 10:22:23 +0100 | [diff] [blame] | 240 | .setCustomId("removeTypes") |
| 241 | .setPlaceholder("Select types to remove") |
| 242 | .setMaxValues(customTypes.length) |
| 243 | .setMinValues(1) |
| 244 | .addOptions( |
| 245 | customTypes.map((t) => ({ |
| 246 | label: t, |
| 247 | value: t |
| 248 | })) |
| 249 | ) |
| 250 | ]) |
| 251 | ] |
| 252 | : [] |
| 253 | ).concat([ |
TheCodedProf | 59772f8 | 2023-01-18 22:17:16 -0500 | [diff] [blame] | 254 | new ActionRowBuilder<ButtonBuilder>().addComponents([ |
TheCodedProf | 21c0859 | 2022-09-13 14:14:43 -0400 | [diff] [blame] | 255 | new ButtonBuilder() |
pineafan | 6702cef | 2022-06-13 17:52:37 +0100 | [diff] [blame] | 256 | .setLabel("Back") |
| 257 | .setEmoji(getEmojiByName("CONTROL.LEFT", "id")) |
TheCodedProf | 21c0859 | 2022-09-13 14:14:43 -0400 | [diff] [blame] | 258 | .setStyle(ButtonStyle.Primary) |
pineafan | 6702cef | 2022-06-13 17:52:37 +0100 | [diff] [blame] | 259 | .setCustomId("back"), |
TheCodedProf | 21c0859 | 2022-09-13 14:14:43 -0400 | [diff] [blame] | 260 | new ButtonBuilder() |
pineafan | 6702cef | 2022-06-13 17:52:37 +0100 | [diff] [blame] | 261 | .setLabel("Add new type") |
Skyler Grey | 11236ba | 2022-08-08 21:13:33 +0100 | [diff] [blame] | 262 | .setEmoji(getEmojiByName("TICKETS.SUGGESTION", "id")) |
TheCodedProf | 21c0859 | 2022-09-13 14:14:43 -0400 | [diff] [blame] | 263 | .setStyle(ButtonStyle.Primary) |
pineafan | 6702cef | 2022-06-13 17:52:37 +0100 | [diff] [blame] | 264 | .setCustomId("addType") |
Skyler Grey | 11236ba | 2022-08-08 21:13:33 +0100 | [diff] [blame] | 265 | .setDisabled(customTypes !== null && customTypes.length >= 25), |
TheCodedProf | 21c0859 | 2022-09-13 14:14:43 -0400 | [diff] [blame] | 266 | new ButtonBuilder() |
pineafan | 6702cef | 2022-06-13 17:52:37 +0100 | [diff] [blame] | 267 | .setLabel("Switch to default types") |
TheCodedProf | 21c0859 | 2022-09-13 14:14:43 -0400 | [diff] [blame] | 268 | .setStyle(ButtonStyle.Secondary) |
pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame] | 269 | .setCustomId("switchToDefault") |
pineafan | 6702cef | 2022-06-13 17:52:37 +0100 | [diff] [blame] | 270 | ]) |
| 271 | ]) |
| 272 | }); |
| 273 | } else { |
pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame] | 274 | const inUse = toHexArray(data.types, ticketTypes); |
TheCodedProf | 59772f8 | 2023-01-18 22:17:16 -0500 | [diff] [blame] | 275 | const options: StringSelectMenuOptionBuilder[] = []; |
Skyler Grey | 75ea917 | 2022-08-06 10:22:23 +0100 | [diff] [blame] | 276 | ticketTypes.forEach((type) => { |
| 277 | options.push( |
TheCodedProf | 59772f8 | 2023-01-18 22:17:16 -0500 | [diff] [blame] | 278 | new StringSelectMenuOptionBuilder({ |
Skyler Grey | 75ea917 | 2022-08-06 10:22:23 +0100 | [diff] [blame] | 279 | label: capitalize(type), |
| 280 | value: type, |
TheCodedProf | 59772f8 | 2023-01-18 22:17:16 -0500 | [diff] [blame] | 281 | emoji: client.emojis.cache.get(getEmojiByName(`TICKETS.${type.toUpperCase()}`, "id")) as APIMessageComponentEmoji, |
Skyler Grey | 75ea917 | 2022-08-06 10:22:23 +0100 | [diff] [blame] | 282 | default: inUse.includes(type) |
| 283 | }) |
| 284 | ); |
pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame] | 285 | }); |
TheCodedProf | 59772f8 | 2023-01-18 22:17:16 -0500 | [diff] [blame] | 286 | const selectPane = new ActionRowBuilder<StringSelectMenuBuilder>().addComponents([ |
TheCodedProf | a16d167 | 2023-01-18 18:58:34 -0500 | [diff] [blame] | 287 | new Discord.StringSelectMenuBuilder() |
pineafan | 6702cef | 2022-06-13 17:52:37 +0100 | [diff] [blame] | 288 | .addOptions(options) |
| 289 | .setCustomId("types") |
| 290 | .setMaxValues(ticketTypes.length) |
| 291 | .setMinValues(1) |
| 292 | .setPlaceholder("Select types to use") |
pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame] | 293 | ]); |
pineafan | c6158ab | 2022-06-17 16:34:07 +0100 | [diff] [blame] | 294 | await interaction.editReply({ |
Skyler Grey | 75ea917 | 2022-08-06 10:22:23 +0100 | [diff] [blame] | 295 | embeds: [ |
| 296 | new EmojiEmbed() |
| 297 | .setTitle("Tickets > Types") |
| 298 | .setDescription( |
| 299 | "**Default types enabled**\n\n" + |
| 300 | "**Types in use:**\n" + |
| 301 | inUse |
Skyler Grey | 11236ba | 2022-08-08 21:13:33 +0100 | [diff] [blame] | 302 | .map((t) => `> ${getEmojiByName("TICKETS." + t.toUpperCase())} ${capitalize(t)}`) |
Skyler Grey | 75ea917 | 2022-08-06 10:22:23 +0100 | [diff] [blame] | 303 | .join("\n") |
| 304 | ) |
| 305 | .setStatus("Success") |
| 306 | .setEmoji("GUILD.TICKET.OPEN") |
| 307 | ], |
| 308 | components: [ |
pineafan | 6702cef | 2022-06-13 17:52:37 +0100 | [diff] [blame] | 309 | selectPane, |
TheCodedProf | 59772f8 | 2023-01-18 22:17:16 -0500 | [diff] [blame] | 310 | new ActionRowBuilder<ButtonBuilder>().addComponents([ |
TheCodedProf | 21c0859 | 2022-09-13 14:14:43 -0400 | [diff] [blame] | 311 | new ButtonBuilder() |
pineafan | 6702cef | 2022-06-13 17:52:37 +0100 | [diff] [blame] | 312 | .setLabel("Back") |
| 313 | .setEmoji(getEmojiByName("CONTROL.LEFT", "id")) |
TheCodedProf | 21c0859 | 2022-09-13 14:14:43 -0400 | [diff] [blame] | 314 | .setStyle(ButtonStyle.Primary) |
pineafan | 6702cef | 2022-06-13 17:52:37 +0100 | [diff] [blame] | 315 | .setCustomId("back"), |
TheCodedProf | 21c0859 | 2022-09-13 14:14:43 -0400 | [diff] [blame] | 316 | new ButtonBuilder() |
pineafan | 6702cef | 2022-06-13 17:52:37 +0100 | [diff] [blame] | 317 | .setLabel("Switch to custom types") |
TheCodedProf | 21c0859 | 2022-09-13 14:14:43 -0400 | [diff] [blame] | 318 | .setStyle(ButtonStyle.Secondary) |
pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame] | 319 | .setCustomId("switchToCustom") |
pineafan | 6702cef | 2022-06-13 17:52:37 +0100 | [diff] [blame] | 320 | ]) |
| 321 | ] |
| 322 | }); |
| 323 | } |
| 324 | let i; |
| 325 | try { |
PineaFan | 0d06edc | 2023-01-17 22:10:31 +0000 | [diff] [blame] | 326 | i = await m.awaitMessageComponent({ |
| 327 | time: 300000, |
TheCodedProf | 267563a | 2023-01-21 17:00:57 -0500 | [diff] [blame] | 328 | 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] | 329 | }); |
Skyler Grey | 75ea917 | 2022-08-06 10:22:23 +0100 | [diff] [blame] | 330 | } catch (e) { |
Skyler Grey | ad00217 | 2022-08-16 18:48:26 +0100 | [diff] [blame] | 331 | timedOut = true; |
| 332 | continue; |
Skyler Grey | 75ea917 | 2022-08-06 10:22:23 +0100 | [diff] [blame] | 333 | } |
TheCodedProf | 4a6d571 | 2023-01-19 15:54:40 -0500 | [diff] [blame] | 334 | if (i.isStringSelectMenu() && i.customId === "types") { |
TheCodedProf | 267563a | 2023-01-21 17:00:57 -0500 | [diff] [blame] | 335 | await i.deferUpdate(); |
TheCodedProf | 4a6d571 | 2023-01-19 15:54:40 -0500 | [diff] [blame] | 336 | const types = toHexInteger(i.values, ticketTypes); |
pineafan | 6702cef | 2022-06-13 17:52:37 +0100 | [diff] [blame] | 337 | data.types = types; |
TheCodedProf | 4a6d571 | 2023-01-19 15:54:40 -0500 | [diff] [blame] | 338 | } else if (i.isStringSelectMenu() && i.customId === "removeTypes") { |
TheCodedProf | 267563a | 2023-01-21 17:00:57 -0500 | [diff] [blame] | 339 | await i.deferUpdate(); |
TheCodedProf | 4a6d571 | 2023-01-19 15:54:40 -0500 | [diff] [blame] | 340 | const types = i.values; |
pineafan | 6702cef | 2022-06-13 17:52:37 +0100 | [diff] [blame] | 341 | let customTypes = data.customTypes; |
| 342 | if (customTypes) { |
| 343 | customTypes = customTypes.filter((t) => !types.includes(t)); |
| 344 | customTypes = customTypes.length > 0 ? customTypes : null; |
pineafan | 6702cef | 2022-06-13 17:52:37 +0100 | [diff] [blame] | 345 | data.customTypes = customTypes; |
| 346 | } |
TheCodedProf | 59772f8 | 2023-01-18 22:17:16 -0500 | [diff] [blame] | 347 | } else if ((i.component as ButtonComponent).customId === "addType") { |
Skyler Grey | 75ea917 | 2022-08-06 10:22:23 +0100 | [diff] [blame] | 348 | await i.showModal( |
TheCodedProf | 59772f8 | 2023-01-18 22:17:16 -0500 | [diff] [blame] | 349 | new Discord.ModalBuilder() |
Skyler Grey | 75ea917 | 2022-08-06 10:22:23 +0100 | [diff] [blame] | 350 | .setCustomId("modal") |
| 351 | .setTitle("Enter a name for the new type") |
| 352 | .addComponents( |
TheCodedProf | 59772f8 | 2023-01-18 22:17:16 -0500 | [diff] [blame] | 353 | new ActionRowBuilder<TextInputBuilder>().addComponents( |
| 354 | new TextInputBuilder() |
Skyler Grey | 75ea917 | 2022-08-06 10:22:23 +0100 | [diff] [blame] | 355 | .setCustomId("type") |
| 356 | .setLabel("Name") |
| 357 | .setMaxLength(100) |
| 358 | .setMinLength(1) |
| 359 | .setPlaceholder('E.g. "Server Idea"') |
| 360 | .setRequired(true) |
TheCodedProf | 59772f8 | 2023-01-18 22:17:16 -0500 | [diff] [blame] | 361 | .setStyle(Discord.TextInputStyle.Short) |
Skyler Grey | 75ea917 | 2022-08-06 10:22:23 +0100 | [diff] [blame] | 362 | ) |
| 363 | ) |
| 364 | ); |
TheCodedProf | 1f67504 | 2023-02-16 17:01:29 -0500 | [diff] [blame] | 365 | await i.editReply({ |
Skyler Grey | 75ea917 | 2022-08-06 10:22:23 +0100 | [diff] [blame] | 366 | embeds: [ |
| 367 | new EmojiEmbed() |
| 368 | .setTitle("Tickets > Types") |
Skyler Grey | 11236ba | 2022-08-08 21:13:33 +0100 | [diff] [blame] | 369 | .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] | 370 | .setStatus("Success") |
| 371 | .setEmoji("GUILD.TICKET.OPEN") |
| 372 | ], |
| 373 | components: [ |
TheCodedProf | 59772f8 | 2023-01-18 22:17:16 -0500 | [diff] [blame] | 374 | new ActionRowBuilder<ButtonBuilder>().addComponents([ |
TheCodedProf | 21c0859 | 2022-09-13 14:14:43 -0400 | [diff] [blame] | 375 | new ButtonBuilder() |
Skyler Grey | 75ea917 | 2022-08-06 10:22:23 +0100 | [diff] [blame] | 376 | .setLabel("Back") |
| 377 | .setEmoji(getEmojiByName("CONTROL.LEFT", "id")) |
TheCodedProf | 21c0859 | 2022-09-13 14:14:43 -0400 | [diff] [blame] | 378 | .setStyle(ButtonStyle.Primary) |
Skyler Grey | 75ea917 | 2022-08-06 10:22:23 +0100 | [diff] [blame] | 379 | .setCustomId("back") |
| 380 | ]) |
| 381 | ] |
pineafan | 6702cef | 2022-06-13 17:52:37 +0100 | [diff] [blame] | 382 | }); |
pineafan | 4edb776 | 2022-06-26 19:21:04 +0100 | [diff] [blame] | 383 | let out; |
pineafan | 6702cef | 2022-06-13 17:52:37 +0100 | [diff] [blame] | 384 | try { |
TheCodedProf | 01cba76 | 2023-02-18 15:55:05 -0500 | [diff] [blame] | 385 | out = await modalInteractionCollector(m, interaction.user); |
Skyler Grey | 75ea917 | 2022-08-06 10:22:23 +0100 | [diff] [blame] | 386 | } catch (e) { |
| 387 | continue; |
| 388 | } |
TheCodedProf | 4a6d571 | 2023-01-19 15:54:40 -0500 | [diff] [blame] | 389 | if (!out || out.isButton()) continue; |
TheCodedProf | 59772f8 | 2023-01-18 22:17:16 -0500 | [diff] [blame] | 390 | out = out as ModalSubmitInteraction; |
PineaFan | 638eb13 | 2023-01-19 10:41:22 +0000 | [diff] [blame] | 391 | let toAdd = out.fields.getTextInputValue("type"); |
| 392 | if (!toAdd) { |
Skyler Grey | 75ea917 | 2022-08-06 10:22:23 +0100 | [diff] [blame] | 393 | continue; |
| 394 | } |
PineaFan | 638eb13 | 2023-01-19 10:41:22 +0000 | [diff] [blame] | 395 | toAdd = toAdd.substring(0, 80); |
| 396 | try { |
TheCodedProf | 1f67504 | 2023-02-16 17:01:29 -0500 | [diff] [blame] | 397 | if(!data.customTypes) data.customTypes = []; |
TheCodedProf | 1807fb3 | 2023-02-20 14:33:48 -0500 | [diff] [blame] | 398 | data.customTypes.push(toAdd); |
PineaFan | 638eb13 | 2023-01-19 10:41:22 +0000 | [diff] [blame] | 399 | } catch { |
| 400 | continue; |
| 401 | } |
PineaFan | 638eb13 | 2023-01-19 10:41:22 +0000 | [diff] [blame] | 402 | if (!data.customTypes.includes(toAdd)) { |
| 403 | data.customTypes.push(toAdd); |
| 404 | } |
TheCodedProf | 59772f8 | 2023-01-18 22:17:16 -0500 | [diff] [blame] | 405 | } else if ((i.component as ButtonComponent).customId === "switchToDefault") { |
PineaFan | b0d0c24 | 2023-02-05 10:59:45 +0000 | [diff] [blame] | 406 | await i.deferUpdate(); |
TheCodedProf | 59772f8 | 2023-01-18 22:17:16 -0500 | [diff] [blame] | 407 | await client.database.guilds.write(interaction.guild!.id, { "tickets.useCustom": false }, []); |
pineafan | 6702cef | 2022-06-13 17:52:37 +0100 | [diff] [blame] | 408 | data.useCustom = false; |
TheCodedProf | 59772f8 | 2023-01-18 22:17:16 -0500 | [diff] [blame] | 409 | } else if ((i.component as ButtonComponent).customId === "switchToCustom") { |
TheCodedProf | 267563a | 2023-01-21 17:00:57 -0500 | [diff] [blame] | 410 | await i.deferUpdate(); |
TheCodedProf | 59772f8 | 2023-01-18 22:17:16 -0500 | [diff] [blame] | 411 | await client.database.guilds.write(interaction.guild!.id, { "tickets.useCustom": true }, []); |
pineafan | 6702cef | 2022-06-13 17:52:37 +0100 | [diff] [blame] | 412 | data.useCustom = true; |
| 413 | } else { |
TheCodedProf | 267563a | 2023-01-21 17:00:57 -0500 | [diff] [blame] | 414 | await i.deferUpdate(); |
Skyler Grey | ad00217 | 2022-08-16 18:48:26 +0100 | [diff] [blame] | 415 | backPressed = true; |
pineafan | 6702cef | 2022-06-13 17:52:37 +0100 | [diff] [blame] | 416 | } |
| 417 | } |
pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame] | 418 | return data; |
pineafan | 6702cef | 2022-06-13 17:52:37 +0100 | [diff] [blame] | 419 | } |
| 420 | |
TheCodedProf | f86ba09 | 2023-01-27 17:10:07 -0500 | [diff] [blame] | 421 | const check = (interaction: CommandInteraction, _partial: boolean = false) => { |
Skyler Grey | 75ea917 | 2022-08-06 10:22:23 +0100 | [diff] [blame] | 422 | const member = interaction.member as Discord.GuildMember; |
PineaFan | a00db1b | 2023-01-02 15:32:54 +0000 | [diff] [blame] | 423 | if (!member.permissions.has("ManageGuild")) |
PineaFan | 0d06edc | 2023-01-17 22:10:31 +0000 | [diff] [blame] | 424 | return "You must have the *Manage Server* permission to use this command"; |
pineafan | 6702cef | 2022-06-13 17:52:37 +0100 | [diff] [blame] | 425 | return true; |
pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame] | 426 | }; |
pineafan | 4f164f3 | 2022-02-26 22:07:12 +0000 | [diff] [blame] | 427 | |
| 428 | export { command }; |
| 429 | export { callback }; |
Skyler Grey | 1a67e18 | 2022-08-04 23:05:44 +0100 | [diff] [blame] | 430 | export { check }; |