blob: 6509c039dee6b164ea94a188d469cbc76e686774 [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) {
Skyler Grey75ea9172022-08-06 10:22:23 +01007 const {
8 log,
9 NucleusColors,
10 entry,
11 renderUser,
12 renderChannel,
13 renderDelta
14 } = client.logger;
pineafan813bdf42022-07-24 10:39:10 +010015
pineafan63fc5e22022-08-04 22:04:10 +010016 const config = await client.database.guilds.read(interaction.guild.id);
pineafanbd02b4a2022-08-05 22:01:38 +010017 let thread = false;
18 if (interaction.channel instanceof Discord.ThreadChannel) thread = true;
19 const threadChannel = interaction.channel as Discord.ThreadChannel;
Skyler Grey75ea9172022-08-06 10:22:23 +010020 const channel = interaction.channel as Discord.TextChannel;
21 if (
22 !channel.parent ||
23 config.tickets.category !== channel.parent.id ||
24 (thread
25 ? threadChannel.parent.parent.id !== config.tickets.category
26 : false)
27 ) {
28 return interaction.reply({
29 embeds: [
30 new EmojiEmbed()
31 .setTitle("Deleting Ticket...")
32 .setDescription(
33 "This ticket is not in your tickets category, so cannot be deleted. You cannot run close in a thread."
34 )
35 .setStatus("Danger")
36 .setEmoji("CONTROL.BLOCKCROSS")
37 ],
38 ephemeral: true
39 });
pineafan813bdf42022-07-24 10:39:10 +010040 }
pineafan63fc5e22022-08-04 22:04:10 +010041 const status = channel.topic.split(" ")[1];
pineafane23c4ec2022-07-27 21:56:27 +010042 if (status === "Archived") {
Skyler Grey75ea9172022-08-06 10:22:23 +010043 await interaction.reply({
44 embeds: [
45 new EmojiEmbed()
46 .setTitle("Delete Ticket")
47 .setDescription("Your ticket is being deleted...")
48 .setStatus("Danger")
49 .setEmoji("GUILD.TICKET.CLOSE")
50 ]
51 });
pineafan63fc5e22022-08-04 22:04:10 +010052 const data = {
Skyler Grey75ea9172022-08-06 10:22:23 +010053 meta: {
pineafan63fc5e22022-08-04 22:04:10 +010054 type: "ticketDeleted",
55 displayName: "Ticket Deleted",
pineafan813bdf42022-07-24 10:39:10 +010056 calculateType: "ticketUpdate",
57 color: NucleusColors.red,
pineafan63fc5e22022-08-04 22:04:10 +010058 emoji: "GUILD.TICKET.CLOSE",
pineafan813bdf42022-07-24 10:39:10 +010059 timestamp: new Date().getTime()
60 },
61 list: {
Skyler Grey75ea9172022-08-06 10:22:23 +010062 ticketFor: entry(
63 channel.topic.split(" ")[0],
64 renderUser(
65 (
66 await interaction.guild.members.fetch(
67 channel.topic.split(" ")[0]
68 )
69 ).user
70 )
71 ),
72 deletedBy: entry(
73 interaction.member.user.id,
74 renderUser(interaction.member.user)
75 ),
76 deleted: entry(
77 new Date().getTime(),
78 renderDelta(new Date().getTime())
79 )
pineafan813bdf42022-07-24 10:39:10 +010080 },
81 hidden: {
82 guild: interaction.guild.id
83 }
pineafan63fc5e22022-08-04 22:04:10 +010084 };
pineafan813bdf42022-07-24 10:39:10 +010085 log(data);
86 interaction.channel.delete();
87 return;
pineafane23c4ec2022-07-27 21:56:27 +010088 } else if (status === "Active") {
Skyler Grey75ea9172022-08-06 10:22:23 +010089 await interaction.reply({
90 embeds: [
91 new EmojiEmbed()
92 .setTitle("Close Ticket")
93 .setDescription("Your ticket is being closed...")
94 .setStatus("Warning")
95 .setEmoji("GUILD.TICKET.ARCHIVED")
96 ]
97 });
pineafan63fc5e22022-08-04 22:04:10 +010098 const overwrites = [
pineafan813bdf42022-07-24 10:39:10 +010099 {
100 id: channel.topic.split(" ")[0],
101 deny: ["VIEW_CHANNEL"],
102 type: "member"
103 },
104 {
105 id: interaction.guild.id,
106 deny: ["VIEW_CHANNEL"],
107 type: "role"
108 }
109 ] as Discord.OverwriteResolvable[];
pineafane23c4ec2022-07-27 21:56:27 +0100110 if (config.tickets.supportRole !== null) {
pineafan813bdf42022-07-24 10:39:10 +0100111 overwrites.push({
Skyler Grey75ea9172022-08-06 10:22:23 +0100112 id: interaction.guild.roles.cache.get(
113 config.tickets.supportRole
114 ),
115 allow: [
116 "VIEW_CHANNEL",
117 "SEND_MESSAGES",
118 "ATTACH_FILES",
119 "ADD_REACTIONS",
120 "READ_MESSAGE_HISTORY"
121 ],
pineafan813bdf42022-07-24 10:39:10 +0100122 type: "role"
pineafan63fc5e22022-08-04 22:04:10 +0100123 });
pineafan813bdf42022-07-24 10:39:10 +0100124 }
Skyler Grey75ea9172022-08-06 10:22:23 +0100125 channel.edit({ permissionOverwrites: overwrites });
pineafan813bdf42022-07-24 10:39:10 +0100126 channel.setTopic(`${channel.topic.split(" ")[0]} Archived`);
pineafan63fc5e22022-08-04 22:04:10 +0100127 const data = {
Skyler Grey75ea9172022-08-06 10:22:23 +0100128 meta: {
pineafan63fc5e22022-08-04 22:04:10 +0100129 type: "ticketClosed",
130 displayName: "Ticket Closed",
pineafan813bdf42022-07-24 10:39:10 +0100131 calculateType: "ticketUpdate",
132 color: NucleusColors.yellow,
pineafan63fc5e22022-08-04 22:04:10 +0100133 emoji: "GUILD.TICKET.ARCHIVED",
pineafan813bdf42022-07-24 10:39:10 +0100134 timestamp: new Date().getTime()
135 },
136 list: {
Skyler Grey75ea9172022-08-06 10:22:23 +0100137 ticketFor: entry(
138 channel.topic.split(" ")[0],
139 renderUser(
140 (
141 await interaction.guild.members.fetch(
142 channel.topic.split(" ")[0]
143 )
144 ).user
145 )
146 ),
147 closedBy: entry(
148 interaction.member.user.id,
149 renderUser(interaction.member.user)
150 ),
151 closed: entry(
152 new Date().getTime(),
153 renderDelta(new Date().getTime())
154 ),
pineafan63fc5e22022-08-04 22:04:10 +0100155 ticketChannel: entry(channel.id, renderChannel(channel))
pineafan813bdf42022-07-24 10:39:10 +0100156 },
157 hidden: {
158 guild: interaction.guild.id
159 }
pineafan63fc5e22022-08-04 22:04:10 +0100160 };
pineafan813bdf42022-07-24 10:39:10 +0100161 log(data);
Skyler Grey75ea9172022-08-06 10:22:23 +0100162 await interaction.editReply({
163 embeds: [
164 new EmojiEmbed()
165 .setTitle("Close Ticket")
166 .setDescription(
167 "This ticket has been closed.\nType `/ticket close` again to delete it.\n\nNote: Check `/privacy` for details about transcripts."
168 )
169 .setStatus("Warning")
170 .setEmoji("GUILD.TICKET.ARCHIVED")
171 ],
172 components: [
173 new MessageActionRow().addComponents(
174 [
175 new MessageButton()
176 .setLabel("Delete")
177 .setStyle("DANGER")
178 .setCustomId("closeticket")
179 .setEmoji(getEmojiByName("CONTROL.CROSS", "id"))
180 ].concat(
181 client.database.premium.hasPremium(interaction.guild.id)
182 ? [
183 new MessageButton()
184 .setLabel("Create Transcript and Delete")
185 .setStyle("PRIMARY")
186 .setCustomId("createtranscript")
187 .setEmoji(
188 getEmojiByName(
189 "CONTROL.DOWNLOAD",
190 "id"
191 )
192 )
193 ]
194 : []
195 )
196 )
197 ]
198 });
pineafan813bdf42022-07-24 10:39:10 +0100199 return;
200 }
201}
202
203async function purgeByUser(member, guild) {
pineafan63fc5e22022-08-04 22:04:10 +0100204 const config = await client.database.guilds.read(guild.id);
pineafan813bdf42022-07-24 10:39:10 +0100205 if (!config.tickets.category) return;
pineafan63fc5e22022-08-04 22:04:10 +0100206 const tickets = guild.channels.cache.get(config.tickets.category);
pineafan813bdf42022-07-24 10:39:10 +0100207 if (!tickets) return;
pineafan63fc5e22022-08-04 22:04:10 +0100208 const ticketChannels = tickets.children;
209 let deleted = 0;
Skyler Grey75ea9172022-08-06 10:22:23 +0100210 ticketChannels.forEach((element) => {
pineafane23c4ec2022-07-27 21:56:27 +0100211 if (element.type !== "GUILD_TEXT") return;
212 if (element.topic.split(" ")[0] === member) {
pineafan63fc5e22022-08-04 22:04:10 +0100213 try {
214 element.delete();
Skyler Grey75ea9172022-08-06 10:22:23 +0100215 } catch {
216 /* Errors if the channel does not exist (deleted already) */
217 }
pineafan63fc5e22022-08-04 22:04:10 +0100218 deleted++;
pineafan813bdf42022-07-24 10:39:10 +0100219 }
220 });
221 if (deleted) {
Skyler Grey75ea9172022-08-06 10:22:23 +0100222 const { log, NucleusColors, entry, renderUser, renderDelta } =
223 member.client.logger;
pineafan63fc5e22022-08-04 22:04:10 +0100224 const data = {
Skyler Grey75ea9172022-08-06 10:22:23 +0100225 meta: {
pineafan63fc5e22022-08-04 22:04:10 +0100226 type: "ticketPurge",
227 displayName: "Tickets Purged",
pineafan02ba0232022-07-24 22:16:15 +0100228 calculateType: "ticketUpdate",
229 color: NucleusColors.red,
pineafan63fc5e22022-08-04 22:04:10 +0100230 emoji: "GUILD.TICKET.DELETE",
pineafan02ba0232022-07-24 22:16:15 +0100231 timestamp: new Date().getTime()
232 },
233 list: {
234 ticketFor: entry(member, renderUser(member)),
235 deletedBy: entry(null, "Member left server"),
Skyler Grey75ea9172022-08-06 10:22:23 +0100236 deleted: entry(
237 new Date().getTime(),
238 renderDelta(new Date().getTime())
239 ),
pineafan63fc5e22022-08-04 22:04:10 +0100240 ticketsDeleted: deleted
pineafan02ba0232022-07-24 22:16:15 +0100241 },
242 hidden: {
243 guild: guild.id
pineafan813bdf42022-07-24 10:39:10 +0100244 }
pineafan63fc5e22022-08-04 22:04:10 +0100245 };
pineafan02ba0232022-07-24 22:16:15 +0100246 log(data);
pineafan813bdf42022-07-24 10:39:10 +0100247 }
248}
249
Skyler Grey75ea9172022-08-06 10:22:23 +0100250export { purgeByUser };