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