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