pineafan | e23c4ec | 2022-07-27 21:56:27 +0100 | [diff] [blame] | 1 | import { LoadingEmbed } from './../../utils/defaultEmbeds.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"; |
pineafan | 6702cef | 2022-06-13 17:52:37 +0100 | [diff] [blame] | 4 | import confirmationMessage from "../../utils/confirmationMessage.js"; |
| 5 | import Discord, { CommandInteraction, MessageActionRow, MessageButton, MessageSelectMenu, TextInputComponent } from "discord.js"; |
| 6 | import { SelectMenuOption, SlashCommandSubcommandBuilder } from "@discordjs/builders"; |
pineafan | 4f164f3 | 2022-02-26 22:07:12 +0000 | [diff] [blame] | 7 | import { WrappedCheck } from "jshaiku"; |
pineafan | 1dc1572 | 2022-03-14 21:27:34 +0000 | [diff] [blame] | 8 | import { ChannelType } from 'discord-api-types'; |
pineafan | 6702cef | 2022-06-13 17:52:37 +0100 | [diff] [blame] | 9 | import client from "../../utils/client.js"; |
| 10 | import { toHexInteger, toHexArray, tickets as ticketTypes } from "../../utils/calculate.js"; |
| 11 | import { capitalize } from '../../utils/generateKeyValueList.js'; |
| 12 | import { modalInteractionCollector } from "../../utils/dualCollector.js"; |
pineafan | 4f164f3 | 2022-02-26 22:07:12 +0000 | [diff] [blame] | 13 | |
pineafan | 6702cef | 2022-06-13 17:52:37 +0100 | [diff] [blame] | 14 | const command = (builder: SlashCommandSubcommandBuilder) => builder |
pineafan | 4f164f3 | 2022-02-26 22:07:12 +0000 | [diff] [blame] | 15 | .setName("tickets") |
pineafan | 6702cef | 2022-06-13 17:52:37 +0100 | [diff] [blame] | 16 | .setDescription("Shows settings for tickets | Use no arguments to manage custom types") |
| 17 | .addStringOption(option => option.setName("enabled").setDescription("If users should be able to create tickets").setRequired(false) |
pineafan | 1dc1572 | 2022-03-14 21:27:34 +0000 | [diff] [blame] | 18 | .addChoices([["Yes", "yes"], ["No", "no"]])) |
| 19 | .addChannelOption(option => option.setName("category").setDescription("The category where tickets are created").addChannelType(ChannelType.GuildCategory).setRequired(false)) |
pineafan | 73a7c4a | 2022-07-24 10:38:04 +0100 | [diff] [blame] | 20 | .addNumberOption(option => option.setName("maxticketsperuser").setDescription("The maximum amount of tickets a user can create | Default: 5").setRequired(false).setMinValue(1)) |
pineafan | 6702cef | 2022-06-13 17:52:37 +0100 | [diff] [blame] | 21 | .addRoleOption(option => option.setName("supportrole").setDescription("This role will have view access to all tickets and will be pinged when a ticket is created").setRequired(false)) |
pineafan | 4f164f3 | 2022-02-26 22:07:12 +0000 | [diff] [blame] | 22 | |
pineafan | 6702cef | 2022-06-13 17:52:37 +0100 | [diff] [blame] | 23 | const callback = async (interaction: CommandInteraction): Promise<any> => { |
| 24 | let m; |
pineafan | e23c4ec | 2022-07-27 21:56:27 +0100 | [diff] [blame] | 25 | m = await interaction.reply({embeds: LoadingEmbed, ephemeral: true, fetchReply: true}); |
pineafan | 6702cef | 2022-06-13 17:52:37 +0100 | [diff] [blame] | 26 | let options = { |
| 27 | enabled: interaction.options.getString("enabled") as string | boolean, |
| 28 | category: interaction.options.getChannel("category"), |
| 29 | maxtickets: interaction.options.getNumber("maxticketsperuser"), |
| 30 | supportping: interaction.options.getRole("supportrole") |
| 31 | } |
| 32 | if (options.enabled !== null || options.category || options.maxtickets || options.supportping) { |
| 33 | options.enabled = options.enabled === "yes" ? true : false; |
| 34 | if (options.category) { |
| 35 | let channel |
| 36 | try { |
| 37 | channel = interaction.guild.channels.cache.get(options.category.id) |
| 38 | } catch { |
| 39 | return await interaction.editReply({ |
pineafan | 4edb776 | 2022-06-26 19:21:04 +0100 | [diff] [blame] | 40 | embeds: [new EmojiEmbed() |
pineafan | 6702cef | 2022-06-13 17:52:37 +0100 | [diff] [blame] | 41 | .setEmoji("CHANNEL.TEXT.DELETE") |
| 42 | .setTitle("Tickets > Category") |
| 43 | .setDescription("The channel you provided is not a valid category") |
| 44 | .setStatus("Danger") |
| 45 | ] |
| 46 | }) |
| 47 | } |
| 48 | channel = channel as Discord.CategoryChannel |
pineafan | e23c4ec | 2022-07-27 21:56:27 +0100 | [diff] [blame] | 49 | if (channel.guild.id !== interaction.guild.id) return interaction.editReply({ |
pineafan | 4edb776 | 2022-06-26 19:21:04 +0100 | [diff] [blame] | 50 | embeds: [new EmojiEmbed() |
pineafan | 6702cef | 2022-06-13 17:52:37 +0100 | [diff] [blame] | 51 | .setTitle("Tickets > Category") |
| 52 | .setDescription(`You must choose a category in this server`) |
| 53 | .setStatus("Danger") |
| 54 | .setEmoji("CHANNEL.TEXT.DELETE") |
| 55 | ] |
| 56 | }); |
| 57 | } |
| 58 | if (options.maxtickets) { |
| 59 | if (options.maxtickets < 1) return interaction.editReply({ |
pineafan | 4edb776 | 2022-06-26 19:21:04 +0100 | [diff] [blame] | 60 | embeds: [new EmojiEmbed() |
pineafan | 6702cef | 2022-06-13 17:52:37 +0100 | [diff] [blame] | 61 | .setTitle("Tickets > Max Tickets") |
| 62 | .setDescription(`You must choose a number greater than 0`) |
| 63 | .setStatus("Danger") |
| 64 | .setEmoji("CHANNEL.TEXT.DELETE") |
| 65 | ] |
| 66 | }); |
| 67 | } |
| 68 | let role |
| 69 | if (options.supportping) { |
| 70 | try { |
| 71 | role = interaction.guild.roles.cache.get(options.supportping.id) |
| 72 | } catch { |
| 73 | return await interaction.editReply({ |
pineafan | 4edb776 | 2022-06-26 19:21:04 +0100 | [diff] [blame] | 74 | embeds: [new EmojiEmbed() |
pineafan | 6702cef | 2022-06-13 17:52:37 +0100 | [diff] [blame] | 75 | .setEmoji("GUILD.ROLE.DELETE") |
| 76 | .setTitle("Tickets > Support Ping") |
| 77 | .setDescription("The role you provided is not a valid role") |
| 78 | .setStatus("Danger") |
| 79 | ] |
| 80 | }) |
| 81 | } |
| 82 | role = role as Discord.Role |
pineafan | e23c4ec | 2022-07-27 21:56:27 +0100 | [diff] [blame] | 83 | if (role.guild.id !== interaction.guild.id) return interaction.editReply({ |
pineafan | 4edb776 | 2022-06-26 19:21:04 +0100 | [diff] [blame] | 84 | embeds: [new EmojiEmbed() |
pineafan | 6702cef | 2022-06-13 17:52:37 +0100 | [diff] [blame] | 85 | .setTitle("Tickets > Support Ping") |
| 86 | .setDescription(`You must choose a role in this server`) |
| 87 | .setStatus("Danger") |
| 88 | .setEmoji("GUILD.ROLE.DELETE") |
| 89 | ] |
| 90 | }); |
| 91 | } |
| 92 | |
| 93 | let confirmation = await new confirmationMessage(interaction) |
| 94 | .setEmoji("GUILD.TICKET.ARCHIVED") |
| 95 | .setTitle("Tickets") |
| 96 | .setDescription( |
| 97 | (options.category ? `**Category:** ${options.category.name}\n` : "") + |
| 98 | (options.maxtickets ? `**Max Tickets:** ${options.maxtickets}\n` : "") + |
| 99 | (options.supportping ? `**Support Ping:** ${options.supportping.name}\n` : "") + |
| 100 | (options.enabled !== null ? `**Enabled:** ${options.enabled ? `${getEmojiByName("CONTROL.TICK")} Yes` : `${getEmojiByName("CONTROL.CROSS")} No` |
| 101 | }\n` : "") + |
| 102 | `\nAre you sure you want to apply these settings?` |
| 103 | ) |
| 104 | .setColor("Warning") |
| 105 | .setInverted(true) |
| 106 | .send(true) |
pineafan | 02ba023 | 2022-07-24 22:16:15 +0100 | [diff] [blame] | 107 | if (confirmation.cancelled) return |
pineafan | 6702cef | 2022-06-13 17:52:37 +0100 | [diff] [blame] | 108 | if (confirmation.success) { |
| 109 | let toUpdate = {} |
| 110 | if (options.enabled !== null) toUpdate["tickets.enabled"] = options.enabled |
| 111 | if (options.category) toUpdate["tickets.category"] = options.category.id |
| 112 | if (options.maxtickets) toUpdate["tickets.maxTickets"] = options.maxtickets |
| 113 | if (options.supportping) toUpdate["tickets.supportRole"] = options.supportping.id |
| 114 | try { |
pineafan | 4edb776 | 2022-06-26 19:21:04 +0100 | [diff] [blame] | 115 | await client.database.guilds.write(interaction.guild.id, toUpdate) |
pineafan | 6702cef | 2022-06-13 17:52:37 +0100 | [diff] [blame] | 116 | } catch (e) { |
| 117 | return interaction.editReply({ |
pineafan | 4edb776 | 2022-06-26 19:21:04 +0100 | [diff] [blame] | 118 | embeds: [new EmojiEmbed() |
pineafan | 6702cef | 2022-06-13 17:52:37 +0100 | [diff] [blame] | 119 | .setTitle("Tickets") |
| 120 | .setDescription(`Something went wrong and the staff notifications channel could not be set`) |
| 121 | .setStatus("Danger") |
| 122 | .setEmoji("GUILD.TICKET.DELETE") |
| 123 | ], components: [] |
| 124 | }); |
| 125 | } |
| 126 | } else { |
| 127 | return interaction.editReply({ |
pineafan | 4edb776 | 2022-06-26 19:21:04 +0100 | [diff] [blame] | 128 | embeds: [new EmojiEmbed() |
pineafan | 6702cef | 2022-06-13 17:52:37 +0100 | [diff] [blame] | 129 | .setTitle("Tickets") |
| 130 | .setDescription(`No changes were made`) |
| 131 | .setStatus("Success") |
| 132 | .setEmoji("GUILD.TICKET.OPEN") |
| 133 | ], components: [] |
| 134 | }); |
| 135 | } |
| 136 | } |
pineafan | 4edb776 | 2022-06-26 19:21:04 +0100 | [diff] [blame] | 137 | let data = await client.database.guilds.read(interaction.guild.id); |
pineafan | 73a7c4a | 2022-07-24 10:38:04 +0100 | [diff] [blame] | 138 | data.tickets.customTypes = (data.tickets.customTypes || []).filter((v, i, a) => a.indexOf(v) === i) |
pineafan | 6702cef | 2022-06-13 17:52:37 +0100 | [diff] [blame] | 139 | let lastClicked = ""; |
| 140 | let embed; |
| 141 | data = { |
| 142 | enabled: data.tickets.enabled, |
| 143 | category: data.tickets.category, |
| 144 | maxTickets: data.tickets.maxTickets, |
| 145 | supportRole: data.tickets.supportRole, |
| 146 | useCustom: data.tickets.useCustom, |
| 147 | types: data.tickets.types, |
| 148 | customTypes: data.tickets.customTypes |
| 149 | } |
| 150 | while (true) { |
pineafan | 4edb776 | 2022-06-26 19:21:04 +0100 | [diff] [blame] | 151 | embed = new EmojiEmbed() |
pineafan | 6702cef | 2022-06-13 17:52:37 +0100 | [diff] [blame] | 152 | .setTitle("Tickets") |
| 153 | .setDescription( |
| 154 | `${data.enabled ? "" : getEmojiByName("TICKETS.REPORT")} **Enabled:** ${data.enabled ? `${getEmojiByName("CONTROL.TICK")} Yes` : `${getEmojiByName("CONTROL.CROSS")} No`}\n` + |
| 155 | `${data.category ? "" : getEmojiByName("TICKETS.REPORT")} **Category:** ${data.category ? `<#${data.category}>` : "*None set*"}\n` + |
| 156 | `**Max Tickets:** ${data.maxTickets ? data.maxTickets : "*No limit*"}\n` + |
| 157 | `**Support Ping:** ${data.supportRole ? `<@&${data.supportRole}>` : "*None set*"}\n\n` + |
| 158 | ((data.useCustom && data.customTypes === null) ? `${getEmojiByName("TICKETS.REPORT")} ` : "") + |
| 159 | `${data.useCustom ? "Custom" : "Default"} types in use` + "\n\n" + |
| 160 | `${getEmojiByName("TICKETS.REPORT")} *Indicates a setting stopping tickets from being used*` |
| 161 | ) |
| 162 | .setStatus("Success") |
| 163 | .setEmoji("GUILD.TICKET.OPEN") |
| 164 | m = await interaction.editReply({ |
| 165 | embeds: [embed], components: [new MessageActionRow().addComponents([ |
| 166 | new MessageButton() |
| 167 | .setLabel("Tickets " + (data.enabled ? "enabled" : "disabled")) |
| 168 | .setEmoji(getEmojiByName("CONTROL." + (data.enabled ? "TICK" : "CROSS"), "id")) |
| 169 | .setStyle(data.enabled ? "SUCCESS" : "DANGER") |
| 170 | .setCustomId("enabled"), |
| 171 | new MessageButton() |
pineafan | e23c4ec | 2022-07-27 21:56:27 +0100 | [diff] [blame] | 172 | .setLabel(lastClicked === "cat" ? "Click again to confirm" : "Clear category") |
pineafan | 6702cef | 2022-06-13 17:52:37 +0100 | [diff] [blame] | 173 | .setEmoji(getEmojiByName("CONTROL.CROSS", "id")) |
| 174 | .setStyle("DANGER") |
| 175 | .setCustomId("clearCategory") |
pineafan | e23c4ec | 2022-07-27 21:56:27 +0100 | [diff] [blame] | 176 | .setDisabled(data.category === null), |
pineafan | 6702cef | 2022-06-13 17:52:37 +0100 | [diff] [blame] | 177 | new MessageButton() |
pineafan | e23c4ec | 2022-07-27 21:56:27 +0100 | [diff] [blame] | 178 | .setLabel(lastClicked === "max" ? "Click again to confirm" : "Reset max tickets") |
pineafan | 6702cef | 2022-06-13 17:52:37 +0100 | [diff] [blame] | 179 | .setEmoji(getEmojiByName("CONTROL.CROSS", "id")) |
| 180 | .setStyle("DANGER") |
| 181 | .setCustomId("clearMaxTickets") |
pineafan | e23c4ec | 2022-07-27 21:56:27 +0100 | [diff] [blame] | 182 | .setDisabled(data.maxTickets === 5), |
pineafan | 6702cef | 2022-06-13 17:52:37 +0100 | [diff] [blame] | 183 | new MessageButton() |
pineafan | e23c4ec | 2022-07-27 21:56:27 +0100 | [diff] [blame] | 184 | .setLabel(lastClicked === "sup" ? "Click again to confirm" : "Clear support ping") |
pineafan | 6702cef | 2022-06-13 17:52:37 +0100 | [diff] [blame] | 185 | .setEmoji(getEmojiByName("CONTROL.CROSS", "id")) |
| 186 | .setStyle("DANGER") |
| 187 | .setCustomId("clearSupportPing") |
pineafan | e23c4ec | 2022-07-27 21:56:27 +0100 | [diff] [blame] | 188 | .setDisabled(data.supportRole === null), |
pineafan | 6702cef | 2022-06-13 17:52:37 +0100 | [diff] [blame] | 189 | ]), new MessageActionRow().addComponents([ |
| 190 | new MessageButton() |
| 191 | .setLabel("Manage types") |
| 192 | .setEmoji(getEmojiByName("TICKETS.OTHER", "id")) |
| 193 | .setStyle("SECONDARY") |
| 194 | .setCustomId("manageTypes"), |
| 195 | ])] |
| 196 | }); |
| 197 | let i; |
| 198 | try { |
pineafan | c6158ab | 2022-06-17 16:34:07 +0100 | [diff] [blame] | 199 | i = await m.awaitMessageComponent({ time: 300000 }); |
pineafan | 6702cef | 2022-06-13 17:52:37 +0100 | [diff] [blame] | 200 | } catch (e) { break } |
| 201 | i.deferUpdate() |
pineafan | e23c4ec | 2022-07-27 21:56:27 +0100 | [diff] [blame] | 202 | if (i.component.customId === "clearCategory") { |
| 203 | if (lastClicked === "cat") { |
pineafan | 6702cef | 2022-06-13 17:52:37 +0100 | [diff] [blame] | 204 | lastClicked = ""; |
pineafan | e23c4ec | 2022-07-27 21:56:27 +0100 | [diff] [blame] | 205 | await client.database.guilds.write(interaction.guild.id, null, ["tickets.category"]) |
pineafan | 6702cef | 2022-06-13 17:52:37 +0100 | [diff] [blame] | 206 | data.category = undefined; |
| 207 | } else lastClicked = "cat"; |
pineafan | e23c4ec | 2022-07-27 21:56:27 +0100 | [diff] [blame] | 208 | } else if (i.component.customId === "clearMaxTickets") { |
| 209 | if (lastClicked === "max") { |
pineafan | 6702cef | 2022-06-13 17:52:37 +0100 | [diff] [blame] | 210 | lastClicked = ""; |
pineafan | e23c4ec | 2022-07-27 21:56:27 +0100 | [diff] [blame] | 211 | await client.database.guilds.write(interaction.guild.id, null, ["tickets.maxTickets"]) |
pineafan | 6702cef | 2022-06-13 17:52:37 +0100 | [diff] [blame] | 212 | data.maxTickets = 5; |
| 213 | } else lastClicked = "max"; |
pineafan | e23c4ec | 2022-07-27 21:56:27 +0100 | [diff] [blame] | 214 | } else if (i.component.customId === "clearSupportPing") { |
| 215 | if (lastClicked === "sup") { |
pineafan | 6702cef | 2022-06-13 17:52:37 +0100 | [diff] [blame] | 216 | lastClicked = ""; |
pineafan | e23c4ec | 2022-07-27 21:56:27 +0100 | [diff] [blame] | 217 | await client.database.guilds.write(interaction.guild.id, null, ["tickets.supportRole"]) |
pineafan | 6702cef | 2022-06-13 17:52:37 +0100 | [diff] [blame] | 218 | data.supportRole = undefined; |
| 219 | } else lastClicked = "sup"; |
pineafan | e23c4ec | 2022-07-27 21:56:27 +0100 | [diff] [blame] | 220 | } else if (i.component.customId === "enabled") { |
pineafan | 4edb776 | 2022-06-26 19:21:04 +0100 | [diff] [blame] | 221 | await client.database.guilds.write(interaction.guild.id, { "tickets.enabled": !data.enabled }) |
pineafan | 6702cef | 2022-06-13 17:52:37 +0100 | [diff] [blame] | 222 | data.enabled = !data.enabled; |
pineafan | e23c4ec | 2022-07-27 21:56:27 +0100 | [diff] [blame] | 223 | } else if (i.component.customId === "manageTypes") { |
pineafan | 6702cef | 2022-06-13 17:52:37 +0100 | [diff] [blame] | 224 | data = await manageTypes(interaction, data, m); |
| 225 | } else { |
| 226 | break |
| 227 | } |
| 228 | } |
| 229 | await interaction.editReply({ embeds: [embed.setFooter({ text: "Message closed" })], components: [] }); |
pineafan | 4f164f3 | 2022-02-26 22:07:12 +0000 | [diff] [blame] | 230 | } |
| 231 | |
pineafan | 6702cef | 2022-06-13 17:52:37 +0100 | [diff] [blame] | 232 | async function manageTypes(interaction, data, m) { |
| 233 | while (true) { |
| 234 | if (data.useCustom) { |
| 235 | let customTypes = data.customTypes; |
pineafan | c6158ab | 2022-06-17 16:34:07 +0100 | [diff] [blame] | 236 | await interaction.editReply({ |
pineafan | 4edb776 | 2022-06-26 19:21:04 +0100 | [diff] [blame] | 237 | embeds: [new EmojiEmbed() |
pineafan | 6702cef | 2022-06-13 17:52:37 +0100 | [diff] [blame] | 238 | .setTitle("Tickets > Types") |
| 239 | .setDescription( |
| 240 | "**Custom types enabled**\n\n" + |
| 241 | "**Types in use:**\n" + ((customTypes !== null) ? |
| 242 | (customTypes.map((t) => `> ${t}`).join("\n")) : |
| 243 | "*None set*" |
| 244 | ) + "\n\n" + (customTypes === null ? |
| 245 | `${getEmojiByName("TICKETS.REPORT")} Having no types will disable tickets. Please add at least 1 type or use default types` : "" |
| 246 | ) |
| 247 | ) |
| 248 | .setStatus("Success") |
| 249 | .setEmoji("GUILD.TICKET.OPEN") |
| 250 | ], components: (customTypes ? [ |
| 251 | new MessageActionRow().addComponents([new Discord.MessageSelectMenu() |
| 252 | .setCustomId("removeTypes") |
| 253 | .setPlaceholder("Select types to remove") |
| 254 | .setMaxValues(customTypes.length) |
| 255 | .setMinValues(1) |
| 256 | .addOptions(customTypes.map((t) => new SelectMenuOption().setLabel(t).setValue(t))) |
| 257 | ]) |
| 258 | ] : []).concat([ |
| 259 | new MessageActionRow().addComponents([ |
| 260 | new MessageButton() |
| 261 | .setLabel("Back") |
| 262 | .setEmoji(getEmojiByName("CONTROL.LEFT", "id")) |
| 263 | .setStyle("PRIMARY") |
| 264 | .setCustomId("back"), |
| 265 | new MessageButton() |
| 266 | .setLabel("Add new type") |
| 267 | .setEmoji(getEmojiByName("TICKETS.SUGGESTION", "id")) |
| 268 | .setStyle("PRIMARY") |
| 269 | .setCustomId("addType") |
| 270 | .setDisabled(customTypes !== null && customTypes.length >= 25), |
| 271 | new MessageButton() |
| 272 | .setLabel("Switch to default types") |
| 273 | .setStyle("SECONDARY") |
| 274 | .setCustomId("switchToDefault"), |
| 275 | ]) |
| 276 | ]) |
| 277 | }); |
| 278 | } else { |
| 279 | let inUse = toHexArray(data.types, ticketTypes) |
| 280 | let options = []; |
| 281 | ticketTypes.forEach(type => { |
| 282 | options.push(new SelectMenuOption({ |
| 283 | label: capitalize(type), |
| 284 | value: type, |
PineappleFan | b3dd83c | 2022-06-17 10:53:48 +0100 | [diff] [blame] | 285 | emoji: client.emojis.cache.get(getEmojiByName(`TICKETS.${type.toUpperCase()}`, "id")), |
pineafan | 6702cef | 2022-06-13 17:52:37 +0100 | [diff] [blame] | 286 | default: inUse.includes(type) |
| 287 | })) |
| 288 | }) |
| 289 | let selectPane = new MessageActionRow().addComponents([ |
| 290 | new Discord.MessageSelectMenu() |
| 291 | .addOptions(options) |
| 292 | .setCustomId("types") |
| 293 | .setMaxValues(ticketTypes.length) |
| 294 | .setMinValues(1) |
| 295 | .setPlaceholder("Select types to use") |
| 296 | ]) |
pineafan | c6158ab | 2022-06-17 16:34:07 +0100 | [diff] [blame] | 297 | await interaction.editReply({ |
pineafan | 4edb776 | 2022-06-26 19:21:04 +0100 | [diff] [blame] | 298 | embeds: [new EmojiEmbed() |
pineafan | 6702cef | 2022-06-13 17:52:37 +0100 | [diff] [blame] | 299 | .setTitle("Tickets > Types") |
| 300 | .setDescription( |
| 301 | "**Default types enabled**\n\n" + |
| 302 | "**Types in use:**\n" + |
| 303 | (inUse.map((t) => `> ${getEmojiByName("TICKETS." + t.toUpperCase())} ${capitalize(t)}`).join("\n")) |
| 304 | ) |
| 305 | .setStatus("Success") |
| 306 | .setEmoji("GUILD.TICKET.OPEN") |
| 307 | ], components: [ |
| 308 | selectPane, |
| 309 | new MessageActionRow().addComponents([ |
| 310 | new MessageButton() |
| 311 | .setLabel("Back") |
| 312 | .setEmoji(getEmojiByName("CONTROL.LEFT", "id")) |
| 313 | .setStyle("PRIMARY") |
| 314 | .setCustomId("back"), |
| 315 | new MessageButton() |
| 316 | .setLabel("Switch to custom types") |
| 317 | .setStyle("SECONDARY") |
| 318 | .setCustomId("switchToCustom"), |
| 319 | ]) |
| 320 | ] |
| 321 | }); |
| 322 | } |
| 323 | let i; |
| 324 | try { |
pineafan | c6158ab | 2022-06-17 16:34:07 +0100 | [diff] [blame] | 325 | i = await m.awaitMessageComponent({ time: 300000 }); |
pineafan | 6702cef | 2022-06-13 17:52:37 +0100 | [diff] [blame] | 326 | } catch (e) { break } |
pineafan | e23c4ec | 2022-07-27 21:56:27 +0100 | [diff] [blame] | 327 | if (i.component.customId === "types") { |
pineafan | 6702cef | 2022-06-13 17:52:37 +0100 | [diff] [blame] | 328 | i.deferUpdate() |
| 329 | let types = toHexInteger(i.values, ticketTypes); |
pineafan | 4edb776 | 2022-06-26 19:21:04 +0100 | [diff] [blame] | 330 | await client.database.guilds.write(interaction.guild.id, { "tickets.types": types }) |
pineafan | 6702cef | 2022-06-13 17:52:37 +0100 | [diff] [blame] | 331 | data.types = types; |
pineafan | e23c4ec | 2022-07-27 21:56:27 +0100 | [diff] [blame] | 332 | } else if (i.component.customId === "removeTypes") { |
pineafan | 6702cef | 2022-06-13 17:52:37 +0100 | [diff] [blame] | 333 | i.deferUpdate() |
| 334 | let types = i.values |
| 335 | let customTypes = data.customTypes; |
| 336 | if (customTypes) { |
| 337 | customTypes = customTypes.filter((t) => !types.includes(t)); |
| 338 | customTypes = customTypes.length > 0 ? customTypes : null; |
pineafan | 4edb776 | 2022-06-26 19:21:04 +0100 | [diff] [blame] | 339 | await client.database.guilds.write(interaction.guild.id, { "tickets.customTypes": customTypes }) |
pineafan | 6702cef | 2022-06-13 17:52:37 +0100 | [diff] [blame] | 340 | data.customTypes = customTypes; |
| 341 | } |
pineafan | e23c4ec | 2022-07-27 21:56:27 +0100 | [diff] [blame] | 342 | } else if (i.component.customId === "addType") { |
pineafan | 6702cef | 2022-06-13 17:52:37 +0100 | [diff] [blame] | 343 | await i.showModal(new Discord.Modal().setCustomId("modal").setTitle("Enter a name for the new type").addComponents( |
pineafan | 02ba023 | 2022-07-24 22:16:15 +0100 | [diff] [blame] | 344 | new MessageActionRow<TextInputComponent>().addComponents(new TextInputComponent() |
pineafan | 6702cef | 2022-06-13 17:52:37 +0100 | [diff] [blame] | 345 | .setCustomId("type") |
| 346 | .setLabel("Name") |
| 347 | .setMaxLength(100) |
| 348 | .setMinLength(1) |
| 349 | .setPlaceholder("E.g. \"Server Idea\"") |
| 350 | .setRequired(true) |
| 351 | .setStyle("SHORT") |
| 352 | ) |
| 353 | )) |
| 354 | await interaction.editReply({ |
pineafan | 4edb776 | 2022-06-26 19:21:04 +0100 | [diff] [blame] | 355 | embeds: [new EmojiEmbed() |
pineafan | 6702cef | 2022-06-13 17:52:37 +0100 | [diff] [blame] | 356 | .setTitle("Tickets > Types") |
| 357 | .setDescription("Modal opened. If you can't see it, click back and try again.") |
| 358 | .setStatus("Success") |
| 359 | .setEmoji("GUILD.TICKET.OPEN") |
| 360 | ], components: [new MessageActionRow().addComponents([new MessageButton() |
| 361 | .setLabel("Back") |
| 362 | .setEmoji(getEmojiByName("CONTROL.LEFT", "id")) |
| 363 | .setStyle("PRIMARY") |
| 364 | .setCustomId("back") |
| 365 | ])] |
| 366 | }); |
pineafan | 4edb776 | 2022-06-26 19:21:04 +0100 | [diff] [blame] | 367 | let out; |
pineafan | 6702cef | 2022-06-13 17:52:37 +0100 | [diff] [blame] | 368 | try { |
pineafan | e23c4ec | 2022-07-27 21:56:27 +0100 | [diff] [blame] | 369 | out = await modalInteractionCollector(m, (m) => m.channel.id === interaction.channel.id, (m) => m.customId === "addType") |
pineafan | 6702cef | 2022-06-13 17:52:37 +0100 | [diff] [blame] | 370 | } catch (e) { continue } |
| 371 | if (out.fields) { |
| 372 | let toAdd = out.fields.getTextInputValue("type"); |
| 373 | if (!toAdd) { continue } |
pineafan | 4edb776 | 2022-06-26 19:21:04 +0100 | [diff] [blame] | 374 | toAdd = toAdd.substring(0, 80) |
pineafan | 6702cef | 2022-06-13 17:52:37 +0100 | [diff] [blame] | 375 | try { |
pineafan | 4edb776 | 2022-06-26 19:21:04 +0100 | [diff] [blame] | 376 | await client.database.guilds.append(interaction.guild.id, "tickets.customTypes", toAdd) |
pineafan | 6702cef | 2022-06-13 17:52:37 +0100 | [diff] [blame] | 377 | } catch { continue } |
| 378 | data.customTypes = data.customTypes || []; |
| 379 | if (!data.customTypes.includes(toAdd)) { |
| 380 | data.customTypes.push(toAdd); |
| 381 | } |
| 382 | } else { continue } |
pineafan | e23c4ec | 2022-07-27 21:56:27 +0100 | [diff] [blame] | 383 | } else if (i.component.customId === "switchToDefault") { |
pineafan | 6702cef | 2022-06-13 17:52:37 +0100 | [diff] [blame] | 384 | i.deferUpdate() |
pineafan | 4edb776 | 2022-06-26 19:21:04 +0100 | [diff] [blame] | 385 | await client.database.guilds.write(interaction.guild.id, { "tickets.useCustom": false }, []) |
pineafan | 6702cef | 2022-06-13 17:52:37 +0100 | [diff] [blame] | 386 | data.useCustom = false; |
pineafan | e23c4ec | 2022-07-27 21:56:27 +0100 | [diff] [blame] | 387 | } else if (i.component.customId === "switchToCustom") { |
pineafan | 6702cef | 2022-06-13 17:52:37 +0100 | [diff] [blame] | 388 | i.deferUpdate() |
pineafan | 4edb776 | 2022-06-26 19:21:04 +0100 | [diff] [blame] | 389 | await client.database.guilds.write(interaction.guild.id, { "tickets.useCustom": true }, []) |
pineafan | 6702cef | 2022-06-13 17:52:37 +0100 | [diff] [blame] | 390 | data.useCustom = true; |
| 391 | } else { |
| 392 | i.deferUpdate() |
| 393 | break |
| 394 | } |
| 395 | } |
| 396 | return data |
| 397 | } |
| 398 | |
| 399 | |
pineafan | 4f164f3 | 2022-02-26 22:07:12 +0000 | [diff] [blame] | 400 | const check = (interaction: CommandInteraction, defaultCheck: WrappedCheck) => { |
pineafan | 6702cef | 2022-06-13 17:52:37 +0100 | [diff] [blame] | 401 | let member = (interaction.member as Discord.GuildMember) |
pineafan | e23c4ec | 2022-07-27 21:56:27 +0100 | [diff] [blame] | 402 | if (!member.permissions.has("MANAGE_GUILD")) throw "You must have the *Manage Server* permission to use this command" |
pineafan | 6702cef | 2022-06-13 17:52:37 +0100 | [diff] [blame] | 403 | return true; |
pineafan | 4f164f3 | 2022-02-26 22:07:12 +0000 | [diff] [blame] | 404 | } |
| 405 | |
| 406 | export { command }; |
| 407 | export { callback }; |
| 408 | export { check }; |