blob: dd01a987f16c0f7e2e8ecf92e294e7f2bf38d725 [file] [log] [blame]
Skyler Greyf21323a2022-08-13 23:58:22 +01001import {
2 CommandInteraction,
3 GuildMember,
4 Message,
TheCodedProf21c08592022-09-13 14:14:43 -04005 ActionRowBuilder,
6 ButtonBuilder,
Skyler Greyf21323a2022-08-13 23:58:22 +01007 MessageComponentInteraction,
TheCodedProf21c08592022-09-13 14:14:43 -04008 TextChannel,
PineaFan538d3752023-01-12 21:48:23 +00009 ButtonStyle,
Samuel Shuert27bf3cd2023-03-03 15:51:25 -050010 User,
11 ThreadChannel
Skyler Greyf21323a2022-08-13 23:58:22 +010012} from "discord.js";
pineafan813bdf42022-07-24 10:39:10 +010013import EmojiEmbed from "../utils/generateEmojiEmbed.js";
14import getEmojiByName from "../utils/getEmojiByName.js";
pineafan813bdf42022-07-24 10:39:10 +010015import client from "../utils/client.js";
Samuel Shuert27bf3cd2023-03-03 15:51:25 -050016import { messageException } from '../utils/createTemporaryStorage.js';
pineafan813bdf42022-07-24 10:39:10 +010017
Samuel Shuert27bf3cd2023-03-03 15:51:25 -050018const noTopic = new EmojiEmbed()
19 .setTitle("User not found")
20 .setDescription("There is no user associated with this ticket.")
21 .setStatus("Danger")
22 .setEmoji("CONTROL.BLOCKCROSS")
pineafan813bdf42022-07-24 10:39:10 +010023
pineafan0f5cc782022-08-12 21:55:42 +010024export default async function (interaction: CommandInteraction | MessageComponentInteraction) {
Skyler Grey11236ba2022-08-08 21:13:33 +010025 if (interaction.channel === null) return;
Samuel Shuert27bf3cd2023-03-03 15:51:25 -050026 if (!(interaction.channel instanceof TextChannel || interaction.channel instanceof ThreadChannel)) return;
Skyler Grey11236ba2022-08-08 21:13:33 +010027 const { log, NucleusColors, entry, renderUser, renderDelta } = client.logger;
pineafan813bdf42022-07-24 10:39:10 +010028
Skyler Grey11236ba2022-08-08 21:13:33 +010029 let messages: Message[] = [];
30 let deletedCount: number;
pineafan813bdf42022-07-24 10:39:10 +010031
Skyler Grey11236ba2022-08-08 21:13:33 +010032 do {
33 const fetched = await (interaction.channel as TextChannel).messages.fetch({ limit: 100 });
34 const deleted = await (interaction.channel as TextChannel).bulkDelete(fetched, true);
35 deletedCount = deleted.size;
TheCodedProfbc94a5c2023-01-18 18:59:31 -050036 messages = messages.concat(Array.from(deleted.values() as Iterable<Message>));
Samuel Shuert27bf3cd2023-03-03 15:51:25 -050037 if (messages.length === 1) messageException(interaction.guild!.id, interaction.channel.id, messages[0]!.id)
Skyler Grey11236ba2022-08-08 21:13:33 +010038 } while (deletedCount === 100);
Samuel Shuert27bf3cd2023-03-03 15:51:25 -050039 messages = messages.filter(message => !(
40 message.components.some(
41 component => component.components.some(
42 child => child.customId?.includes("transcript") ?? false
43 )
44 )
45 ));
Skyler Grey11236ba2022-08-08 21:13:33 +010046
Samuel Shuert27bf3cd2023-03-03 15:51:25 -050047 let topic
48 let member: GuildMember = interaction.guild?.members.me!;
49 if (interaction.channel instanceof TextChannel) {
50 topic = interaction.channel.topic;
51 if (topic === null) return await interaction.reply({ embeds: [noTopic] });
52 const mem = interaction.guild!.members.cache.get(topic.split(" ")[1]!);
53 if (mem) member = mem;
pineafan813bdf42022-07-24 10:39:10 +010054 } else {
Samuel Shuert27bf3cd2023-03-03 15:51:25 -050055 topic = interaction.channel.name;
56 const split = topic.split("-").map(p => p.trim()) as [string, string, string];
57 const mem = interaction.guild!.members.cache.get(split[1])
58 if (mem) member = mem;
pineafan813bdf42022-07-24 10:39:10 +010059 }
Samuel Shuert27bf3cd2023-03-03 15:51:25 -050060
61 const newOut = await client.database.transcripts.createTranscript(messages, interaction, member);
62
Samuel Shuertcc63dee2023-03-03 18:54:29 -050063
64 const [code, key, iv] = await client.database.transcripts.create(newOut);
65
Samuel Shuert27bf3cd2023-03-03 15:51:25 -050066 if(!code) return await interaction.reply({
67 embeds: [
68 new EmojiEmbed()
69 .setTitle("Error")
70 .setDescription("An error occurred while creating the transcript.")
71 .setStatus("Danger")
72 .setEmoji("CONTROL.BLOCKCROSS")
73 ]
74 })
75 const guildConfig = await client.database.guilds.read(interaction.guild!.id);
76 const m: Message = (await interaction.reply({
77 embeds: [
78 new EmojiEmbed()
79 .setTitle("Transcript")
80 .setDescription(
81 "You can view the transcript using the link below. You can save the link for later" +
82 (guildConfig.logging.logs.channel
83 ? ` or find it in <#${guildConfig.logging.logs.channel}> once you press delete below. After this the channel will be deleted.`
84 : ".")
85 )
86 .setStatus("Success")
87 .setEmoji("CONTROL.DOWNLOAD")
88 ],
89 components: [
90 new ActionRowBuilder<ButtonBuilder>().addComponents([
Samuel Shuertcc63dee2023-03-03 18:54:29 -050091
92 new ButtonBuilder().setLabel("View").setStyle(ButtonStyle.Link).setURL(`https://testing.coded.codes/nucleus/transcript/${code}?key=${key}&iv=${iv}`),
93
Samuel Shuert27bf3cd2023-03-03 15:51:25 -050094 new ButtonBuilder()
95 .setLabel("Delete")
96 .setStyle(ButtonStyle.Danger)
97 .setCustomId("close")
98 .setEmoji(getEmojiByName("CONTROL.CROSS", "id"))
99 ])
100 ],
101 fetchReply: true
102 })) as Message;
pineafan813bdf42022-07-24 10:39:10 +0100103 let i;
104 try {
PineaFan0d06edc2023-01-17 22:10:31 +0000105 i = await m.awaitMessageComponent({
106 time: 300000,
Samuel Shuert27bf3cd2023-03-03 15:51:25 -0500107 filter: (i) => { return i.user.id === interaction.user.id && i.channel!.id === interaction.channel!.id && i.message.id === m.id }
PineaFan0d06edc2023-01-17 22:10:31 +0000108 });
Samuel Shuert27bf3cd2023-03-03 15:51:25 -0500109 await i.deferUpdate();
Skyler Grey75ea9172022-08-06 10:22:23 +0100110 } catch {
111 return;
112 }
pineafan63fc5e22022-08-04 22:04:10 +0100113 const data = {
Skyler Grey75ea9172022-08-06 10:22:23 +0100114 meta: {
pineafan63fc5e22022-08-04 22:04:10 +0100115 type: "ticketDeleted",
116 displayName: "Ticket Deleted",
pineafan813bdf42022-07-24 10:39:10 +0100117 calculateType: "ticketUpdate",
118 color: NucleusColors.red,
pineafan63fc5e22022-08-04 22:04:10 +0100119 emoji: "GUILD.TICKET.CLOSE",
Samuel Shuert27bf3cd2023-03-03 15:51:25 -0500120 timestamp: Date.now()
pineafan813bdf42022-07-24 10:39:10 +0100121 },
122 list: {
Samuel Shuert27bf3cd2023-03-03 15:51:25 -0500123 ticketFor: entry(member.id, renderUser(member.user)),
PineaFan538d3752023-01-12 21:48:23 +0000124 deletedBy: entry(interaction.member!.user.id, renderUser(interaction.member!.user as User)),
Samuel Shuert27bf3cd2023-03-03 15:51:25 -0500125 deleted: entry(Date.now().toString(), renderDelta(Date.now())),
126 transcript: entry(code, `https://clicks.codes/nucleus/transcript/${code}`)
pineafan813bdf42022-07-24 10:39:10 +0100127 },
128 hidden: {
pineafan4e425942022-08-08 22:01:47 +0100129 guild: interaction.guild!.id
pineafan813bdf42022-07-24 10:39:10 +0100130 }
pineafan63fc5e22022-08-04 22:04:10 +0100131 };
pineafan813bdf42022-07-24 10:39:10 +0100132 log(data);
pineafan63fc5e22022-08-04 22:04:10 +0100133 await interaction.channel.delete();
134 return;
Skyler Grey75ea9172022-08-06 10:22:23 +0100135}