blob: 33860b72a3ab1926fd0cddaa3f123dac1f055238 [file] [log] [blame]
TheCodedProf21c08592022-09-13 14:14:43 -04001import Discord, { ButtonBuilder, ActionRowBuilder, ButtonStyle } from "discord.js";
pineafan813bdf42022-07-24 10:39:10 +01002import client from "../../utils/client.js";
3import EmojiEmbed from "../../utils/generateEmojiEmbed.js";
4import getEmojiByName from "../../utils/getEmojiByName.js";
5
PineaFan100df682023-01-02 13:26:08 +00006export default async function (interaction: Discord.CommandInteraction) {
PineaFana00db1b2023-01-02 15:32:54 +00007 if (!interaction.guild) return;
Skyler Grey11236ba2022-08-08 21:13:33 +01008 const { log, NucleusColors, entry, renderUser, renderChannel, renderDelta } = client.logger;
pineafan813bdf42022-07-24 10:39:10 +01009
PineaFana00db1b2023-01-02 15:32:54 +000010 const config = await client.database.guilds.read(interaction.guild.id);
pineafanbd02b4a2022-08-05 22:01:38 +010011 let thread = false;
12 if (interaction.channel instanceof Discord.ThreadChannel) thread = true;
13 const threadChannel = interaction.channel as Discord.ThreadChannel;
Skyler Grey75ea9172022-08-06 10:22:23 +010014 const channel = interaction.channel as Discord.TextChannel;
15 if (
16 !channel.parent ||
17 config.tickets.category !== channel.parent.id ||
Skyler Grey11236ba2022-08-08 21:13:33 +010018 (thread ? threadChannel.parent.parent.id !== config.tickets.category : false)
Skyler Grey75ea9172022-08-06 10:22:23 +010019 ) {
20 return interaction.reply({
21 embeds: [
22 new EmojiEmbed()
23 .setTitle("Deleting Ticket...")
24 .setDescription(
25 "This ticket is not in your tickets category, so cannot be deleted. You cannot run close in a thread."
26 )
27 .setStatus("Danger")
28 .setEmoji("CONTROL.BLOCKCROSS")
29 ],
30 ephemeral: true
31 });
pineafan813bdf42022-07-24 10:39:10 +010032 }
pineafan63fc5e22022-08-04 22:04:10 +010033 const status = channel.topic.split(" ")[1];
pineafane23c4ec2022-07-27 21:56:27 +010034 if (status === "Archived") {
Skyler Grey75ea9172022-08-06 10:22:23 +010035 await interaction.reply({
36 embeds: [
37 new EmojiEmbed()
38 .setTitle("Delete Ticket")
39 .setDescription("Your ticket is being deleted...")
40 .setStatus("Danger")
41 .setEmoji("GUILD.TICKET.CLOSE")
42 ]
43 });
pineafan63fc5e22022-08-04 22:04:10 +010044 const data = {
Skyler Grey75ea9172022-08-06 10:22:23 +010045 meta: {
pineafan63fc5e22022-08-04 22:04:10 +010046 type: "ticketDeleted",
47 displayName: "Ticket Deleted",
pineafan813bdf42022-07-24 10:39:10 +010048 calculateType: "ticketUpdate",
49 color: NucleusColors.red,
pineafan63fc5e22022-08-04 22:04:10 +010050 emoji: "GUILD.TICKET.CLOSE",
pineafan813bdf42022-07-24 10:39:10 +010051 timestamp: new Date().getTime()
52 },
53 list: {
Skyler Grey75ea9172022-08-06 10:22:23 +010054 ticketFor: entry(
55 channel.topic.split(" ")[0],
Skyler Grey11236ba2022-08-08 21:13:33 +010056 renderUser((await interaction.guild.members.fetch(channel.topic.split(" ")[0])).user)
Skyler Grey75ea9172022-08-06 10:22:23 +010057 ),
Skyler Grey11236ba2022-08-08 21:13:33 +010058 deletedBy: entry(interaction.member.user.id, renderUser(interaction.member.user)),
59 deleted: entry(new Date().getTime(), renderDelta(new Date().getTime()))
pineafan813bdf42022-07-24 10:39:10 +010060 },
61 hidden: {
62 guild: interaction.guild.id
63 }
pineafan63fc5e22022-08-04 22:04:10 +010064 };
pineafan813bdf42022-07-24 10:39:10 +010065 log(data);
66 interaction.channel.delete();
67 return;
pineafane23c4ec2022-07-27 21:56:27 +010068 } else if (status === "Active") {
Skyler Grey75ea9172022-08-06 10:22:23 +010069 await interaction.reply({
70 embeds: [
71 new EmojiEmbed()
72 .setTitle("Close Ticket")
73 .setDescription("Your ticket is being closed...")
74 .setStatus("Warning")
75 .setEmoji("GUILD.TICKET.ARCHIVED")
76 ]
77 });
pineafan63fc5e22022-08-04 22:04:10 +010078 const overwrites = [
pineafan813bdf42022-07-24 10:39:10 +010079 {
80 id: channel.topic.split(" ")[0],
81 deny: ["VIEW_CHANNEL"],
82 type: "member"
83 },
84 {
85 id: interaction.guild.id,
86 deny: ["VIEW_CHANNEL"],
87 type: "role"
88 }
89 ] as Discord.OverwriteResolvable[];
pineafane23c4ec2022-07-27 21:56:27 +010090 if (config.tickets.supportRole !== null) {
pineafan813bdf42022-07-24 10:39:10 +010091 overwrites.push({
Skyler Grey11236ba2022-08-08 21:13:33 +010092 id: interaction.guild.roles.cache.get(config.tickets.supportRole),
93 allow: ["VIEW_CHANNEL", "SEND_MESSAGES", "ATTACH_FILES", "ADD_REACTIONS", "READ_MESSAGE_HISTORY"],
pineafan813bdf42022-07-24 10:39:10 +010094 type: "role"
pineafan63fc5e22022-08-04 22:04:10 +010095 });
pineafan813bdf42022-07-24 10:39:10 +010096 }
Skyler Grey75ea9172022-08-06 10:22:23 +010097 channel.edit({ permissionOverwrites: overwrites });
pineafan813bdf42022-07-24 10:39:10 +010098 channel.setTopic(`${channel.topic.split(" ")[0]} Archived`);
pineafan63fc5e22022-08-04 22:04:10 +010099 const data = {
Skyler Grey75ea9172022-08-06 10:22:23 +0100100 meta: {
pineafan63fc5e22022-08-04 22:04:10 +0100101 type: "ticketClosed",
102 displayName: "Ticket Closed",
pineafan813bdf42022-07-24 10:39:10 +0100103 calculateType: "ticketUpdate",
104 color: NucleusColors.yellow,
pineafan63fc5e22022-08-04 22:04:10 +0100105 emoji: "GUILD.TICKET.ARCHIVED",
pineafan813bdf42022-07-24 10:39:10 +0100106 timestamp: new Date().getTime()
107 },
108 list: {
Skyler Grey75ea9172022-08-06 10:22:23 +0100109 ticketFor: entry(
110 channel.topic.split(" ")[0],
Skyler Grey11236ba2022-08-08 21:13:33 +0100111 renderUser((await interaction.guild.members.fetch(channel.topic.split(" ")[0])).user)
Skyler Grey75ea9172022-08-06 10:22:23 +0100112 ),
Skyler Grey11236ba2022-08-08 21:13:33 +0100113 closedBy: entry(interaction.member.user.id, renderUser(interaction.member.user)),
114 closed: entry(new Date().getTime(), renderDelta(new Date().getTime())),
pineafan63fc5e22022-08-04 22:04:10 +0100115 ticketChannel: entry(channel.id, renderChannel(channel))
pineafan813bdf42022-07-24 10:39:10 +0100116 },
117 hidden: {
118 guild: interaction.guild.id
119 }
pineafan63fc5e22022-08-04 22:04:10 +0100120 };
pineafan813bdf42022-07-24 10:39:10 +0100121 log(data);
Skyler Grey75ea9172022-08-06 10:22:23 +0100122 await interaction.editReply({
123 embeds: [
124 new EmojiEmbed()
125 .setTitle("Close Ticket")
126 .setDescription(
127 "This ticket has been closed.\nType `/ticket close` again to delete it.\n\nNote: Check `/privacy` for details about transcripts."
128 )
129 .setStatus("Warning")
130 .setEmoji("GUILD.TICKET.ARCHIVED")
131 ],
132 components: [
TheCodedProf21c08592022-09-13 14:14:43 -0400133 new ActionRowBuilder().addComponents(
Skyler Grey75ea9172022-08-06 10:22:23 +0100134 [
TheCodedProf21c08592022-09-13 14:14:43 -0400135 new ButtonBuilder()
Skyler Grey75ea9172022-08-06 10:22:23 +0100136 .setLabel("Delete")
TheCodedProf21c08592022-09-13 14:14:43 -0400137 .setStyle(ButtonStyle.Danger)
Skyler Grey75ea9172022-08-06 10:22:23 +0100138 .setCustomId("closeticket")
139 .setEmoji(getEmojiByName("CONTROL.CROSS", "id"))
140 ].concat(
141 client.database.premium.hasPremium(interaction.guild.id)
142 ? [
TheCodedProf21c08592022-09-13 14:14:43 -0400143 new ButtonBuilder()
Skyler Grey75ea9172022-08-06 10:22:23 +0100144 .setLabel("Create Transcript and Delete")
TheCodedProf21c08592022-09-13 14:14:43 -0400145 .setStyle(ButtonStyle.Primary)
Skyler Grey75ea9172022-08-06 10:22:23 +0100146 .setCustomId("createtranscript")
Skyler Grey11236ba2022-08-08 21:13:33 +0100147 .setEmoji(getEmojiByName("CONTROL.DOWNLOAD", "id"))
Skyler Grey75ea9172022-08-06 10:22:23 +0100148 ]
149 : []
150 )
151 )
152 ]
153 });
pineafan813bdf42022-07-24 10:39:10 +0100154 return;
155 }
156}
157
158async function purgeByUser(member, guild) {
pineafan63fc5e22022-08-04 22:04:10 +0100159 const config = await client.database.guilds.read(guild.id);
pineafan813bdf42022-07-24 10:39:10 +0100160 if (!config.tickets.category) return;
pineafan63fc5e22022-08-04 22:04:10 +0100161 const tickets = guild.channels.cache.get(config.tickets.category);
pineafan813bdf42022-07-24 10:39:10 +0100162 if (!tickets) return;
pineafan63fc5e22022-08-04 22:04:10 +0100163 const ticketChannels = tickets.children;
164 let deleted = 0;
Skyler Grey75ea9172022-08-06 10:22:23 +0100165 ticketChannels.forEach((element) => {
pineafane23c4ec2022-07-27 21:56:27 +0100166 if (element.type !== "GUILD_TEXT") return;
167 if (element.topic.split(" ")[0] === member) {
pineafan63fc5e22022-08-04 22:04:10 +0100168 try {
169 element.delete();
Skyler Grey75ea9172022-08-06 10:22:23 +0100170 } catch {
171 /* Errors if the channel does not exist (deleted already) */
172 }
pineafan63fc5e22022-08-04 22:04:10 +0100173 deleted++;
pineafan813bdf42022-07-24 10:39:10 +0100174 }
175 });
176 if (deleted) {
Skyler Grey11236ba2022-08-08 21:13:33 +0100177 const { log, NucleusColors, entry, renderUser, renderDelta } = member.client.logger;
pineafan63fc5e22022-08-04 22:04:10 +0100178 const data = {
Skyler Grey75ea9172022-08-06 10:22:23 +0100179 meta: {
pineafan63fc5e22022-08-04 22:04:10 +0100180 type: "ticketPurge",
181 displayName: "Tickets Purged",
pineafan02ba0232022-07-24 22:16:15 +0100182 calculateType: "ticketUpdate",
183 color: NucleusColors.red,
pineafan63fc5e22022-08-04 22:04:10 +0100184 emoji: "GUILD.TICKET.DELETE",
pineafan02ba0232022-07-24 22:16:15 +0100185 timestamp: new Date().getTime()
186 },
187 list: {
188 ticketFor: entry(member, renderUser(member)),
189 deletedBy: entry(null, "Member left server"),
Skyler Grey11236ba2022-08-08 21:13:33 +0100190 deleted: entry(new Date().getTime(), renderDelta(new Date().getTime())),
pineafan63fc5e22022-08-04 22:04:10 +0100191 ticketsDeleted: deleted
pineafan02ba0232022-07-24 22:16:15 +0100192 },
193 hidden: {
194 guild: guild.id
pineafan813bdf42022-07-24 10:39:10 +0100195 }
pineafan63fc5e22022-08-04 22:04:10 +0100196 };
pineafan02ba0232022-07-24 22:16:15 +0100197 log(data);
pineafan813bdf42022-07-24 10:39:10 +0100198 }
199}
200
Skyler Grey75ea9172022-08-06 10:22:23 +0100201export { purgeByUser };