pineafan | 813bdf4 | 2022-07-24 10:39:10 +0100 | [diff] [blame] | 1 | import Discord, { MessageActionRow, MessageButton } from "discord.js"; |
| 2 | import { tickets, toHexArray } from "../../utils/calculate.js"; |
| 3 | import client from "../../utils/client.js"; |
| 4 | import EmojiEmbed from "../../utils/generateEmojiEmbed.js"; |
| 5 | import getEmojiByName from "../../utils/getEmojiByName.js"; |
| 6 | |
| 7 | function capitalize(s: string) { |
pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame^] | 8 | s = s.replace(/([A-Z])/g, " $1"); |
pineafan | 813bdf4 | 2022-07-24 10:39:10 +0100 | [diff] [blame] | 9 | return s.length < 3 ? s.toUpperCase() : s[0].toUpperCase() + s.slice(1).toLowerCase(); |
| 10 | } |
| 11 | |
| 12 | export default async function (interaction) { |
pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame^] | 13 | const { log, NucleusColors, entry, renderUser, renderChannel, renderDelta } = client.logger; |
pineafan | 813bdf4 | 2022-07-24 10:39:10 +0100 | [diff] [blame] | 14 | |
pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame^] | 15 | const config = await client.database.guilds.read(interaction.guild.id); |
pineafan | 813bdf4 | 2022-07-24 10:39:10 +0100 | [diff] [blame] | 16 | if (!config.tickets.enabled || !config.tickets.category) { |
| 17 | return await interaction.reply({embeds: [new EmojiEmbed() |
| 18 | .setTitle("Tickets are disabled") |
| 19 | .setDescription("Please enable tickets in the configuration to use this command.") |
pineafan | 41d9356 | 2022-07-30 22:10:15 +0100 | [diff] [blame] | 20 | .setFooter({text: interaction.member.permissions.has("MANAGE_GUILD") ? "You can enable it by running /settings tickets" : ""}) |
pineafan | 813bdf4 | 2022-07-24 10:39:10 +0100 | [diff] [blame] | 21 | .setStatus("Danger") |
| 22 | .setEmoji("CONTROL.BLOCKCROSS") |
| 23 | ], ephemeral: true}); |
| 24 | } |
pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame^] | 25 | const category = interaction.guild.channels.cache.get(config.tickets.category) as Discord.CategoryChannel; |
pineafan | 813bdf4 | 2022-07-24 10:39:10 +0100 | [diff] [blame] | 26 | let count = 0; |
| 27 | category.children.forEach(element => { |
pineafan | e23c4ec | 2022-07-27 21:56:27 +0100 | [diff] [blame] | 28 | if (!(element.type === "GUILD_TEXT")) return; |
pineafan | 813bdf4 | 2022-07-24 10:39:10 +0100 | [diff] [blame] | 29 | if ((element as Discord.TextChannel).topic.includes(`${interaction.member.user.id}`)) { |
| 30 | if ((element as Discord.TextChannel).topic.endsWith("Active")) { |
| 31 | count++; |
| 32 | } |
| 33 | } |
| 34 | }); |
| 35 | if (count >= config.tickets.maxTickets) { |
| 36 | return await interaction.reply({embeds: [new EmojiEmbed() |
| 37 | .setTitle("Create Ticket") |
| 38 | .setDescription(`You have reached the maximum amount of tickets (${config.tickets.maxTickets}). Please close one of your active tickets before creating a new one.`) |
| 39 | .setStatus("Danger") |
| 40 | .setEmoji("CONTROL.BLOCKCROSS") |
| 41 | ], ephemeral: true}); |
| 42 | } |
| 43 | let ticketTypes; |
pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame^] | 44 | let custom = false; |
| 45 | if (config.tickets.customTypes && config.tickets.useCustom) { ticketTypes = config.tickets.customTypes; custom = true; } |
pineafan | 813bdf4 | 2022-07-24 10:39:10 +0100 | [diff] [blame] | 46 | else if (config.tickets.types) ticketTypes = toHexArray(config.tickets.types, tickets); |
| 47 | else ticketTypes = []; |
| 48 | let chosenType; |
| 49 | let splitFormattedTicketTypes = []; |
| 50 | if (ticketTypes.length > 0) { |
| 51 | let formattedTicketTypes = []; |
| 52 | formattedTicketTypes = ticketTypes.map(type => { |
| 53 | if (custom) { |
| 54 | return new MessageButton() |
| 55 | .setLabel(type) |
| 56 | .setStyle("PRIMARY") |
pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame^] | 57 | .setCustomId(type); |
pineafan | 813bdf4 | 2022-07-24 10:39:10 +0100 | [diff] [blame] | 58 | } else { |
| 59 | return new MessageButton() |
| 60 | .setLabel(capitalize(type)) |
| 61 | .setStyle("PRIMARY") |
| 62 | .setCustomId(type) |
| 63 | .setEmoji(getEmojiByName(("TICKETS." + type.toString().toUpperCase()), "id")); |
| 64 | } |
| 65 | }); |
| 66 | for (let i = 0; i < formattedTicketTypes.length; i += 5) { |
| 67 | splitFormattedTicketTypes.push(new MessageActionRow().addComponents(formattedTicketTypes.slice(i, i + 5))); |
| 68 | } |
pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame^] | 69 | const m = await interaction.reply({embeds: [new EmojiEmbed() |
pineafan | 813bdf4 | 2022-07-24 10:39:10 +0100 | [diff] [blame] | 70 | .setTitle("Create Ticket") |
| 71 | .setDescription("Select a ticket type") |
| 72 | .setStatus("Success") |
| 73 | .setEmoji("GUILD.TICKET.OPEN") |
| 74 | ], ephemeral: true, fetchReply: true, components: splitFormattedTicketTypes}); |
| 75 | let component; |
| 76 | try { |
| 77 | component = await (m as Discord.Message).awaitMessageComponent({time: 300000}); |
| 78 | } catch (e) { |
| 79 | return; |
| 80 | } |
| 81 | chosenType = component.customId; |
| 82 | splitFormattedTicketTypes = []; |
| 83 | formattedTicketTypes = []; |
| 84 | formattedTicketTypes = ticketTypes.map(type => { |
| 85 | if (custom) { |
| 86 | return new MessageButton() |
| 87 | .setLabel(type) |
pineafan | e23c4ec | 2022-07-27 21:56:27 +0100 | [diff] [blame] | 88 | .setStyle(chosenType === type ? "SUCCESS" : "SECONDARY") |
pineafan | 813bdf4 | 2022-07-24 10:39:10 +0100 | [diff] [blame] | 89 | .setCustomId(type) |
pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame^] | 90 | .setDisabled(true); |
| 91 | } else { |
| 92 | return new MessageButton() |
pineafan | 813bdf4 | 2022-07-24 10:39:10 +0100 | [diff] [blame] | 93 | .setLabel(capitalize(type)) |
pineafan | e23c4ec | 2022-07-27 21:56:27 +0100 | [diff] [blame] | 94 | .setStyle(chosenType === type ? "SUCCESS" : "SECONDARY") |
pineafan | 813bdf4 | 2022-07-24 10:39:10 +0100 | [diff] [blame] | 95 | .setCustomId(type) |
| 96 | .setEmoji(getEmojiByName(("TICKETS." + type.toString().toUpperCase()), "id")) |
pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame^] | 97 | .setDisabled(true); |
pineafan | 813bdf4 | 2022-07-24 10:39:10 +0100 | [diff] [blame] | 98 | } |
| 99 | }); |
| 100 | for (let i = 0; i < formattedTicketTypes.length; i += 5) { |
| 101 | splitFormattedTicketTypes.push(new MessageActionRow().addComponents(formattedTicketTypes.slice(i, i + 5))); |
| 102 | } |
| 103 | component.update({embeds: [new EmojiEmbed() |
| 104 | .setTitle("Create Ticket") |
| 105 | .setDescription("Select a ticket type") |
| 106 | .setStatus("Success") |
| 107 | .setEmoji("GUILD.TICKET.OPEN") |
| 108 | ], components: splitFormattedTicketTypes}); |
| 109 | } else { |
pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame^] | 110 | chosenType = null; |
pineafan | 813bdf4 | 2022-07-24 10:39:10 +0100 | [diff] [blame] | 111 | await interaction.reply({embeds: [new EmojiEmbed() |
| 112 | .setTitle("Create Ticket") |
| 113 | .setEmoji("GUILD.TICKET.OPEN") |
pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame^] | 114 | ], ephemeral: true, components: splitFormattedTicketTypes}); |
pineafan | 813bdf4 | 2022-07-24 10:39:10 +0100 | [diff] [blame] | 115 | } |
pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame^] | 116 | const overwrites = [{ |
pineafan | 813bdf4 | 2022-07-24 10:39:10 +0100 | [diff] [blame] | 117 | id: interaction.member, |
| 118 | allow: ["VIEW_CHANNEL", "SEND_MESSAGES", "ATTACH_FILES", "ADD_REACTIONS", "READ_MESSAGE_HISTORY"], |
| 119 | type: "member" |
| 120 | }] as Discord.OverwriteResolvable[]; |
| 121 | overwrites.push({ |
| 122 | id: interaction.guild.roles.everyone, |
| 123 | deny: ["VIEW_CHANNEL"], |
| 124 | type: "role" |
pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame^] | 125 | }); |
pineafan | e23c4ec | 2022-07-27 21:56:27 +0100 | [diff] [blame] | 126 | if (config.tickets.supportRole !== null) { |
pineafan | 813bdf4 | 2022-07-24 10:39:10 +0100 | [diff] [blame] | 127 | overwrites.push({ |
| 128 | id: interaction.guild.roles.cache.get(config.tickets.supportRole), |
| 129 | allow: ["VIEW_CHANNEL", "SEND_MESSAGES", "ATTACH_FILES", "ADD_REACTIONS", "READ_MESSAGE_HISTORY"], |
| 130 | type: "role" |
pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame^] | 131 | }); |
pineafan | 813bdf4 | 2022-07-24 10:39:10 +0100 | [diff] [blame] | 132 | } |
| 133 | |
| 134 | let c; |
| 135 | try { |
| 136 | c = await interaction.guild.channels.create(interaction.member.user.username, { |
| 137 | type: "GUILD_TEXT", |
| 138 | topic: `${interaction.member.user.id} Active`, |
| 139 | parent: config.tickets.category, |
| 140 | nsfw: false, |
| 141 | permissionOverwrites: (overwrites as Discord.OverwriteResolvable[]), |
| 142 | reason: "Creating ticket" |
pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame^] | 143 | }); |
pineafan | 813bdf4 | 2022-07-24 10:39:10 +0100 | [diff] [blame] | 144 | } catch (e) { |
| 145 | return await interaction.editReply({embeds: [new EmojiEmbed() |
| 146 | .setTitle("Create Ticket") |
| 147 | .setDescription("Failed to create ticket") |
| 148 | .setStatus("Danger") |
| 149 | .setEmoji("CONTROL.BLOCKCROSS") |
| 150 | ]}); |
| 151 | } |
| 152 | try { |
| 153 | await c.send( |
| 154 | { |
pineafan | e23c4ec | 2022-07-27 21:56:27 +0100 | [diff] [blame] | 155 | content: (`<@${interaction.member.user.id}>` + (config.tickets.supportRole !== null ? ` • <@&${config.tickets.supportRole}>` : "")), |
pineafan | 813bdf4 | 2022-07-24 10:39:10 +0100 | [diff] [blame] | 156 | allowedMentions: { |
| 157 | users: [(interaction.member as Discord.GuildMember).id], |
pineafan | e23c4ec | 2022-07-27 21:56:27 +0100 | [diff] [blame] | 158 | roles: (config.tickets.supportRole !== null ? [config.tickets.supportRole] : []) |
pineafan | 813bdf4 | 2022-07-24 10:39:10 +0100 | [diff] [blame] | 159 | } |
| 160 | } |
pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame^] | 161 | ); |
pineafan | 813bdf4 | 2022-07-24 10:39:10 +0100 | [diff] [blame] | 162 | let content = interaction.options ? interaction.options.getString("message") || "" : ""; |
| 163 | if (content) content = `**Message:**\n> ${content}\n`; |
pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame^] | 164 | const emoji = custom ? "" : getEmojiByName("TICKETS." + chosenType.toUpperCase()); |
pineafan | 813bdf4 | 2022-07-24 10:39:10 +0100 | [diff] [blame] | 165 | await c.send({ embeds: [new EmojiEmbed() |
| 166 | .setTitle("New Ticket") |
| 167 | .setDescription( |
| 168 | `Ticket created by <@${interaction.member.user.id}>\n` + |
pineafan | e23c4ec | 2022-07-27 21:56:27 +0100 | [diff] [blame] | 169 | `**Support type:** ${chosenType !== null ? (emoji) + " " + capitalize(chosenType) : "General"}\n` + |
pineafan | 813bdf4 | 2022-07-24 10:39:10 +0100 | [diff] [blame] | 170 | `**Ticket ID:** \`${c.id}\`\n${content}\n` + |
pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame^] | 171 | "Type `/ticket close` to close this ticket." |
pineafan | 813bdf4 | 2022-07-24 10:39:10 +0100 | [diff] [blame] | 172 | ) |
| 173 | .setStatus("Success") |
| 174 | .setEmoji("GUILD.TICKET.OPEN") |
| 175 | ], components: [new MessageActionRow().addComponents([new MessageButton() |
| 176 | .setLabel("Close") |
| 177 | .setStyle("DANGER") |
| 178 | .setCustomId("closeticket") |
| 179 | .setEmoji(getEmojiByName("CONTROL.CROSS", "id")) |
pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame^] | 180 | ])]}); |
| 181 | const data = { |
pineafan | 813bdf4 | 2022-07-24 10:39:10 +0100 | [diff] [blame] | 182 | meta:{ |
pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame^] | 183 | type: "ticketCreate", |
| 184 | displayName: "Ticket Created", |
pineafan | 813bdf4 | 2022-07-24 10:39:10 +0100 | [diff] [blame] | 185 | calculateType: "ticketUpdate", |
| 186 | color: NucleusColors.green, |
pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame^] | 187 | emoji: "GUILD.TICKET.OPEN", |
pineafan | 813bdf4 | 2022-07-24 10:39:10 +0100 | [diff] [blame] | 188 | timestamp: new Date().getTime() |
| 189 | }, |
| 190 | list: { |
| 191 | ticketFor: entry(interaction.member.user.id, renderUser(interaction.member.user)), |
| 192 | created: entry(new Date().getTime(), renderDelta(new Date().getTime())), |
pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame^] | 193 | ticketChannel: entry(c.id, renderChannel(c)) |
pineafan | 813bdf4 | 2022-07-24 10:39:10 +0100 | [diff] [blame] | 194 | }, |
| 195 | hidden: { |
| 196 | guild: interaction.guild.id |
| 197 | } |
pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame^] | 198 | }; |
pineafan | 813bdf4 | 2022-07-24 10:39:10 +0100 | [diff] [blame] | 199 | log(data); |
pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame^] | 200 | } catch (e) { console.log(e);} |
pineafan | 813bdf4 | 2022-07-24 10:39:10 +0100 | [diff] [blame] | 201 | await interaction.editReply({embeds: [new EmojiEmbed() |
| 202 | .setTitle("Create Ticket") |
| 203 | .setDescription(`Ticket created. You can view it here: <#${c.id}>`) |
| 204 | .setStatus("Success") |
| 205 | .setEmoji("GUILD.TICKET.OPEN") |
| 206 | ], components: splitFormattedTicketTypes}); |
| 207 | } |