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