blob: 3419763e53b19dfb2ee2313d1cb6f2636b5ce9ec [file] [log] [blame]
pineafan813bdf42022-07-24 10:39:10 +01001import Discord, { MessageButton, MessageActionRow } from "discord.js";
2import client from "../../utils/client.js";
3import EmojiEmbed from "../../utils/generateEmojiEmbed.js";
4import getEmojiByName from "../../utils/getEmojiByName.js";
5
6export default async function (interaction) {
pineafan63fc5e22022-08-04 22:04:10 +01007 const { log, NucleusColors, entry, renderUser, renderChannel, renderDelta } = client.logger;
pineafan813bdf42022-07-24 10:39:10 +01008
pineafan63fc5e22022-08-04 22:04:10 +01009 const config = await client.database.guilds.read(interaction.guild.id);
10 let thread = false; let threadChannel;
11 if (interaction.channel instanceof Discord.ThreadChannel) thread = true; threadChannel = interaction.channel as Discord.ThreadChannel;
12 const channel = (interaction.channel as Discord.TextChannel);
pineafane23c4ec2022-07-27 21:56:27 +010013 if (!channel.parent || config.tickets.category !== channel.parent.id || (thread ? (threadChannel.parent.parent.id !== config.tickets.category) : false)) {
pineafan813bdf42022-07-24 10:39:10 +010014 return interaction.reply({embeds: [new EmojiEmbed()
15 .setTitle("Deleting Ticket...")
16 .setDescription("This ticket is not in your tickets category, so cannot be deleted. You cannot run close in a thread.")
17 .setStatus("Danger")
18 .setEmoji("CONTROL.BLOCKCROSS")
19 ], ephemeral: true});
20 }
pineafan63fc5e22022-08-04 22:04:10 +010021 const status = channel.topic.split(" ")[1];
pineafane23c4ec2022-07-27 21:56:27 +010022 if (status === "Archived") {
pineafan813bdf42022-07-24 10:39:10 +010023 await interaction.reply({embeds: [new EmojiEmbed()
24 .setTitle("Delete Ticket")
25 .setDescription("Your ticket is being deleted...")
26 .setStatus("Danger")
27 .setEmoji("GUILD.TICKET.CLOSE")
28 ]});
pineafan63fc5e22022-08-04 22:04:10 +010029 const data = {
pineafan813bdf42022-07-24 10:39:10 +010030 meta:{
pineafan63fc5e22022-08-04 22:04:10 +010031 type: "ticketDeleted",
32 displayName: "Ticket Deleted",
pineafan813bdf42022-07-24 10:39:10 +010033 calculateType: "ticketUpdate",
34 color: NucleusColors.red,
pineafan63fc5e22022-08-04 22:04:10 +010035 emoji: "GUILD.TICKET.CLOSE",
pineafan813bdf42022-07-24 10:39:10 +010036 timestamp: new Date().getTime()
37 },
38 list: {
39 ticketFor: entry(channel.topic.split(" ")[0], renderUser((await interaction.guild.members.fetch(channel.topic.split(" ")[0])).user)),
40 deletedBy: entry(interaction.member.user.id, renderUser(interaction.member.user)),
41 deleted: entry(new Date().getTime(), renderDelta(new Date().getTime()))
42 },
43 hidden: {
44 guild: interaction.guild.id
45 }
pineafan63fc5e22022-08-04 22:04:10 +010046 };
pineafan813bdf42022-07-24 10:39:10 +010047 log(data);
48 interaction.channel.delete();
49 return;
pineafane23c4ec2022-07-27 21:56:27 +010050 } else if (status === "Active") {
pineafan813bdf42022-07-24 10:39:10 +010051 await interaction.reply({embeds: [new EmojiEmbed()
52 .setTitle("Close Ticket")
53 .setDescription("Your ticket is being closed...")
54 .setStatus("Warning")
55 .setEmoji("GUILD.TICKET.ARCHIVED")
56 ]});
pineafan63fc5e22022-08-04 22:04:10 +010057 const overwrites = [
pineafan813bdf42022-07-24 10:39:10 +010058 {
59 id: channel.topic.split(" ")[0],
60 deny: ["VIEW_CHANNEL"],
61 type: "member"
62 },
63 {
64 id: interaction.guild.id,
65 deny: ["VIEW_CHANNEL"],
66 type: "role"
67 }
68 ] as Discord.OverwriteResolvable[];
pineafane23c4ec2022-07-27 21:56:27 +010069 if (config.tickets.supportRole !== null) {
pineafan813bdf42022-07-24 10:39:10 +010070 overwrites.push({
71 id: interaction.guild.roles.cache.get(config.tickets.supportRole),
72 allow: ["VIEW_CHANNEL", "SEND_MESSAGES", "ATTACH_FILES", "ADD_REACTIONS", "READ_MESSAGE_HISTORY"],
73 type: "role"
pineafan63fc5e22022-08-04 22:04:10 +010074 });
pineafan813bdf42022-07-24 10:39:10 +010075 }
pineafan63fc5e22022-08-04 22:04:10 +010076 channel.edit({permissionOverwrites: overwrites});
pineafan813bdf42022-07-24 10:39:10 +010077 channel.setTopic(`${channel.topic.split(" ")[0]} Archived`);
pineafan63fc5e22022-08-04 22:04:10 +010078 const data = {
pineafan813bdf42022-07-24 10:39:10 +010079 meta:{
pineafan63fc5e22022-08-04 22:04:10 +010080 type: "ticketClosed",
81 displayName: "Ticket Closed",
pineafan813bdf42022-07-24 10:39:10 +010082 calculateType: "ticketUpdate",
83 color: NucleusColors.yellow,
pineafan63fc5e22022-08-04 22:04:10 +010084 emoji: "GUILD.TICKET.ARCHIVED",
pineafan813bdf42022-07-24 10:39:10 +010085 timestamp: new Date().getTime()
86 },
87 list: {
88 ticketFor: entry(channel.topic.split(" ")[0], renderUser((await interaction.guild.members.fetch(channel.topic.split(" ")[0])).user)),
89 closedBy: entry(interaction.member.user.id, renderUser(interaction.member.user)),
90 closed: entry(new Date().getTime(), renderDelta(new Date().getTime())),
pineafan63fc5e22022-08-04 22:04:10 +010091 ticketChannel: entry(channel.id, renderChannel(channel))
pineafan813bdf42022-07-24 10:39:10 +010092 },
93 hidden: {
94 guild: interaction.guild.id
95 }
pineafan63fc5e22022-08-04 22:04:10 +010096 };
pineafan813bdf42022-07-24 10:39:10 +010097 log(data);
98 await interaction.editReply({embeds: [new EmojiEmbed()
99 .setTitle("Close Ticket")
100 .setDescription("This ticket has been closed.\nType `/ticket close` again to delete it.\n\nNote: Check `/privacy` for details about transcripts.")
101 .setStatus("Warning")
102 .setEmoji("GUILD.TICKET.ARCHIVED")
103 ], components: [
104 new MessageActionRow().addComponents([
105 new MessageButton()
106 .setLabel("Delete")
107 .setStyle("DANGER")
108 .setCustomId("closeticket")
109 .setEmoji(getEmojiByName("CONTROL.CROSS", "id"))
pineafan63fc5e22022-08-04 22:04:10 +0100110 ].concat(client.database.premium.hasPremium(interaction.guild.id) ? [
111 new MessageButton()
pineafan813bdf42022-07-24 10:39:10 +0100112 .setLabel("Create Transcript and Delete")
113 .setStyle("PRIMARY")
114 .setCustomId("createtranscript")
115 .setEmoji(getEmojiByName("CONTROL.DOWNLOAD", "id"))
116 ] : []))
117 ]});
118 return;
119 }
120}
121
122async function purgeByUser(member, guild) {
pineafan63fc5e22022-08-04 22:04:10 +0100123 const config = await client.database.guilds.read(guild.id);
pineafan813bdf42022-07-24 10:39:10 +0100124 if (!config.tickets.category) return;
pineafan63fc5e22022-08-04 22:04:10 +0100125 const tickets = guild.channels.cache.get(config.tickets.category);
pineafan813bdf42022-07-24 10:39:10 +0100126 if (!tickets) return;
pineafan63fc5e22022-08-04 22:04:10 +0100127 const ticketChannels = tickets.children;
128 let deleted = 0;
pineafan813bdf42022-07-24 10:39:10 +0100129 ticketChannels.forEach(element => {
pineafane23c4ec2022-07-27 21:56:27 +0100130 if (element.type !== "GUILD_TEXT") return;
131 if (element.topic.split(" ")[0] === member) {
pineafan63fc5e22022-08-04 22:04:10 +0100132 try {
133 element.delete();
134 } catch { /* Errors if the channel does not exist (deleted already) */ }
135 deleted++;
pineafan813bdf42022-07-24 10:39:10 +0100136 }
137 });
138 if (deleted) {
pineafan63fc5e22022-08-04 22:04:10 +0100139 const { log, NucleusColors, entry, renderUser, renderDelta } = member.client.logger;
140 const data = {
pineafan02ba0232022-07-24 22:16:15 +0100141 meta:{
pineafan63fc5e22022-08-04 22:04:10 +0100142 type: "ticketPurge",
143 displayName: "Tickets Purged",
pineafan02ba0232022-07-24 22:16:15 +0100144 calculateType: "ticketUpdate",
145 color: NucleusColors.red,
pineafan63fc5e22022-08-04 22:04:10 +0100146 emoji: "GUILD.TICKET.DELETE",
pineafan02ba0232022-07-24 22:16:15 +0100147 timestamp: new Date().getTime()
148 },
149 list: {
150 ticketFor: entry(member, renderUser(member)),
151 deletedBy: entry(null, "Member left server"),
152 deleted: entry(new Date().getTime(), renderDelta(new Date().getTime())),
pineafan63fc5e22022-08-04 22:04:10 +0100153 ticketsDeleted: deleted
pineafan02ba0232022-07-24 22:16:15 +0100154 },
155 hidden: {
156 guild: guild.id
pineafan813bdf42022-07-24 10:39:10 +0100157 }
pineafan63fc5e22022-08-04 22:04:10 +0100158 };
pineafan02ba0232022-07-24 22:16:15 +0100159 log(data);
pineafan813bdf42022-07-24 10:39:10 +0100160 }
161}
162
pineafan63fc5e22022-08-04 22:04:10 +0100163export { purgeByUser };