pineafan | 63fc5e2 | 2022-08-04 22:04:10 +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 | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [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"; |
pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame^] | 11 | import { capitalize } from "../../utils/generateKeyValueList.js"; |
pineafan | 6702cef | 2022-06-13 17:52:37 +0100 | [diff] [blame] | 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 | 63fc5e2 | 2022-08-04 22:04:10 +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 | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame^] | 25 | m = await interaction.reply({embeds: LoadingEmbed, ephemeral: true, fetchReply: true}); |
| 26 | const options = { |
pineafan | 6702cef | 2022-06-13 17:52:37 +0100 | [diff] [blame] | 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") |
pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame^] | 31 | }; |
| 32 | console.log(m); |
pineafan | 6702cef | 2022-06-13 17:52:37 +0100 | [diff] [blame] | 33 | if (options.enabled !== null || options.category || options.maxtickets || options.supportping) { |
| 34 | options.enabled = options.enabled === "yes" ? true : false; |
| 35 | if (options.category) { |
pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame^] | 36 | let channel; |
pineafan | 6702cef | 2022-06-13 17:52:37 +0100 | [diff] [blame] | 37 | try { |
pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame^] | 38 | channel = interaction.guild.channels.cache.get(options.category.id); |
pineafan | 6702cef | 2022-06-13 17:52:37 +0100 | [diff] [blame] | 39 | } catch { |
| 40 | return await interaction.editReply({ |
pineafan | 4edb776 | 2022-06-26 19:21:04 +0100 | [diff] [blame] | 41 | embeds: [new EmojiEmbed() |
pineafan | 6702cef | 2022-06-13 17:52:37 +0100 | [diff] [blame] | 42 | .setEmoji("CHANNEL.TEXT.DELETE") |
| 43 | .setTitle("Tickets > Category") |
| 44 | .setDescription("The channel you provided is not a valid category") |
| 45 | .setStatus("Danger") |
| 46 | ] |
pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame^] | 47 | }); |
pineafan | 6702cef | 2022-06-13 17:52:37 +0100 | [diff] [blame] | 48 | } |
pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame^] | 49 | channel = channel as Discord.CategoryChannel; |
pineafan | e23c4ec | 2022-07-27 21:56:27 +0100 | [diff] [blame] | 50 | if (channel.guild.id !== interaction.guild.id) return interaction.editReply({ |
pineafan | 4edb776 | 2022-06-26 19:21:04 +0100 | [diff] [blame] | 51 | embeds: [new EmojiEmbed() |
pineafan | 6702cef | 2022-06-13 17:52:37 +0100 | [diff] [blame] | 52 | .setTitle("Tickets > Category") |
pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame^] | 53 | .setDescription("You must choose a category in this server") |
pineafan | 6702cef | 2022-06-13 17:52:37 +0100 | [diff] [blame] | 54 | .setStatus("Danger") |
| 55 | .setEmoji("CHANNEL.TEXT.DELETE") |
| 56 | ] |
| 57 | }); |
| 58 | } |
| 59 | if (options.maxtickets) { |
| 60 | if (options.maxtickets < 1) return interaction.editReply({ |
pineafan | 4edb776 | 2022-06-26 19:21:04 +0100 | [diff] [blame] | 61 | embeds: [new EmojiEmbed() |
pineafan | 6702cef | 2022-06-13 17:52:37 +0100 | [diff] [blame] | 62 | .setTitle("Tickets > Max Tickets") |
pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame^] | 63 | .setDescription("You must choose a number greater than 0") |
pineafan | 6702cef | 2022-06-13 17:52:37 +0100 | [diff] [blame] | 64 | .setStatus("Danger") |
| 65 | .setEmoji("CHANNEL.TEXT.DELETE") |
| 66 | ] |
| 67 | }); |
| 68 | } |
pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame^] | 69 | let role; |
pineafan | 6702cef | 2022-06-13 17:52:37 +0100 | [diff] [blame] | 70 | if (options.supportping) { |
| 71 | try { |
pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame^] | 72 | role = interaction.guild.roles.cache.get(options.supportping.id); |
pineafan | 6702cef | 2022-06-13 17:52:37 +0100 | [diff] [blame] | 73 | } catch { |
| 74 | return await interaction.editReply({ |
pineafan | 4edb776 | 2022-06-26 19:21:04 +0100 | [diff] [blame] | 75 | embeds: [new EmojiEmbed() |
pineafan | 6702cef | 2022-06-13 17:52:37 +0100 | [diff] [blame] | 76 | .setEmoji("GUILD.ROLE.DELETE") |
| 77 | .setTitle("Tickets > Support Ping") |
| 78 | .setDescription("The role you provided is not a valid role") |
| 79 | .setStatus("Danger") |
| 80 | ] |
pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame^] | 81 | }); |
pineafan | 6702cef | 2022-06-13 17:52:37 +0100 | [diff] [blame] | 82 | } |
pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame^] | 83 | role = role as Discord.Role; |
pineafan | e23c4ec | 2022-07-27 21:56:27 +0100 | [diff] [blame] | 84 | if (role.guild.id !== interaction.guild.id) return interaction.editReply({ |
pineafan | 4edb776 | 2022-06-26 19:21:04 +0100 | [diff] [blame] | 85 | embeds: [new EmojiEmbed() |
pineafan | 6702cef | 2022-06-13 17:52:37 +0100 | [diff] [blame] | 86 | .setTitle("Tickets > Support Ping") |
pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame^] | 87 | .setDescription("You must choose a role in this server") |
pineafan | 6702cef | 2022-06-13 17:52:37 +0100 | [diff] [blame] | 88 | .setStatus("Danger") |
| 89 | .setEmoji("GUILD.ROLE.DELETE") |
| 90 | ] |
| 91 | }); |
| 92 | } |
| 93 | |
pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame^] | 94 | const confirmation = await new confirmationMessage(interaction) |
pineafan | 6702cef | 2022-06-13 17:52:37 +0100 | [diff] [blame] | 95 | .setEmoji("GUILD.TICKET.ARCHIVED") |
| 96 | .setTitle("Tickets") |
| 97 | .setDescription( |
| 98 | (options.category ? `**Category:** ${options.category.name}\n` : "") + |
| 99 | (options.maxtickets ? `**Max Tickets:** ${options.maxtickets}\n` : "") + |
| 100 | (options.supportping ? `**Support Ping:** ${options.supportping.name}\n` : "") + |
| 101 | (options.enabled !== null ? `**Enabled:** ${options.enabled ? `${getEmojiByName("CONTROL.TICK")} Yes` : `${getEmojiByName("CONTROL.CROSS")} No` |
pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame^] | 102 | }\n` : "") + |
| 103 | "\nAre you sure you want to apply these settings?" |
pineafan | 6702cef | 2022-06-13 17:52:37 +0100 | [diff] [blame] | 104 | ) |
| 105 | .setColor("Warning") |
| 106 | .setInverted(true) |
pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame^] | 107 | .send(true); |
| 108 | if (confirmation.cancelled) return; |
pineafan | 6702cef | 2022-06-13 17:52:37 +0100 | [diff] [blame] | 109 | if (confirmation.success) { |
pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame^] | 110 | const toUpdate = {}; |
| 111 | if (options.enabled !== null) toUpdate["tickets.enabled"] = options.enabled; |
| 112 | if (options.category) toUpdate["tickets.category"] = options.category.id; |
| 113 | if (options.maxtickets) toUpdate["tickets.maxTickets"] = options.maxtickets; |
| 114 | if (options.supportping) toUpdate["tickets.supportRole"] = options.supportping.id; |
pineafan | 6702cef | 2022-06-13 17:52:37 +0100 | [diff] [blame] | 115 | try { |
pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame^] | 116 | await client.database.guilds.write(interaction.guild.id, toUpdate); |
pineafan | 6702cef | 2022-06-13 17:52:37 +0100 | [diff] [blame] | 117 | } catch (e) { |
| 118 | return interaction.editReply({ |
pineafan | 4edb776 | 2022-06-26 19:21:04 +0100 | [diff] [blame] | 119 | embeds: [new EmojiEmbed() |
pineafan | 6702cef | 2022-06-13 17:52:37 +0100 | [diff] [blame] | 120 | .setTitle("Tickets") |
pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame^] | 121 | .setDescription("Something went wrong and the staff notifications channel could not be set") |
pineafan | 6702cef | 2022-06-13 17:52:37 +0100 | [diff] [blame] | 122 | .setStatus("Danger") |
| 123 | .setEmoji("GUILD.TICKET.DELETE") |
| 124 | ], components: [] |
| 125 | }); |
| 126 | } |
| 127 | } else { |
| 128 | return interaction.editReply({ |
pineafan | 4edb776 | 2022-06-26 19:21:04 +0100 | [diff] [blame] | 129 | embeds: [new EmojiEmbed() |
pineafan | 6702cef | 2022-06-13 17:52:37 +0100 | [diff] [blame] | 130 | .setTitle("Tickets") |
pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame^] | 131 | .setDescription("No changes were made") |
pineafan | 6702cef | 2022-06-13 17:52:37 +0100 | [diff] [blame] | 132 | .setStatus("Success") |
| 133 | .setEmoji("GUILD.TICKET.OPEN") |
| 134 | ], components: [] |
| 135 | }); |
| 136 | } |
| 137 | } |
pineafan | 4edb776 | 2022-06-26 19:21:04 +0100 | [diff] [blame] | 138 | let data = await client.database.guilds.read(interaction.guild.id); |
pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame^] | 139 | 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] | 140 | let lastClicked = ""; |
| 141 | let embed; |
| 142 | data = { |
| 143 | enabled: data.tickets.enabled, |
| 144 | category: data.tickets.category, |
| 145 | maxTickets: data.tickets.maxTickets, |
| 146 | supportRole: data.tickets.supportRole, |
| 147 | useCustom: data.tickets.useCustom, |
| 148 | types: data.tickets.types, |
| 149 | customTypes: data.tickets.customTypes |
pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame^] | 150 | }; |
pineafan | 6702cef | 2022-06-13 17:52:37 +0100 | [diff] [blame] | 151 | while (true) { |
pineafan | 4edb776 | 2022-06-26 19:21:04 +0100 | [diff] [blame] | 152 | embed = new EmojiEmbed() |
pineafan | 6702cef | 2022-06-13 17:52:37 +0100 | [diff] [blame] | 153 | .setTitle("Tickets") |
| 154 | .setDescription( |
| 155 | `${data.enabled ? "" : getEmojiByName("TICKETS.REPORT")} **Enabled:** ${data.enabled ? `${getEmojiByName("CONTROL.TICK")} Yes` : `${getEmojiByName("CONTROL.CROSS")} No`}\n` + |
| 156 | `${data.category ? "" : getEmojiByName("TICKETS.REPORT")} **Category:** ${data.category ? `<#${data.category}>` : "*None set*"}\n` + |
| 157 | `**Max Tickets:** ${data.maxTickets ? data.maxTickets : "*No limit*"}\n` + |
| 158 | `**Support Ping:** ${data.supportRole ? `<@&${data.supportRole}>` : "*None set*"}\n\n` + |
| 159 | ((data.useCustom && data.customTypes === null) ? `${getEmojiByName("TICKETS.REPORT")} ` : "") + |
| 160 | `${data.useCustom ? "Custom" : "Default"} types in use` + "\n\n" + |
| 161 | `${getEmojiByName("TICKETS.REPORT")} *Indicates a setting stopping tickets from being used*` |
| 162 | ) |
| 163 | .setStatus("Success") |
pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame^] | 164 | .setEmoji("GUILD.TICKET.OPEN"); |
pineafan | 6702cef | 2022-06-13 17:52:37 +0100 | [diff] [blame] | 165 | m = await interaction.editReply({ |
| 166 | embeds: [embed], components: [new MessageActionRow().addComponents([ |
| 167 | new MessageButton() |
| 168 | .setLabel("Tickets " + (data.enabled ? "enabled" : "disabled")) |
| 169 | .setEmoji(getEmojiByName("CONTROL." + (data.enabled ? "TICK" : "CROSS"), "id")) |
| 170 | .setStyle(data.enabled ? "SUCCESS" : "DANGER") |
| 171 | .setCustomId("enabled"), |
| 172 | new MessageButton() |
pineafan | e23c4ec | 2022-07-27 21:56:27 +0100 | [diff] [blame] | 173 | .setLabel(lastClicked === "cat" ? "Click again to confirm" : "Clear category") |
pineafan | 6702cef | 2022-06-13 17:52:37 +0100 | [diff] [blame] | 174 | .setEmoji(getEmojiByName("CONTROL.CROSS", "id")) |
| 175 | .setStyle("DANGER") |
| 176 | .setCustomId("clearCategory") |
pineafan | e23c4ec | 2022-07-27 21:56:27 +0100 | [diff] [blame] | 177 | .setDisabled(data.category === null), |
pineafan | 6702cef | 2022-06-13 17:52:37 +0100 | [diff] [blame] | 178 | new MessageButton() |
pineafan | e23c4ec | 2022-07-27 21:56:27 +0100 | [diff] [blame] | 179 | .setLabel(lastClicked === "max" ? "Click again to confirm" : "Reset max tickets") |
pineafan | 6702cef | 2022-06-13 17:52:37 +0100 | [diff] [blame] | 180 | .setEmoji(getEmojiByName("CONTROL.CROSS", "id")) |
| 181 | .setStyle("DANGER") |
| 182 | .setCustomId("clearMaxTickets") |
pineafan | e23c4ec | 2022-07-27 21:56:27 +0100 | [diff] [blame] | 183 | .setDisabled(data.maxTickets === 5), |
pineafan | 6702cef | 2022-06-13 17:52:37 +0100 | [diff] [blame] | 184 | new MessageButton() |
pineafan | e23c4ec | 2022-07-27 21:56:27 +0100 | [diff] [blame] | 185 | .setLabel(lastClicked === "sup" ? "Click again to confirm" : "Clear support ping") |
pineafan | 6702cef | 2022-06-13 17:52:37 +0100 | [diff] [blame] | 186 | .setEmoji(getEmojiByName("CONTROL.CROSS", "id")) |
| 187 | .setStyle("DANGER") |
| 188 | .setCustomId("clearSupportPing") |
pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame^] | 189 | .setDisabled(data.supportRole === null) |
pineafan | 6702cef | 2022-06-13 17:52:37 +0100 | [diff] [blame] | 190 | ]), new MessageActionRow().addComponents([ |
| 191 | new MessageButton() |
| 192 | .setLabel("Manage types") |
| 193 | .setEmoji(getEmojiByName("TICKETS.OTHER", "id")) |
| 194 | .setStyle("SECONDARY") |
| 195 | .setCustomId("manageTypes"), |
pineafan | 41d9356 | 2022-07-30 22:10:15 +0100 | [diff] [blame] | 196 | new MessageButton() |
| 197 | .setLabel("Add create ticket button") |
| 198 | .setEmoji(getEmojiByName("TICKETS.SUGGESTION", "id")) |
| 199 | .setStyle("PRIMARY") |
pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame^] | 200 | .setCustomId("send") |
pineafan | 6702cef | 2022-06-13 17:52:37 +0100 | [diff] [blame] | 201 | ])] |
| 202 | }); |
| 203 | let i; |
| 204 | try { |
pineafan | c6158ab | 2022-06-17 16:34:07 +0100 | [diff] [blame] | 205 | i = await m.awaitMessageComponent({ time: 300000 }); |
pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame^] | 206 | } catch (e) { break; } |
| 207 | i.deferUpdate(); |
pineafan | e23c4ec | 2022-07-27 21:56:27 +0100 | [diff] [blame] | 208 | if (i.component.customId === "clearCategory") { |
| 209 | if (lastClicked === "cat") { |
pineafan | 6702cef | 2022-06-13 17:52:37 +0100 | [diff] [blame] | 210 | lastClicked = ""; |
pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame^] | 211 | await client.database.guilds.write(interaction.guild.id, null, ["tickets.category"]); |
pineafan | 6702cef | 2022-06-13 17:52:37 +0100 | [diff] [blame] | 212 | data.category = undefined; |
| 213 | } else lastClicked = "cat"; |
pineafan | e23c4ec | 2022-07-27 21:56:27 +0100 | [diff] [blame] | 214 | } else if (i.component.customId === "clearMaxTickets") { |
| 215 | if (lastClicked === "max") { |
pineafan | 6702cef | 2022-06-13 17:52:37 +0100 | [diff] [blame] | 216 | lastClicked = ""; |
pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame^] | 217 | await client.database.guilds.write(interaction.guild.id, null, ["tickets.maxTickets"]); |
pineafan | 6702cef | 2022-06-13 17:52:37 +0100 | [diff] [blame] | 218 | data.maxTickets = 5; |
| 219 | } else lastClicked = "max"; |
pineafan | e23c4ec | 2022-07-27 21:56:27 +0100 | [diff] [blame] | 220 | } else if (i.component.customId === "clearSupportPing") { |
| 221 | if (lastClicked === "sup") { |
pineafan | 6702cef | 2022-06-13 17:52:37 +0100 | [diff] [blame] | 222 | lastClicked = ""; |
pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame^] | 223 | await client.database.guilds.write(interaction.guild.id, null, ["tickets.supportRole"]); |
pineafan | 6702cef | 2022-06-13 17:52:37 +0100 | [diff] [blame] | 224 | data.supportRole = undefined; |
| 225 | } else lastClicked = "sup"; |
pineafan | 41d9356 | 2022-07-30 22:10:15 +0100 | [diff] [blame] | 226 | } else if (i.component.customId === "send") { |
| 227 | const ticketMessages = [ |
| 228 | {label: "Create ticket", description: "Click the button below to create a ticket"}, |
| 229 | {label: "Issues, questions or feedback?", description: "Click below to open a ticket and get help from our staff team"}, |
pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame^] | 230 | {label: "Contact Us", description: "Click the button below to speak to us privately"} |
| 231 | ]; |
pineafan | 41d9356 | 2022-07-30 22:10:15 +0100 | [diff] [blame] | 232 | while (true) { |
pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame^] | 233 | const enabled = data.enabled && data.category !== null; |
pineafan | 41d9356 | 2022-07-30 22:10:15 +0100 | [diff] [blame] | 234 | await interaction.editReply({embeds: [new EmojiEmbed() |
| 235 | .setTitle("Ticket Button") |
| 236 | .setDescription("Select a message template to send in this channel") |
| 237 | .setFooter({text: enabled ? "" : "Tickets are not set up correctly so the button may not work for users. Check the main menu to find which options must be set."}) |
| 238 | .setStatus(enabled ? "Success" : "Warning") |
| 239 | .setEmoji("GUILD.ROLES.CREATE") |
| 240 | ], components: [ |
| 241 | new MessageActionRow().addComponents([ |
| 242 | new MessageSelectMenu().setOptions(ticketMessages.map((t: {label: string, description: string, value?: string}, index) => { |
pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame^] | 243 | t.value = index.toString(); return t as {value: string, label: string, description: string}; |
| 244 | })).setCustomId("template").setMaxValues(1).setMinValues(1).setPlaceholder("Select a message template") |
pineafan | 41d9356 | 2022-07-30 22:10:15 +0100 | [diff] [blame] | 245 | ]), |
| 246 | new MessageActionRow().addComponents([ |
| 247 | new MessageButton() |
| 248 | .setCustomId("back") |
| 249 | .setLabel("Back") |
| 250 | .setEmoji(getEmojiByName("CONTROL.LEFT", "id")) |
| 251 | .setStyle("DANGER"), |
| 252 | new MessageButton() |
| 253 | .setCustomId("blank") |
| 254 | .setLabel("Empty") |
| 255 | .setStyle("SECONDARY"), |
| 256 | new MessageButton() |
| 257 | .setCustomId("custom") |
| 258 | .setLabel("Custom") |
| 259 | .setEmoji(getEmojiByName("TICKETS.OTHER", "id")) |
| 260 | .setStyle("PRIMARY") |
| 261 | ]) |
| 262 | ]}); |
| 263 | let i; |
| 264 | try { |
| 265 | i = await m.awaitMessageComponent({time: 300000}); |
pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame^] | 266 | } catch(e) { break; } |
pineafan | 41d9356 | 2022-07-30 22:10:15 +0100 | [diff] [blame] | 267 | if (i.component.customId === "template") { |
pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame^] | 268 | i.deferUpdate(); |
pineafan | 41d9356 | 2022-07-30 22:10:15 +0100 | [diff] [blame] | 269 | await interaction.channel.send({embeds: [new EmojiEmbed() |
| 270 | .setTitle(ticketMessages[parseInt(i.values[0])].label) |
| 271 | .setDescription(ticketMessages[parseInt(i.values[0])].description) |
| 272 | .setStatus("Success") |
| 273 | .setEmoji("GUILD.TICKET.OPEN") |
| 274 | ], components: [new MessageActionRow().addComponents([new MessageButton() |
| 275 | .setLabel("Create Ticket") |
| 276 | .setEmoji(getEmojiByName("CONTROL.TICK", "id")) |
| 277 | .setStyle("SUCCESS") |
| 278 | .setCustomId("createticket") |
| 279 | ])]}); |
pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame^] | 280 | break; |
pineafan | 41d9356 | 2022-07-30 22:10:15 +0100 | [diff] [blame] | 281 | } else if (i.component.customId === "blank") { |
pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame^] | 282 | i.deferUpdate(); |
pineafan | 41d9356 | 2022-07-30 22:10:15 +0100 | [diff] [blame] | 283 | await interaction.channel.send({components: [new MessageActionRow().addComponents([new MessageButton() |
| 284 | .setLabel("Create Ticket") |
| 285 | .setEmoji(getEmojiByName("TICKETS.SUGGESTION", "id")) |
| 286 | .setStyle("SUCCESS") |
| 287 | .setCustomId("createticket") |
| 288 | ])]}); |
pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame^] | 289 | break; |
pineafan | 41d9356 | 2022-07-30 22:10:15 +0100 | [diff] [blame] | 290 | } else if (i.component.customId === "custom") { |
pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame^] | 291 | await i.showModal(new Discord.Modal().setCustomId("modal").setTitle("Enter embed details").addComponents( |
pineafan | 41d9356 | 2022-07-30 22:10:15 +0100 | [diff] [blame] | 292 | new MessageActionRow<TextInputComponent>().addComponents(new TextInputComponent() |
| 293 | .setCustomId("title") |
| 294 | .setLabel("Title") |
| 295 | .setMaxLength(256) |
| 296 | .setRequired(true) |
| 297 | .setStyle("SHORT") |
| 298 | ), |
| 299 | new MessageActionRow<TextInputComponent>().addComponents(new TextInputComponent() |
| 300 | .setCustomId("description") |
| 301 | .setLabel("Description") |
| 302 | .setMaxLength(4000) |
| 303 | .setRequired(true) |
| 304 | .setStyle("PARAGRAPH") |
| 305 | ) |
pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame^] | 306 | )); |
pineafan | 41d9356 | 2022-07-30 22:10:15 +0100 | [diff] [blame] | 307 | await interaction.editReply({ |
| 308 | embeds: [new EmojiEmbed() |
| 309 | .setTitle("Ticket Button") |
| 310 | .setDescription("Modal opened. If you can't see it, click back and try again.") |
| 311 | .setStatus("Success") |
| 312 | .setEmoji("GUILD.TICKET.OPEN") |
| 313 | ], components: [new MessageActionRow().addComponents([new MessageButton() |
| 314 | .setLabel("Back") |
| 315 | .setEmoji(getEmojiByName("CONTROL.LEFT", "id")) |
| 316 | .setStyle("PRIMARY") |
| 317 | .setCustomId("back") |
| 318 | ])] |
| 319 | }); |
| 320 | let out; |
| 321 | try { |
pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame^] | 322 | out = await modalInteractionCollector(m, (m) => m.channel.id === interaction.channel.id, (m) => m.customId === "modify"); |
| 323 | } catch (e) { break; } |
pineafan | 41d9356 | 2022-07-30 22:10:15 +0100 | [diff] [blame] | 324 | if (out.fields) { |
pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame^] | 325 | const title = out.fields.getTextInputValue("title"); |
| 326 | const description = out.fields.getTextInputValue("description"); |
pineafan | 41d9356 | 2022-07-30 22:10:15 +0100 | [diff] [blame] | 327 | await interaction.channel.send({embeds: [new EmojiEmbed() |
| 328 | .setTitle(title) |
| 329 | .setDescription(description) |
| 330 | .setStatus("Success") |
| 331 | .setEmoji("GUILD.TICKET.OPEN") |
| 332 | ], components: [new MessageActionRow().addComponents([new MessageButton() |
| 333 | .setLabel("Create Ticket") |
| 334 | .setEmoji(getEmojiByName("TICKETS.SUGGESTION", "id")) |
| 335 | .setStyle("SUCCESS") |
| 336 | .setCustomId("createticket") |
| 337 | ])]}); |
pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame^] | 338 | break; |
| 339 | } else { continue; } |
pineafan | 41d9356 | 2022-07-30 22:10:15 +0100 | [diff] [blame] | 340 | } |
| 341 | } |
pineafan | e23c4ec | 2022-07-27 21:56:27 +0100 | [diff] [blame] | 342 | } else if (i.component.customId === "enabled") { |
pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame^] | 343 | await client.database.guilds.write(interaction.guild.id, { "tickets.enabled": !data.enabled }); |
pineafan | 6702cef | 2022-06-13 17:52:37 +0100 | [diff] [blame] | 344 | data.enabled = !data.enabled; |
pineafan | e23c4ec | 2022-07-27 21:56:27 +0100 | [diff] [blame] | 345 | } else if (i.component.customId === "manageTypes") { |
pineafan | 6702cef | 2022-06-13 17:52:37 +0100 | [diff] [blame] | 346 | data = await manageTypes(interaction, data, m); |
| 347 | } else { |
pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame^] | 348 | break; |
pineafan | 6702cef | 2022-06-13 17:52:37 +0100 | [diff] [blame] | 349 | } |
| 350 | } |
| 351 | await interaction.editReply({ embeds: [embed.setFooter({ text: "Message closed" })], components: [] }); |
pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame^] | 352 | }; |
pineafan | 4f164f3 | 2022-02-26 22:07:12 +0000 | [diff] [blame] | 353 | |
pineafan | 6702cef | 2022-06-13 17:52:37 +0100 | [diff] [blame] | 354 | async function manageTypes(interaction, data, m) { |
| 355 | while (true) { |
| 356 | if (data.useCustom) { |
pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame^] | 357 | const customTypes = data.customTypes; |
pineafan | c6158ab | 2022-06-17 16:34:07 +0100 | [diff] [blame] | 358 | await interaction.editReply({ |
pineafan | 4edb776 | 2022-06-26 19:21:04 +0100 | [diff] [blame] | 359 | embeds: [new EmojiEmbed() |
pineafan | 6702cef | 2022-06-13 17:52:37 +0100 | [diff] [blame] | 360 | .setTitle("Tickets > Types") |
| 361 | .setDescription( |
| 362 | "**Custom types enabled**\n\n" + |
| 363 | "**Types in use:**\n" + ((customTypes !== null) ? |
| 364 | (customTypes.map((t) => `> ${t}`).join("\n")) : |
| 365 | "*None set*" |
| 366 | ) + "\n\n" + (customTypes === null ? |
| 367 | `${getEmojiByName("TICKETS.REPORT")} Having no types will disable tickets. Please add at least 1 type or use default types` : "" |
| 368 | ) |
| 369 | ) |
| 370 | .setStatus("Success") |
| 371 | .setEmoji("GUILD.TICKET.OPEN") |
| 372 | ], components: (customTypes ? [ |
| 373 | new MessageActionRow().addComponents([new Discord.MessageSelectMenu() |
| 374 | .setCustomId("removeTypes") |
| 375 | .setPlaceholder("Select types to remove") |
| 376 | .setMaxValues(customTypes.length) |
| 377 | .setMinValues(1) |
| 378 | .addOptions(customTypes.map((t) => new SelectMenuOption().setLabel(t).setValue(t))) |
| 379 | ]) |
| 380 | ] : []).concat([ |
| 381 | new MessageActionRow().addComponents([ |
| 382 | new MessageButton() |
| 383 | .setLabel("Back") |
| 384 | .setEmoji(getEmojiByName("CONTROL.LEFT", "id")) |
| 385 | .setStyle("PRIMARY") |
| 386 | .setCustomId("back"), |
| 387 | new MessageButton() |
| 388 | .setLabel("Add new type") |
| 389 | .setEmoji(getEmojiByName("TICKETS.SUGGESTION", "id")) |
| 390 | .setStyle("PRIMARY") |
| 391 | .setCustomId("addType") |
| 392 | .setDisabled(customTypes !== null && customTypes.length >= 25), |
| 393 | new MessageButton() |
| 394 | .setLabel("Switch to default types") |
| 395 | .setStyle("SECONDARY") |
pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame^] | 396 | .setCustomId("switchToDefault") |
pineafan | 6702cef | 2022-06-13 17:52:37 +0100 | [diff] [blame] | 397 | ]) |
| 398 | ]) |
| 399 | }); |
| 400 | } else { |
pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame^] | 401 | const inUse = toHexArray(data.types, ticketTypes); |
| 402 | const options = []; |
pineafan | 6702cef | 2022-06-13 17:52:37 +0100 | [diff] [blame] | 403 | ticketTypes.forEach(type => { |
| 404 | options.push(new SelectMenuOption({ |
| 405 | label: capitalize(type), |
| 406 | value: type, |
PineappleFan | b3dd83c | 2022-06-17 10:53:48 +0100 | [diff] [blame] | 407 | emoji: client.emojis.cache.get(getEmojiByName(`TICKETS.${type.toUpperCase()}`, "id")), |
pineafan | 6702cef | 2022-06-13 17:52:37 +0100 | [diff] [blame] | 408 | default: inUse.includes(type) |
pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame^] | 409 | })); |
| 410 | }); |
| 411 | const selectPane = new MessageActionRow().addComponents([ |
pineafan | 6702cef | 2022-06-13 17:52:37 +0100 | [diff] [blame] | 412 | new Discord.MessageSelectMenu() |
| 413 | .addOptions(options) |
| 414 | .setCustomId("types") |
| 415 | .setMaxValues(ticketTypes.length) |
| 416 | .setMinValues(1) |
| 417 | .setPlaceholder("Select types to use") |
pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame^] | 418 | ]); |
pineafan | c6158ab | 2022-06-17 16:34:07 +0100 | [diff] [blame] | 419 | await interaction.editReply({ |
pineafan | 4edb776 | 2022-06-26 19:21:04 +0100 | [diff] [blame] | 420 | embeds: [new EmojiEmbed() |
pineafan | 6702cef | 2022-06-13 17:52:37 +0100 | [diff] [blame] | 421 | .setTitle("Tickets > Types") |
| 422 | .setDescription( |
| 423 | "**Default types enabled**\n\n" + |
| 424 | "**Types in use:**\n" + |
| 425 | (inUse.map((t) => `> ${getEmojiByName("TICKETS." + t.toUpperCase())} ${capitalize(t)}`).join("\n")) |
| 426 | ) |
| 427 | .setStatus("Success") |
| 428 | .setEmoji("GUILD.TICKET.OPEN") |
| 429 | ], components: [ |
| 430 | selectPane, |
| 431 | new MessageActionRow().addComponents([ |
| 432 | new MessageButton() |
| 433 | .setLabel("Back") |
| 434 | .setEmoji(getEmojiByName("CONTROL.LEFT", "id")) |
| 435 | .setStyle("PRIMARY") |
| 436 | .setCustomId("back"), |
| 437 | new MessageButton() |
| 438 | .setLabel("Switch to custom types") |
| 439 | .setStyle("SECONDARY") |
pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame^] | 440 | .setCustomId("switchToCustom") |
pineafan | 6702cef | 2022-06-13 17:52:37 +0100 | [diff] [blame] | 441 | ]) |
| 442 | ] |
| 443 | }); |
| 444 | } |
| 445 | let i; |
| 446 | try { |
pineafan | c6158ab | 2022-06-17 16:34:07 +0100 | [diff] [blame] | 447 | i = await m.awaitMessageComponent({ time: 300000 }); |
pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame^] | 448 | } catch (e) { break; } |
pineafan | e23c4ec | 2022-07-27 21:56:27 +0100 | [diff] [blame] | 449 | if (i.component.customId === "types") { |
pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame^] | 450 | i.deferUpdate(); |
| 451 | const types = toHexInteger(i.values, ticketTypes); |
| 452 | await client.database.guilds.write(interaction.guild.id, { "tickets.types": types }); |
pineafan | 6702cef | 2022-06-13 17:52:37 +0100 | [diff] [blame] | 453 | data.types = types; |
pineafan | e23c4ec | 2022-07-27 21:56:27 +0100 | [diff] [blame] | 454 | } else if (i.component.customId === "removeTypes") { |
pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame^] | 455 | i.deferUpdate(); |
| 456 | const types = i.values; |
pineafan | 6702cef | 2022-06-13 17:52:37 +0100 | [diff] [blame] | 457 | let customTypes = data.customTypes; |
| 458 | if (customTypes) { |
| 459 | customTypes = customTypes.filter((t) => !types.includes(t)); |
| 460 | customTypes = customTypes.length > 0 ? customTypes : null; |
pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame^] | 461 | await client.database.guilds.write(interaction.guild.id, { "tickets.customTypes": customTypes }); |
pineafan | 6702cef | 2022-06-13 17:52:37 +0100 | [diff] [blame] | 462 | data.customTypes = customTypes; |
| 463 | } |
pineafan | e23c4ec | 2022-07-27 21:56:27 +0100 | [diff] [blame] | 464 | } else if (i.component.customId === "addType") { |
pineafan | 6702cef | 2022-06-13 17:52:37 +0100 | [diff] [blame] | 465 | 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] | 466 | new MessageActionRow<TextInputComponent>().addComponents(new TextInputComponent() |
pineafan | 6702cef | 2022-06-13 17:52:37 +0100 | [diff] [blame] | 467 | .setCustomId("type") |
| 468 | .setLabel("Name") |
| 469 | .setMaxLength(100) |
| 470 | .setMinLength(1) |
| 471 | .setPlaceholder("E.g. \"Server Idea\"") |
| 472 | .setRequired(true) |
| 473 | .setStyle("SHORT") |
| 474 | ) |
pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame^] | 475 | )); |
pineafan | 6702cef | 2022-06-13 17:52:37 +0100 | [diff] [blame] | 476 | await interaction.editReply({ |
pineafan | 4edb776 | 2022-06-26 19:21:04 +0100 | [diff] [blame] | 477 | embeds: [new EmojiEmbed() |
pineafan | 6702cef | 2022-06-13 17:52:37 +0100 | [diff] [blame] | 478 | .setTitle("Tickets > Types") |
| 479 | .setDescription("Modal opened. If you can't see it, click back and try again.") |
| 480 | .setStatus("Success") |
| 481 | .setEmoji("GUILD.TICKET.OPEN") |
| 482 | ], components: [new MessageActionRow().addComponents([new MessageButton() |
| 483 | .setLabel("Back") |
| 484 | .setEmoji(getEmojiByName("CONTROL.LEFT", "id")) |
| 485 | .setStyle("PRIMARY") |
| 486 | .setCustomId("back") |
| 487 | ])] |
| 488 | }); |
pineafan | 4edb776 | 2022-06-26 19:21:04 +0100 | [diff] [blame] | 489 | let out; |
pineafan | 6702cef | 2022-06-13 17:52:37 +0100 | [diff] [blame] | 490 | try { |
pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame^] | 491 | out = await modalInteractionCollector(m, (m) => m.channel.id === interaction.channel.id, (m) => m.customId === "addType"); |
| 492 | } catch (e) { continue; } |
pineafan | 6702cef | 2022-06-13 17:52:37 +0100 | [diff] [blame] | 493 | if (out.fields) { |
| 494 | let toAdd = out.fields.getTextInputValue("type"); |
pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame^] | 495 | if (!toAdd) { continue; } |
| 496 | toAdd = toAdd.substring(0, 80); |
pineafan | 6702cef | 2022-06-13 17:52:37 +0100 | [diff] [blame] | 497 | try { |
pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame^] | 498 | await client.database.guilds.append(interaction.guild.id, "tickets.customTypes", toAdd); |
| 499 | } catch { continue; } |
pineafan | 6702cef | 2022-06-13 17:52:37 +0100 | [diff] [blame] | 500 | data.customTypes = data.customTypes || []; |
| 501 | if (!data.customTypes.includes(toAdd)) { |
| 502 | data.customTypes.push(toAdd); |
| 503 | } |
pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame^] | 504 | } else { continue; } |
pineafan | e23c4ec | 2022-07-27 21:56:27 +0100 | [diff] [blame] | 505 | } else if (i.component.customId === "switchToDefault") { |
pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame^] | 506 | i.deferUpdate(); |
| 507 | await client.database.guilds.write(interaction.guild.id, { "tickets.useCustom": false }, []); |
pineafan | 6702cef | 2022-06-13 17:52:37 +0100 | [diff] [blame] | 508 | data.useCustom = false; |
pineafan | e23c4ec | 2022-07-27 21:56:27 +0100 | [diff] [blame] | 509 | } else if (i.component.customId === "switchToCustom") { |
pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame^] | 510 | i.deferUpdate(); |
| 511 | await client.database.guilds.write(interaction.guild.id, { "tickets.useCustom": true }, []); |
pineafan | 6702cef | 2022-06-13 17:52:37 +0100 | [diff] [blame] | 512 | data.useCustom = true; |
| 513 | } else { |
pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame^] | 514 | i.deferUpdate(); |
| 515 | break; |
pineafan | 6702cef | 2022-06-13 17:52:37 +0100 | [diff] [blame] | 516 | } |
| 517 | } |
pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame^] | 518 | return data; |
pineafan | 6702cef | 2022-06-13 17:52:37 +0100 | [diff] [blame] | 519 | } |
| 520 | |
| 521 | |
pineafan | 4f164f3 | 2022-02-26 22:07:12 +0000 | [diff] [blame] | 522 | const check = (interaction: CommandInteraction, defaultCheck: WrappedCheck) => { |
pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame^] | 523 | const member = (interaction.member as Discord.GuildMember); |
| 524 | 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] | 525 | return true; |
pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame^] | 526 | }; |
pineafan | 4f164f3 | 2022-02-26 22:07:12 +0000 | [diff] [blame] | 527 | |
| 528 | export { command }; |
| 529 | export { callback }; |
| 530 | export { check }; |