blob: 6c8d42cdb91835b3d35acbda7bba29e0326a9c22 [file] [log] [blame]
pineafan813bdf42022-07-24 10:39:10 +01001import Discord, { MessageActionRow, MessageButton } from "discord.js";
2import { tickets, toHexArray } from "../../utils/calculate.js";
3import client from "../../utils/client.js";
4import EmojiEmbed from "../../utils/generateEmojiEmbed.js";
5import getEmojiByName from "../../utils/getEmojiByName.js";
6
7function capitalize(s: string) {
pineafan63fc5e22022-08-04 22:04:10 +01008 s = s.replace(/([A-Z])/g, " $1");
pineafan813bdf42022-07-24 10:39:10 +01009 return s.length < 3 ? s.toUpperCase() : s[0].toUpperCase() + s.slice(1).toLowerCase();
10}
11
12export default async function (interaction) {
pineafan63fc5e22022-08-04 22:04:10 +010013 const { log, NucleusColors, entry, renderUser, renderChannel, renderDelta } = client.logger;
pineafan813bdf42022-07-24 10:39:10 +010014
pineafan63fc5e22022-08-04 22:04:10 +010015 const config = await client.database.guilds.read(interaction.guild.id);
pineafan813bdf42022-07-24 10:39:10 +010016 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.")
pineafan41d93562022-07-30 22:10:15 +010020 .setFooter({text: interaction.member.permissions.has("MANAGE_GUILD") ? "You can enable it by running /settings tickets" : ""})
pineafan813bdf42022-07-24 10:39:10 +010021 .setStatus("Danger")
22 .setEmoji("CONTROL.BLOCKCROSS")
23 ], ephemeral: true});
24 }
pineafan63fc5e22022-08-04 22:04:10 +010025 const category = interaction.guild.channels.cache.get(config.tickets.category) as Discord.CategoryChannel;
pineafan813bdf42022-07-24 10:39:10 +010026 let count = 0;
27 category.children.forEach(element => {
pineafane23c4ec2022-07-27 21:56:27 +010028 if (!(element.type === "GUILD_TEXT")) return;
pineafan813bdf42022-07-24 10:39:10 +010029 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;
pineafan63fc5e22022-08-04 22:04:10 +010044 let custom = false;
45 if (config.tickets.customTypes && config.tickets.useCustom) { ticketTypes = config.tickets.customTypes; custom = true; }
pineafan813bdf42022-07-24 10:39:10 +010046 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")
pineafan63fc5e22022-08-04 22:04:10 +010057 .setCustomId(type);
pineafan813bdf42022-07-24 10:39:10 +010058 } 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 }
pineafan63fc5e22022-08-04 22:04:10 +010069 const m = await interaction.reply({embeds: [new EmojiEmbed()
pineafan813bdf42022-07-24 10:39:10 +010070 .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)
pineafane23c4ec2022-07-27 21:56:27 +010088 .setStyle(chosenType === type ? "SUCCESS" : "SECONDARY")
pineafan813bdf42022-07-24 10:39:10 +010089 .setCustomId(type)
pineafan63fc5e22022-08-04 22:04:10 +010090 .setDisabled(true);
91 } else {
92 return new MessageButton()
pineafan813bdf42022-07-24 10:39:10 +010093 .setLabel(capitalize(type))
pineafane23c4ec2022-07-27 21:56:27 +010094 .setStyle(chosenType === type ? "SUCCESS" : "SECONDARY")
pineafan813bdf42022-07-24 10:39:10 +010095 .setCustomId(type)
96 .setEmoji(getEmojiByName(("TICKETS." + type.toString().toUpperCase()), "id"))
pineafan63fc5e22022-08-04 22:04:10 +010097 .setDisabled(true);
pineafan813bdf42022-07-24 10:39:10 +010098 }
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 {
pineafan63fc5e22022-08-04 22:04:10 +0100110 chosenType = null;
pineafan813bdf42022-07-24 10:39:10 +0100111 await interaction.reply({embeds: [new EmojiEmbed()
112 .setTitle("Create Ticket")
113 .setEmoji("GUILD.TICKET.OPEN")
pineafan63fc5e22022-08-04 22:04:10 +0100114 ], ephemeral: true, components: splitFormattedTicketTypes});
pineafan813bdf42022-07-24 10:39:10 +0100115 }
pineafan63fc5e22022-08-04 22:04:10 +0100116 const overwrites = [{
pineafan813bdf42022-07-24 10:39:10 +0100117 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"
pineafan63fc5e22022-08-04 22:04:10 +0100125 });
pineafane23c4ec2022-07-27 21:56:27 +0100126 if (config.tickets.supportRole !== null) {
pineafan813bdf42022-07-24 10:39:10 +0100127 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"
pineafan63fc5e22022-08-04 22:04:10 +0100131 });
pineafan813bdf42022-07-24 10:39:10 +0100132 }
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"
pineafan63fc5e22022-08-04 22:04:10 +0100143 });
pineafan813bdf42022-07-24 10:39:10 +0100144 } 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 {
pineafane23c4ec2022-07-27 21:56:27 +0100155 content: (`<@${interaction.member.user.id}>` + (config.tickets.supportRole !== null ? ` • <@&${config.tickets.supportRole}>` : "")),
pineafan813bdf42022-07-24 10:39:10 +0100156 allowedMentions: {
157 users: [(interaction.member as Discord.GuildMember).id],
pineafane23c4ec2022-07-27 21:56:27 +0100158 roles: (config.tickets.supportRole !== null ? [config.tickets.supportRole] : [])
pineafan813bdf42022-07-24 10:39:10 +0100159 }
160 }
pineafan63fc5e22022-08-04 22:04:10 +0100161 );
pineafan813bdf42022-07-24 10:39:10 +0100162 let content = interaction.options ? interaction.options.getString("message") || "" : "";
163 if (content) content = `**Message:**\n> ${content}\n`;
pineafan63fc5e22022-08-04 22:04:10 +0100164 const emoji = custom ? "" : getEmojiByName("TICKETS." + chosenType.toUpperCase());
pineafan813bdf42022-07-24 10:39:10 +0100165 await c.send({ embeds: [new EmojiEmbed()
166 .setTitle("New Ticket")
167 .setDescription(
168 `Ticket created by <@${interaction.member.user.id}>\n` +
pineafane23c4ec2022-07-27 21:56:27 +0100169 `**Support type:** ${chosenType !== null ? (emoji) + " " + capitalize(chosenType) : "General"}\n` +
pineafan813bdf42022-07-24 10:39:10 +0100170 `**Ticket ID:** \`${c.id}\`\n${content}\n` +
pineafan63fc5e22022-08-04 22:04:10 +0100171 "Type `/ticket close` to close this ticket."
pineafan813bdf42022-07-24 10:39:10 +0100172 )
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"))
pineafan63fc5e22022-08-04 22:04:10 +0100180 ])]});
181 const data = {
pineafan813bdf42022-07-24 10:39:10 +0100182 meta:{
pineafan63fc5e22022-08-04 22:04:10 +0100183 type: "ticketCreate",
184 displayName: "Ticket Created",
pineafan813bdf42022-07-24 10:39:10 +0100185 calculateType: "ticketUpdate",
186 color: NucleusColors.green,
pineafan63fc5e22022-08-04 22:04:10 +0100187 emoji: "GUILD.TICKET.OPEN",
pineafan813bdf42022-07-24 10:39:10 +0100188 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())),
pineafan63fc5e22022-08-04 22:04:10 +0100193 ticketChannel: entry(c.id, renderChannel(c))
pineafan813bdf42022-07-24 10:39:10 +0100194 },
195 hidden: {
196 guild: interaction.guild.id
197 }
pineafan63fc5e22022-08-04 22:04:10 +0100198 };
pineafan813bdf42022-07-24 10:39:10 +0100199 log(data);
pineafan63fc5e22022-08-04 22:04:10 +0100200 } catch (e) { console.log(e);}
pineafan813bdf42022-07-24 10:39:10 +0100201 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}