blob: 0db4232db895ffb50662b2f4c257586232df69d7 [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) {
Skyler Grey11236ba2022-08-08 21:13:33 +01007 const { log, NucleusColors, entry, renderUser, renderChannel, renderDelta } = client.logger;
pineafan813bdf42022-07-24 10:39:10 +01008
PineaFan100df682023-01-02 13:26:08 +00009 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;
Skyler Grey75ea9172022-08-06 10:22:23 +010013 const channel = interaction.channel as Discord.TextChannel;
14 if (
15 !channel.parent ||
16 config.tickets.category !== channel.parent.id ||
Skyler Grey11236ba2022-08-08 21:13:33 +010017 (thread ? threadChannel.parent.parent.id !== config.tickets.category : false)
Skyler Grey75ea9172022-08-06 10:22:23 +010018 ) {
19 return interaction.reply({
20 embeds: [
21 new EmojiEmbed()
22 .setTitle("Deleting Ticket...")
23 .setDescription(
24 "This ticket is not in your tickets category, so cannot be deleted. You cannot run close in a thread."
25 )
26 .setStatus("Danger")
27 .setEmoji("CONTROL.BLOCKCROSS")
28 ],
29 ephemeral: true
30 });
pineafan813bdf42022-07-24 10:39:10 +010031 }
pineafan63fc5e22022-08-04 22:04:10 +010032 const status = channel.topic.split(" ")[1];
pineafane23c4ec2022-07-27 21:56:27 +010033 if (status === "Archived") {
Skyler Grey75ea9172022-08-06 10:22:23 +010034 await interaction.reply({
35 embeds: [
36 new EmojiEmbed()
37 .setTitle("Delete Ticket")
38 .setDescription("Your ticket is being deleted...")
39 .setStatus("Danger")
40 .setEmoji("GUILD.TICKET.CLOSE")
41 ]
42 });
pineafan63fc5e22022-08-04 22:04:10 +010043 const data = {
Skyler Grey75ea9172022-08-06 10:22:23 +010044 meta: {
pineafan63fc5e22022-08-04 22:04:10 +010045 type: "ticketDeleted",
46 displayName: "Ticket Deleted",
pineafan813bdf42022-07-24 10:39:10 +010047 calculateType: "ticketUpdate",
48 color: NucleusColors.red,
pineafan63fc5e22022-08-04 22:04:10 +010049 emoji: "GUILD.TICKET.CLOSE",
pineafan813bdf42022-07-24 10:39:10 +010050 timestamp: new Date().getTime()
51 },
52 list: {
Skyler Grey75ea9172022-08-06 10:22:23 +010053 ticketFor: entry(
54 channel.topic.split(" ")[0],
Skyler Grey11236ba2022-08-08 21:13:33 +010055 renderUser((await interaction.guild.members.fetch(channel.topic.split(" ")[0])).user)
Skyler Grey75ea9172022-08-06 10:22:23 +010056 ),
Skyler Grey11236ba2022-08-08 21:13:33 +010057 deletedBy: entry(interaction.member.user.id, renderUser(interaction.member.user)),
58 deleted: entry(new Date().getTime(), renderDelta(new Date().getTime()))
pineafan813bdf42022-07-24 10:39:10 +010059 },
60 hidden: {
61 guild: interaction.guild.id
62 }
pineafan63fc5e22022-08-04 22:04:10 +010063 };
pineafan813bdf42022-07-24 10:39:10 +010064 log(data);
65 interaction.channel.delete();
66 return;
pineafane23c4ec2022-07-27 21:56:27 +010067 } else if (status === "Active") {
Skyler Grey75ea9172022-08-06 10:22:23 +010068 await interaction.reply({
69 embeds: [
70 new EmojiEmbed()
71 .setTitle("Close Ticket")
72 .setDescription("Your ticket is being closed...")
73 .setStatus("Warning")
74 .setEmoji("GUILD.TICKET.ARCHIVED")
75 ]
76 });
pineafan63fc5e22022-08-04 22:04:10 +010077 const overwrites = [
pineafan813bdf42022-07-24 10:39:10 +010078 {
79 id: channel.topic.split(" ")[0],
80 deny: ["VIEW_CHANNEL"],
81 type: "member"
82 },
83 {
84 id: interaction.guild.id,
85 deny: ["VIEW_CHANNEL"],
86 type: "role"
87 }
88 ] as Discord.OverwriteResolvable[];
pineafane23c4ec2022-07-27 21:56:27 +010089 if (config.tickets.supportRole !== null) {
pineafan813bdf42022-07-24 10:39:10 +010090 overwrites.push({
Skyler Grey11236ba2022-08-08 21:13:33 +010091 id: interaction.guild.roles.cache.get(config.tickets.supportRole),
92 allow: ["VIEW_CHANNEL", "SEND_MESSAGES", "ATTACH_FILES", "ADD_REACTIONS", "READ_MESSAGE_HISTORY"],
pineafan813bdf42022-07-24 10:39:10 +010093 type: "role"
pineafan63fc5e22022-08-04 22:04:10 +010094 });
pineafan813bdf42022-07-24 10:39:10 +010095 }
Skyler Grey75ea9172022-08-06 10:22:23 +010096 channel.edit({ permissionOverwrites: overwrites });
pineafan813bdf42022-07-24 10:39:10 +010097 channel.setTopic(`${channel.topic.split(" ")[0]} Archived`);
pineafan63fc5e22022-08-04 22:04:10 +010098 const data = {
Skyler Grey75ea9172022-08-06 10:22:23 +010099 meta: {
pineafan63fc5e22022-08-04 22:04:10 +0100100 type: "ticketClosed",
101 displayName: "Ticket Closed",
pineafan813bdf42022-07-24 10:39:10 +0100102 calculateType: "ticketUpdate",
103 color: NucleusColors.yellow,
pineafan63fc5e22022-08-04 22:04:10 +0100104 emoji: "GUILD.TICKET.ARCHIVED",
pineafan813bdf42022-07-24 10:39:10 +0100105 timestamp: new Date().getTime()
106 },
107 list: {
Skyler Grey75ea9172022-08-06 10:22:23 +0100108 ticketFor: entry(
109 channel.topic.split(" ")[0],
Skyler Grey11236ba2022-08-08 21:13:33 +0100110 renderUser((await interaction.guild.members.fetch(channel.topic.split(" ")[0])).user)
Skyler Grey75ea9172022-08-06 10:22:23 +0100111 ),
Skyler Grey11236ba2022-08-08 21:13:33 +0100112 closedBy: entry(interaction.member.user.id, renderUser(interaction.member.user)),
113 closed: entry(new Date().getTime(), renderDelta(new Date().getTime())),
pineafan63fc5e22022-08-04 22:04:10 +0100114 ticketChannel: entry(channel.id, renderChannel(channel))
pineafan813bdf42022-07-24 10:39:10 +0100115 },
116 hidden: {
117 guild: interaction.guild.id
118 }
pineafan63fc5e22022-08-04 22:04:10 +0100119 };
pineafan813bdf42022-07-24 10:39:10 +0100120 log(data);
Skyler Grey75ea9172022-08-06 10:22:23 +0100121 await interaction.editReply({
122 embeds: [
123 new EmojiEmbed()
124 .setTitle("Close Ticket")
125 .setDescription(
126 "This ticket has been closed.\nType `/ticket close` again to delete it.\n\nNote: Check `/privacy` for details about transcripts."
127 )
128 .setStatus("Warning")
129 .setEmoji("GUILD.TICKET.ARCHIVED")
130 ],
131 components: [
TheCodedProf21c08592022-09-13 14:14:43 -0400132 new ActionRowBuilder().addComponents(
Skyler Grey75ea9172022-08-06 10:22:23 +0100133 [
TheCodedProf21c08592022-09-13 14:14:43 -0400134 new ButtonBuilder()
Skyler Grey75ea9172022-08-06 10:22:23 +0100135 .setLabel("Delete")
TheCodedProf21c08592022-09-13 14:14:43 -0400136 .setStyle(ButtonStyle.Danger)
Skyler Grey75ea9172022-08-06 10:22:23 +0100137 .setCustomId("closeticket")
138 .setEmoji(getEmojiByName("CONTROL.CROSS", "id"))
139 ].concat(
140 client.database.premium.hasPremium(interaction.guild.id)
141 ? [
TheCodedProf21c08592022-09-13 14:14:43 -0400142 new ButtonBuilder()
Skyler Grey75ea9172022-08-06 10:22:23 +0100143 .setLabel("Create Transcript and Delete")
TheCodedProf21c08592022-09-13 14:14:43 -0400144 .setStyle(ButtonStyle.Primary)
Skyler Grey75ea9172022-08-06 10:22:23 +0100145 .setCustomId("createtranscript")
Skyler Grey11236ba2022-08-08 21:13:33 +0100146 .setEmoji(getEmojiByName("CONTROL.DOWNLOAD", "id"))
Skyler Grey75ea9172022-08-06 10:22:23 +0100147 ]
148 : []
149 )
150 )
151 ]
152 });
pineafan813bdf42022-07-24 10:39:10 +0100153 return;
154 }
155}
156
157async function purgeByUser(member, guild) {
pineafan63fc5e22022-08-04 22:04:10 +0100158 const config = await client.database.guilds.read(guild.id);
pineafan813bdf42022-07-24 10:39:10 +0100159 if (!config.tickets.category) return;
pineafan63fc5e22022-08-04 22:04:10 +0100160 const tickets = guild.channels.cache.get(config.tickets.category);
pineafan813bdf42022-07-24 10:39:10 +0100161 if (!tickets) return;
pineafan63fc5e22022-08-04 22:04:10 +0100162 const ticketChannels = tickets.children;
163 let deleted = 0;
Skyler Grey75ea9172022-08-06 10:22:23 +0100164 ticketChannels.forEach((element) => {
pineafane23c4ec2022-07-27 21:56:27 +0100165 if (element.type !== "GUILD_TEXT") return;
166 if (element.topic.split(" ")[0] === member) {
pineafan63fc5e22022-08-04 22:04:10 +0100167 try {
168 element.delete();
Skyler Grey75ea9172022-08-06 10:22:23 +0100169 } catch {
170 /* Errors if the channel does not exist (deleted already) */
171 }
pineafan63fc5e22022-08-04 22:04:10 +0100172 deleted++;
pineafan813bdf42022-07-24 10:39:10 +0100173 }
174 });
175 if (deleted) {
Skyler Grey11236ba2022-08-08 21:13:33 +0100176 const { log, NucleusColors, entry, renderUser, renderDelta } = member.client.logger;
pineafan63fc5e22022-08-04 22:04:10 +0100177 const data = {
Skyler Grey75ea9172022-08-06 10:22:23 +0100178 meta: {
pineafan63fc5e22022-08-04 22:04:10 +0100179 type: "ticketPurge",
180 displayName: "Tickets Purged",
pineafan02ba0232022-07-24 22:16:15 +0100181 calculateType: "ticketUpdate",
182 color: NucleusColors.red,
pineafan63fc5e22022-08-04 22:04:10 +0100183 emoji: "GUILD.TICKET.DELETE",
pineafan02ba0232022-07-24 22:16:15 +0100184 timestamp: new Date().getTime()
185 },
186 list: {
187 ticketFor: entry(member, renderUser(member)),
188 deletedBy: entry(null, "Member left server"),
Skyler Grey11236ba2022-08-08 21:13:33 +0100189 deleted: entry(new Date().getTime(), renderDelta(new Date().getTime())),
pineafan63fc5e22022-08-04 22:04:10 +0100190 ticketsDeleted: deleted
pineafan02ba0232022-07-24 22:16:15 +0100191 },
192 hidden: {
193 guild: guild.id
pineafan813bdf42022-07-24 10:39:10 +0100194 }
pineafan63fc5e22022-08-04 22:04:10 +0100195 };
pineafan02ba0232022-07-24 22:16:15 +0100196 log(data);
pineafan813bdf42022-07-24 10:39:10 +0100197 }
198}
199
Skyler Grey75ea9172022-08-06 10:22:23 +0100200export { purgeByUser };