pineafan | ad54d75 | 2022-04-18 19:01:43 +0100 | [diff] [blame^] | 1 | import Discord, { MessageActionRow, MessageButton } from "discord.js"; |
| 2 | import { tickets, toHexArray } from "../../utils/calculate.js"; |
| 3 | import readConfig from "../../utils/readConfig.js"; |
| 4 | import generateEmojiEmbed from "../../utils/generateEmojiEmbed.js"; |
| 5 | import getEmojiByName from "../../utils/getEmojiByName.js"; |
| 6 | |
| 7 | function capitalize(s: string) { |
| 8 | s = s.replace(/([A-Z])/g, ' $1'); |
| 9 | return s.length < 3 ? s.toUpperCase() : s[0].toUpperCase() + s.slice(1).toLowerCase(); |
| 10 | } |
| 11 | |
| 12 | export default async function (interaction) { |
| 13 | // @ts-ignore |
| 14 | const { log, NucleusColors, entry, renderUser, renderChannel, renderDelta } = interaction.client.logger |
| 15 | |
| 16 | let config = await readConfig(interaction.guild.id); |
| 17 | if (!config.tickets.enabled || !config.tickets.category) { |
| 18 | return await interaction.reply({embeds: [new generateEmojiEmbed() |
| 19 | .setTitle("Tickets are disabled") |
| 20 | .setDescription("Please enable tickets in the configuration to use this command.") |
| 21 | .setStatus("Danger") |
| 22 | .setEmoji("CONTROL.BLOCKCROSS") |
| 23 | ], ephemeral: true}); |
| 24 | } |
| 25 | let category = interaction.guild.channels.cache.get(config.tickets.category) as Discord.CategoryChannel; |
| 26 | let count = 0; |
| 27 | category.children.forEach(element => { |
| 28 | if (!(element.type == "GUILD_TEXT")) return; |
| 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 generateEmojiEmbed() |
| 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; |
| 44 | let custom = false |
| 45 | if (config.tickets.customTypes) { ticketTypes = config.tickets.customTypes; custom = true } |
| 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") |
| 57 | .setCustomId(type) |
| 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 += 4) { |
| 67 | splitFormattedTicketTypes.push(new MessageActionRow().addComponents(formattedTicketTypes.slice(i, i + 4))); |
| 68 | } |
| 69 | let m = await interaction.reply({embeds: [new generateEmojiEmbed() |
| 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: 2.5 * 60 * 1000}); |
| 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) |
| 88 | .setStyle(chosenType == type ? "SUCCESS" : "SECONDARY") |
| 89 | .setCustomId(type) |
| 90 | .setDisabled(true) |
| 91 | } else { |
| 92 | return new MessageButton() |
| 93 | .setLabel(capitalize(type)) |
| 94 | .setStyle(chosenType == type ? "SUCCESS" : "SECONDARY") |
| 95 | .setCustomId(type) |
| 96 | .setEmoji(getEmojiByName(("TICKETS." + type.toString().toUpperCase()), "id")) |
| 97 | .setDisabled(true) |
| 98 | } |
| 99 | }); |
| 100 | for (let i = 0; i < formattedTicketTypes.length; i += 4) { |
| 101 | splitFormattedTicketTypes.push(new MessageActionRow().addComponents(formattedTicketTypes.slice(i, i + 4))); |
| 102 | } |
| 103 | component.update({embeds: [new generateEmojiEmbed() |
| 104 | .setTitle("Create Ticket") |
| 105 | .setDescription("Select a ticket type") |
| 106 | .setStatus("Success") |
| 107 | .setEmoji("GUILD.TICKET.OPEN") |
| 108 | ], components: splitFormattedTicketTypes}); |
| 109 | } else { |
| 110 | chosenType = null |
| 111 | await interaction.reply({embeds: [new generateEmojiEmbed() |
| 112 | .setTitle("Create Ticket") |
| 113 | .setEmoji("GUILD.TICKET.OPEN") |
| 114 | ], ephemeral: true, components: splitFormattedTicketTypes}) |
| 115 | } |
| 116 | let overwrites = [{ |
| 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 | if (config.tickets.supportRole != null) { |
| 122 | overwrites.push({ |
| 123 | id: interaction.guild.roles.cache.get(config.tickets.supportRole), |
| 124 | allow: ["VIEW_CHANNEL", "SEND_MESSAGES", "ATTACH_FILES", "ADD_REACTIONS", "READ_MESSAGE_HISTORY"], |
| 125 | type: "role" |
| 126 | }) |
| 127 | } |
| 128 | |
| 129 | let c; |
| 130 | try { |
| 131 | c = await interaction.guild.channels.create(interaction.member.user.username, { |
| 132 | type: "GUILD_TEXT", |
| 133 | topic: `${interaction.member.user.id} Active`, |
| 134 | parent: config.tickets.category, |
| 135 | nsfw: false, |
| 136 | permissionOverwrites: (overwrites as Discord.OverwriteResolvable[]), |
| 137 | reason: "Creating ticket" |
| 138 | }) |
| 139 | } catch (e) { |
| 140 | return await interaction.editReply({embeds: [new generateEmojiEmbed() |
| 141 | .setTitle("Create Ticket") |
| 142 | .setDescription("Failed to create ticket") |
| 143 | .setStatus("Danger") |
| 144 | .setEmoji("CONTROL.BLOCKCROSS") |
| 145 | ]}); |
| 146 | } |
| 147 | try { |
| 148 | await c.send( |
| 149 | { |
| 150 | content: (`<@${interaction.member.user.id}>` + (config.tickets.supportRole != null ? ` • <@&${config.tickets.supportRole}>` : "")), |
| 151 | allowedMentions: { |
| 152 | users: [(interaction.member as Discord.GuildMember).id], |
| 153 | roles: (config.tickets.supportRole != null ? [config.tickets.supportRole] : []) |
| 154 | } |
| 155 | } |
| 156 | ) |
| 157 | let content = interaction.options ? interaction.options.getString("message") || "" : ""; |
| 158 | if (content) content = `**Message:**\n> ${content}\n`; |
| 159 | let emoji = custom ? "" : getEmojiByName("TICKETS." + chosenType.toUpperCase()); |
| 160 | await c.send({ embeds: [new generateEmojiEmbed() |
| 161 | .setTitle("New Ticket") |
| 162 | .setDescription( |
| 163 | `Ticket created by <@${interaction.member.user.id}>\n` + |
| 164 | `**Support type:** ${chosenType != null ? (emoji) + " " + capitalize(chosenType) : "General"}\n` + |
| 165 | `**Ticket ID:** \`${c.id}\`\n${content}\n` + |
| 166 | `Type \`/ticket close\` to archive this ticket.`, |
| 167 | ) |
| 168 | .setStatus("Success") |
| 169 | .setEmoji("GUILD.TICKET.OPEN") |
| 170 | ], components: [new MessageActionRow().addComponents([new MessageButton() |
| 171 | .setLabel("Close") |
| 172 | .setStyle("DANGER") |
| 173 | .setCustomId("closeticket") |
| 174 | .setEmoji(getEmojiByName("CONTROL.CROSS", "id")) |
| 175 | ])]}) |
| 176 | let data = { |
| 177 | meta:{ |
| 178 | type: 'ticketCreate', |
| 179 | displayName: 'Ticket Created', |
| 180 | calculateType: true, |
| 181 | color: NucleusColors.green, |
| 182 | emoji: 'GUILD.TICKET.OPEN', |
| 183 | timestamp: new Date().getTime() |
| 184 | }, |
| 185 | list: { |
| 186 | ticketFor: entry(interaction.member.user.id, renderUser(interaction.member.user)), |
| 187 | created: entry(new Date().getTime(), renderDelta(new Date().getTime())), |
| 188 | ticketChannel: entry(c.id, renderChannel(c)), |
| 189 | }, |
| 190 | hidden: { |
| 191 | guild: interaction.guild.id |
| 192 | } |
| 193 | } |
| 194 | log(data, interaction.client); |
| 195 | } catch (e) { console.log(e)} |
| 196 | await interaction.editReply({embeds: [new generateEmojiEmbed() |
| 197 | .setTitle("Create Ticket") |
| 198 | .setDescription(`Ticket created. You can view it here: <#${c.id}>`) |
| 199 | .setStatus("Success") |
| 200 | .setEmoji("GUILD.TICKET.OPEN") |
| 201 | ], components: splitFormattedTicketTypes}); |
| 202 | } |