blob: e5c017f5551a1a1d7fd041a34d24b6d5edef7f6f [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);
pineafanbd02b4a2022-08-05 22:01:38 +010010 let thread = false;
11 if (interaction.channel instanceof Discord.ThreadChannel) thread = true;
12 const threadChannel = interaction.channel as Discord.ThreadChannel;
pineafan63fc5e22022-08-04 22:04:10 +010013 const channel = (interaction.channel as Discord.TextChannel);
pineafane23c4ec2022-07-27 21:56:27 +010014 if (!channel.parent || config.tickets.category !== channel.parent.id || (thread ? (threadChannel.parent.parent.id !== config.tickets.category) : false)) {
pineafan813bdf42022-07-24 10:39:10 +010015 return interaction.reply({embeds: [new EmojiEmbed()
16 .setTitle("Deleting Ticket...")
17 .setDescription("This ticket is not in your tickets category, so cannot be deleted. You cannot run close in a thread.")
18 .setStatus("Danger")
19 .setEmoji("CONTROL.BLOCKCROSS")
20 ], ephemeral: true});
21 }
pineafan63fc5e22022-08-04 22:04:10 +010022 const status = channel.topic.split(" ")[1];
pineafane23c4ec2022-07-27 21:56:27 +010023 if (status === "Archived") {
pineafan813bdf42022-07-24 10:39:10 +010024 await interaction.reply({embeds: [new EmojiEmbed()
25 .setTitle("Delete Ticket")
26 .setDescription("Your ticket is being deleted...")
27 .setStatus("Danger")
28 .setEmoji("GUILD.TICKET.CLOSE")
29 ]});
pineafan63fc5e22022-08-04 22:04:10 +010030 const data = {
pineafan813bdf42022-07-24 10:39:10 +010031 meta:{
pineafan63fc5e22022-08-04 22:04:10 +010032 type: "ticketDeleted",
33 displayName: "Ticket Deleted",
pineafan813bdf42022-07-24 10:39:10 +010034 calculateType: "ticketUpdate",
35 color: NucleusColors.red,
pineafan63fc5e22022-08-04 22:04:10 +010036 emoji: "GUILD.TICKET.CLOSE",
pineafan813bdf42022-07-24 10:39:10 +010037 timestamp: new Date().getTime()
38 },
39 list: {
40 ticketFor: entry(channel.topic.split(" ")[0], renderUser((await interaction.guild.members.fetch(channel.topic.split(" ")[0])).user)),
41 deletedBy: entry(interaction.member.user.id, renderUser(interaction.member.user)),
42 deleted: entry(new Date().getTime(), renderDelta(new Date().getTime()))
43 },
44 hidden: {
45 guild: interaction.guild.id
46 }
pineafan63fc5e22022-08-04 22:04:10 +010047 };
pineafan813bdf42022-07-24 10:39:10 +010048 log(data);
49 interaction.channel.delete();
50 return;
pineafane23c4ec2022-07-27 21:56:27 +010051 } else if (status === "Active") {
pineafan813bdf42022-07-24 10:39:10 +010052 await interaction.reply({embeds: [new EmojiEmbed()
53 .setTitle("Close Ticket")
54 .setDescription("Your ticket is being closed...")
55 .setStatus("Warning")
56 .setEmoji("GUILD.TICKET.ARCHIVED")
57 ]});
pineafan63fc5e22022-08-04 22:04:10 +010058 const overwrites = [
pineafan813bdf42022-07-24 10:39:10 +010059 {
60 id: channel.topic.split(" ")[0],
61 deny: ["VIEW_CHANNEL"],
62 type: "member"
63 },
64 {
65 id: interaction.guild.id,
66 deny: ["VIEW_CHANNEL"],
67 type: "role"
68 }
69 ] as Discord.OverwriteResolvable[];
pineafane23c4ec2022-07-27 21:56:27 +010070 if (config.tickets.supportRole !== null) {
pineafan813bdf42022-07-24 10:39:10 +010071 overwrites.push({
72 id: interaction.guild.roles.cache.get(config.tickets.supportRole),
73 allow: ["VIEW_CHANNEL", "SEND_MESSAGES", "ATTACH_FILES", "ADD_REACTIONS", "READ_MESSAGE_HISTORY"],
74 type: "role"
pineafan63fc5e22022-08-04 22:04:10 +010075 });
pineafan813bdf42022-07-24 10:39:10 +010076 }
pineafan63fc5e22022-08-04 22:04:10 +010077 channel.edit({permissionOverwrites: overwrites});
pineafan813bdf42022-07-24 10:39:10 +010078 channel.setTopic(`${channel.topic.split(" ")[0]} Archived`);
pineafan63fc5e22022-08-04 22:04:10 +010079 const data = {
pineafan813bdf42022-07-24 10:39:10 +010080 meta:{
pineafan63fc5e22022-08-04 22:04:10 +010081 type: "ticketClosed",
82 displayName: "Ticket Closed",
pineafan813bdf42022-07-24 10:39:10 +010083 calculateType: "ticketUpdate",
84 color: NucleusColors.yellow,
pineafan63fc5e22022-08-04 22:04:10 +010085 emoji: "GUILD.TICKET.ARCHIVED",
pineafan813bdf42022-07-24 10:39:10 +010086 timestamp: new Date().getTime()
87 },
88 list: {
89 ticketFor: entry(channel.topic.split(" ")[0], renderUser((await interaction.guild.members.fetch(channel.topic.split(" ")[0])).user)),
90 closedBy: entry(interaction.member.user.id, renderUser(interaction.member.user)),
91 closed: entry(new Date().getTime(), renderDelta(new Date().getTime())),
pineafan63fc5e22022-08-04 22:04:10 +010092 ticketChannel: entry(channel.id, renderChannel(channel))
pineafan813bdf42022-07-24 10:39:10 +010093 },
94 hidden: {
95 guild: interaction.guild.id
96 }
pineafan63fc5e22022-08-04 22:04:10 +010097 };
pineafan813bdf42022-07-24 10:39:10 +010098 log(data);
99 await interaction.editReply({embeds: [new EmojiEmbed()
100 .setTitle("Close Ticket")
101 .setDescription("This ticket has been closed.\nType `/ticket close` again to delete it.\n\nNote: Check `/privacy` for details about transcripts.")
102 .setStatus("Warning")
103 .setEmoji("GUILD.TICKET.ARCHIVED")
104 ], components: [
105 new MessageActionRow().addComponents([
106 new MessageButton()
107 .setLabel("Delete")
108 .setStyle("DANGER")
109 .setCustomId("closeticket")
110 .setEmoji(getEmojiByName("CONTROL.CROSS", "id"))
pineafan63fc5e22022-08-04 22:04:10 +0100111 ].concat(client.database.premium.hasPremium(interaction.guild.id) ? [
112 new MessageButton()
pineafan813bdf42022-07-24 10:39:10 +0100113 .setLabel("Create Transcript and Delete")
114 .setStyle("PRIMARY")
115 .setCustomId("createtranscript")
116 .setEmoji(getEmojiByName("CONTROL.DOWNLOAD", "id"))
117 ] : []))
118 ]});
119 return;
120 }
121}
122
123async function purgeByUser(member, guild) {
pineafan63fc5e22022-08-04 22:04:10 +0100124 const config = await client.database.guilds.read(guild.id);
pineafan813bdf42022-07-24 10:39:10 +0100125 if (!config.tickets.category) return;
pineafan63fc5e22022-08-04 22:04:10 +0100126 const tickets = guild.channels.cache.get(config.tickets.category);
pineafan813bdf42022-07-24 10:39:10 +0100127 if (!tickets) return;
pineafan63fc5e22022-08-04 22:04:10 +0100128 const ticketChannels = tickets.children;
129 let deleted = 0;
pineafan813bdf42022-07-24 10:39:10 +0100130 ticketChannels.forEach(element => {
pineafane23c4ec2022-07-27 21:56:27 +0100131 if (element.type !== "GUILD_TEXT") return;
132 if (element.topic.split(" ")[0] === member) {
pineafan63fc5e22022-08-04 22:04:10 +0100133 try {
134 element.delete();
135 } catch { /* Errors if the channel does not exist (deleted already) */ }
136 deleted++;
pineafan813bdf42022-07-24 10:39:10 +0100137 }
138 });
139 if (deleted) {
pineafan63fc5e22022-08-04 22:04:10 +0100140 const { log, NucleusColors, entry, renderUser, renderDelta } = member.client.logger;
141 const data = {
pineafan02ba0232022-07-24 22:16:15 +0100142 meta:{
pineafan63fc5e22022-08-04 22:04:10 +0100143 type: "ticketPurge",
144 displayName: "Tickets Purged",
pineafan02ba0232022-07-24 22:16:15 +0100145 calculateType: "ticketUpdate",
146 color: NucleusColors.red,
pineafan63fc5e22022-08-04 22:04:10 +0100147 emoji: "GUILD.TICKET.DELETE",
pineafan02ba0232022-07-24 22:16:15 +0100148 timestamp: new Date().getTime()
149 },
150 list: {
151 ticketFor: entry(member, renderUser(member)),
152 deletedBy: entry(null, "Member left server"),
153 deleted: entry(new Date().getTime(), renderDelta(new Date().getTime())),
pineafan63fc5e22022-08-04 22:04:10 +0100154 ticketsDeleted: deleted
pineafan02ba0232022-07-24 22:16:15 +0100155 },
156 hidden: {
157 guild: guild.id
pineafan813bdf42022-07-24 10:39:10 +0100158 }
pineafan63fc5e22022-08-04 22:04:10 +0100159 };
pineafan02ba0232022-07-24 22:16:15 +0100160 log(data);
pineafan813bdf42022-07-24 10:39:10 +0100161 }
162}
163
pineafan63fc5e22022-08-04 22:04:10 +0100164export { purgeByUser };