blob: 48d2a51ec1834f7e85243f816592c90d58b466c6 [file] [log] [blame]
PineaFan0d06edc2023-01-17 22:10:31 +00001import { getCommandMentionByName } from '../../utils/getCommandMentionByName.js';
2import Discord, { ActionRowBuilder, ButtonBuilder, ButtonInteraction, PrivateThreadChannel, TextChannel, ButtonStyle } from "discord.js";
pineafan813bdf42022-07-24 10:39:10 +01003import client from "../../utils/client.js";
4import EmojiEmbed from "../../utils/generateEmojiEmbed.js";
5import getEmojiByName from "../../utils/getEmojiByName.js";
PineaFan0d06edc2023-01-17 22:10:31 +00006import { preloadPage } from '../../utils/createTemporaryStorage.js';
pineafan813bdf42022-07-24 10:39:10 +01007
PineaFan538d3752023-01-12 21:48:23 +00008export default async function (interaction: Discord.CommandInteraction | ButtonInteraction) {
PineaFana00db1b2023-01-02 15:32:54 +00009 if (!interaction.guild) return;
PineaFan0d06edc2023-01-17 22:10:31 +000010 const config = await client.database.guilds.read(interaction.guild.id);
Skyler Grey11236ba2022-08-08 21:13:33 +010011 const { log, NucleusColors, entry, renderUser, renderChannel, renderDelta } = client.logger;
pineafan813bdf42022-07-24 10:39:10 +010012
PineaFan0d06edc2023-01-17 22:10:31 +000013 const ticketChannel = config.tickets.category;
14 if (!("parent" in interaction.channel!)) {
15 return await interaction.reply({
Skyler Grey75ea9172022-08-06 10:22:23 +010016 embeds: [
17 new EmojiEmbed()
PineaFan0d06edc2023-01-17 22:10:31 +000018 .setTitle("Not a ticket")
19 .setDescription("This channel isn't a ticket, so you can't delete it.")
Skyler Grey75ea9172022-08-06 10:22:23 +010020 .setStatus("Danger")
21 .setEmoji("CONTROL.BLOCKCROSS")
PineaFan0d06edc2023-01-17 22:10:31 +000022 ], ephemeral: true
Skyler Grey75ea9172022-08-06 10:22:23 +010023 });
PineaFan0d06edc2023-01-17 22:10:31 +000024 } else if (interaction.channel!.parent!.id !== ticketChannel) {
25 return await interaction.reply({
Skyler Grey75ea9172022-08-06 10:22:23 +010026 embeds: [
27 new EmojiEmbed()
PineaFan0d06edc2023-01-17 22:10:31 +000028 .setTitle("Not a ticket")
29 .setDescription("This channel isn't a ticket, so you can't delete it.")
Skyler Grey75ea9172022-08-06 10:22:23 +010030 .setStatus("Danger")
PineaFan0d06edc2023-01-17 22:10:31 +000031 .setEmoji("CONTROL.BLOCKCROSS")
32 ], ephemeral: true
Skyler Grey75ea9172022-08-06 10:22:23 +010033 });
PineaFan0d06edc2023-01-17 22:10:31 +000034 }
35 const channel: PrivateThreadChannel | TextChannel = interaction.channel as PrivateThreadChannel | TextChannel;
36 let status: string | null = ("topic" in interaction.channel) ? interaction.channel!.topic : interaction.channel.name;
37 status = status ?? "";
38 if (status.endsWith("Archived")) { status = "Archived"; }
39 else { status = "Active"; }
40
41 const uID = channel.type === Discord.ChannelType.PrivateThread ? channel.name.split(" - ")[1] : channel.topic!.split(" ")[0];
42
43 if (status === "Archived") {
44 // Delete the ticket
45
pineafan63fc5e22022-08-04 22:04:10 +010046 const data = {
Skyler Grey75ea9172022-08-06 10:22:23 +010047 meta: {
PineaFan0d06edc2023-01-17 22:10:31 +000048 type: "ticketClosed",
49 displayName: "Ticket Closed",
pineafan813bdf42022-07-24 10:39:10 +010050 calculateType: "ticketUpdate",
51 color: NucleusColors.red,
pineafan63fc5e22022-08-04 22:04:10 +010052 emoji: "GUILD.TICKET.CLOSE",
pineafan813bdf42022-07-24 10:39:10 +010053 timestamp: new Date().getTime()
54 },
55 list: {
Skyler Grey75ea9172022-08-06 10:22:23 +010056 ticketFor: entry(
PineaFan0d06edc2023-01-17 22:10:31 +000057 uID!,
58 renderUser((await interaction.guild.members.fetch(uID!)).user)
Skyler Grey75ea9172022-08-06 10:22:23 +010059 ),
PineaFan0d06edc2023-01-17 22:10:31 +000060 closedBy: entry(interaction.member!.user.id, renderUser(interaction.member!.user as Discord.User)),
61 closed: entry(new Date().getTime(), renderDelta(new Date().getTime())),
62 ticketChannel: entry(channel.id, channel.name)
pineafan813bdf42022-07-24 10:39:10 +010063 },
64 hidden: {
65 guild: interaction.guild.id
66 }
pineafan63fc5e22022-08-04 22:04:10 +010067 };
pineafan813bdf42022-07-24 10:39:10 +010068 log(data);
PineaFan0d06edc2023-01-17 22:10:31 +000069
70 await channel.delete();
pineafane23c4ec2022-07-27 21:56:27 +010071 } else if (status === "Active") {
PineaFan0d06edc2023-01-17 22:10:31 +000072 // Close the ticket
73
74 if (channel.isThread()) {
75 channel.setName(`${channel.name.replace("Active", "Archived")}`);
76 channel.members.remove(channel.name.split(" - ")[1]!);
77 } else {
78 channel.setTopic(`${(channel.topic ?? "").replace("Active", "Archived")}`);
79 if (!channel.topic!.includes("Archived")) { channel.setTopic("0 Archived"); }
80 await channel.permissionOverwrites.delete(channel.topic!.split(" ")[0]!);
81 }
82 preloadPage(interaction.channel.id, "privacy", "2")
Skyler Grey75ea9172022-08-06 10:22:23 +010083 await interaction.reply({
84 embeds: [
85 new EmojiEmbed()
PineaFan0d06edc2023-01-17 22:10:31 +000086 .setTitle("Archived Ticket")
87 .setDescription(`This ticket has been Archived. Type ${await getCommandMentionByName("ticket/close")} to delete it.` +
88 await client.database.premium.hasPremium(interaction.guild.id) ?
89 `\n\nFor more info on transcripts, check ${await getCommandMentionByName("privacy")}` :
90 "")
Skyler Grey75ea9172022-08-06 10:22:23 +010091 .setStatus("Warning")
92 .setEmoji("GUILD.TICKET.ARCHIVED")
93 ],
94 components: [
PineaFan0d06edc2023-01-17 22:10:31 +000095 new ActionRowBuilder<ButtonBuilder>().addComponents(
Skyler Grey75ea9172022-08-06 10:22:23 +010096 [
TheCodedProf21c08592022-09-13 14:14:43 -040097 new ButtonBuilder()
Skyler Grey75ea9172022-08-06 10:22:23 +010098 .setLabel("Delete")
TheCodedProf21c08592022-09-13 14:14:43 -040099 .setStyle(ButtonStyle.Danger)
Skyler Grey75ea9172022-08-06 10:22:23 +0100100 .setCustomId("closeticket")
101 .setEmoji(getEmojiByName("CONTROL.CROSS", "id"))
102 ].concat(
PineaFan0d06edc2023-01-17 22:10:31 +0000103 await client.database.premium.hasPremium(interaction.guild.id)
Skyler Grey75ea9172022-08-06 10:22:23 +0100104 ? [
PineaFan0d06edc2023-01-17 22:10:31 +0000105 new ButtonBuilder()
106 .setLabel("Create Transcript and Delete")
107 .setStyle(ButtonStyle.Primary)
108 .setCustomId("createtranscript")
109 .setEmoji(getEmojiByName("CONTROL.DOWNLOAD", "id"))
110 ]
Skyler Grey75ea9172022-08-06 10:22:23 +0100111 : []
112 )
113 )
114 ]
115 });
PineaFan0d06edc2023-01-17 22:10:31 +0000116 const data = {
117 meta: {
118 type: "ticketClosed",
119 displayName: "Ticket Archived",
120 calculateType: "ticketUpdate",
121 color: NucleusColors.yellow,
122 emoji: "GUILD.TICKET.ARCHIVED",
123 timestamp: new Date().getTime()
124 },
125 list: {
126 ticketFor: entry(
127 uID!,
128 renderUser((await interaction.guild.members.fetch(uID!)).user)
129 ),
130 archivedBy: entry(interaction.member!.user.id, renderUser(interaction.member!.user as Discord.User)),
131 archived: entry(new Date().getTime(), renderDelta(new Date().getTime())),
132 ticketChannel: entry(channel.id, renderChannel(channel))
133 },
134 hidden: {
135 guild: interaction.guild.id
136 }
137 };
138 log(data);
pineafan813bdf42022-07-24 10:39:10 +0100139 }
PineaFan0d06edc2023-01-17 22:10:31 +0000140 return;
pineafan813bdf42022-07-24 10:39:10 +0100141}
142
PineaFan0d06edc2023-01-17 22:10:31 +0000143
144async function purgeByUser(member: string, guild: string) {
pineafan63fc5e22022-08-04 22:04:10 +0100145 const config = await client.database.guilds.read(guild.id);
PineaFan0d06edc2023-01-17 22:10:31 +0000146 const fetchedGuild = await client.guilds.fetch(guild);
pineafan813bdf42022-07-24 10:39:10 +0100147 if (!config.tickets.category) return;
PineaFan0d06edc2023-01-17 22:10:31 +0000148 const tickets = fetchedGuild.channels.cache.get(config.tickets.category);
pineafan813bdf42022-07-24 10:39:10 +0100149 if (!tickets) return;
pineafan63fc5e22022-08-04 22:04:10 +0100150 const ticketChannels = tickets.children;
151 let deleted = 0;
Skyler Grey75ea9172022-08-06 10:22:23 +0100152 ticketChannels.forEach((element) => {
pineafane23c4ec2022-07-27 21:56:27 +0100153 if (element.type !== "GUILD_TEXT") return;
154 if (element.topic.split(" ")[0] === member) {
pineafan63fc5e22022-08-04 22:04:10 +0100155 try {
156 element.delete();
Skyler Grey75ea9172022-08-06 10:22:23 +0100157 } catch {
158 /* Errors if the channel does not exist (deleted already) */
159 }
pineafan63fc5e22022-08-04 22:04:10 +0100160 deleted++;
pineafan813bdf42022-07-24 10:39:10 +0100161 }
162 });
163 if (deleted) {
Skyler Grey11236ba2022-08-08 21:13:33 +0100164 const { log, NucleusColors, entry, renderUser, renderDelta } = member.client.logger;
pineafan63fc5e22022-08-04 22:04:10 +0100165 const data = {
Skyler Grey75ea9172022-08-06 10:22:23 +0100166 meta: {
pineafan63fc5e22022-08-04 22:04:10 +0100167 type: "ticketPurge",
168 displayName: "Tickets Purged",
pineafan02ba0232022-07-24 22:16:15 +0100169 calculateType: "ticketUpdate",
170 color: NucleusColors.red,
pineafan63fc5e22022-08-04 22:04:10 +0100171 emoji: "GUILD.TICKET.DELETE",
pineafan02ba0232022-07-24 22:16:15 +0100172 timestamp: new Date().getTime()
173 },
174 list: {
175 ticketFor: entry(member, renderUser(member)),
176 deletedBy: entry(null, "Member left server"),
Skyler Grey11236ba2022-08-08 21:13:33 +0100177 deleted: entry(new Date().getTime(), renderDelta(new Date().getTime())),
pineafan63fc5e22022-08-04 22:04:10 +0100178 ticketsDeleted: deleted
pineafan02ba0232022-07-24 22:16:15 +0100179 },
180 hidden: {
181 guild: guild.id
pineafan813bdf42022-07-24 10:39:10 +0100182 }
pineafan63fc5e22022-08-04 22:04:10 +0100183 };
pineafan02ba0232022-07-24 22:16:15 +0100184 log(data);
pineafan813bdf42022-07-24 10:39:10 +0100185 }
186}
187
PineaFan0d06edc2023-01-17 22:10:31 +0000188export { purgeByUser };