blob: d86396abfd03c84d62988b260c951f5da0ed4722 [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) {
7 const { log, NucleusColors, entry, renderUser, renderChannel, renderDelta } = client.logger
8
9 let 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 let 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 }
21 let 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 ]});
29 let data = {
30 meta:{
31 type: 'ticketDeleted',
32 displayName: 'Ticket Deleted',
33 calculateType: "ticketUpdate",
34 color: NucleusColors.red,
35 emoji: 'GUILD.TICKET.CLOSE',
36 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 }
46 }
47 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 ]});
57 let overwrites = [
58 {
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"
74 })
75 }
76 channel.edit({permissionOverwrites: overwrites})
77 channel.setTopic(`${channel.topic.split(" ")[0]} Archived`);
78 let data = {
79 meta:{
80 type: 'ticketClosed',
81 displayName: 'Ticket Closed',
82 calculateType: "ticketUpdate",
83 color: NucleusColors.yellow,
84 emoji: 'GUILD.TICKET.ARCHIVED',
85 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())),
91 ticketChannel: entry(channel.id, renderChannel(channel)),
92 },
93 hidden: {
94 guild: interaction.guild.id
95 }
96 }
97 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"))
110 ].concat(client.database.premium.hasPremium(interaction.guild.id) ? [
111 new MessageButton()
112 .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) {
123 let config = await client.database.guilds.read(guild.id);
124 if (!config.tickets.category) return;
125 let tickets = guild.channels.cache.get(config.tickets.category);
126 if (!tickets) return;
127 let ticketChannels = tickets.children;
128 let deleted = 0
129 ticketChannels.forEach(element => {
pineafane23c4ec2022-07-27 21:56:27 +0100130 if (element.type !== "GUILD_TEXT") return;
131 if (element.topic.split(" ")[0] === member) {
pineafan813bdf42022-07-24 10:39:10 +0100132 try { element.delete(); } catch {}
133 deleted++
134 }
135 });
136 if (deleted) {
pineafan02ba0232022-07-24 22:16:15 +0100137 const { log, NucleusColors, entry, renderUser, renderDelta } = member.client.logger
138 let data = {
139 meta:{
140 type: 'ticketPurge',
141 displayName: 'Tickets Purged',
142 calculateType: "ticketUpdate",
143 color: NucleusColors.red,
144 emoji: 'GUILD.TICKET.DELETE',
145 timestamp: new Date().getTime()
146 },
147 list: {
148 ticketFor: entry(member, renderUser(member)),
149 deletedBy: entry(null, "Member left server"),
150 deleted: entry(new Date().getTime(), renderDelta(new Date().getTime())),
151 ticketsDeleted: deleted,
152 },
153 hidden: {
154 guild: guild.id
pineafan813bdf42022-07-24 10:39:10 +0100155 }
pineafan02ba0232022-07-24 22:16:15 +0100156 }
157 log(data);
pineafan813bdf42022-07-24 10:39:10 +0100158 }
159}
160
161export { purgeByUser }