blob: 57720bfcc250bef8dd29f7ff9d998719173e11aa [file] [log] [blame]
pineafan0f5cc782022-08-12 21:55:42 +01001import { CommandInteraction, GuildMember, Message, MessageActionRow, MessageButton, MessageComponentInteraction, TextChannel } from "discord.js";
pineafan813bdf42022-07-24 10:39:10 +01002import EmojiEmbed from "../utils/generateEmojiEmbed.js";
3import getEmojiByName from "../utils/getEmojiByName.js";
4import { PasteClient, Publicity, ExpireDate } from "pastebin-api";
Skyler Grey75ea9172022-08-06 10:22:23 +01005import config from "../config/main.json" assert { type: "json" };
pineafan813bdf42022-07-24 10:39:10 +01006import client from "../utils/client.js";
7
pineafan63fc5e22022-08-04 22:04:10 +01008const pbClient = new PasteClient(config.pastebinApiKey);
pineafan813bdf42022-07-24 10:39:10 +01009
pineafan0f5cc782022-08-12 21:55:42 +010010export default async function (interaction: CommandInteraction | MessageComponentInteraction) {
Skyler Grey11236ba2022-08-08 21:13:33 +010011 if (interaction.channel === null) return;
pineafan4e425942022-08-08 22:01:47 +010012 if (!(interaction.channel instanceof TextChannel)) return;
Skyler Grey11236ba2022-08-08 21:13:33 +010013 const { log, NucleusColors, entry, renderUser, renderDelta } = client.logger;
pineafan813bdf42022-07-24 10:39:10 +010014
Skyler Grey11236ba2022-08-08 21:13:33 +010015 let messages: Message[] = [];
16 let deletedCount: number;
pineafan813bdf42022-07-24 10:39:10 +010017
Skyler Grey11236ba2022-08-08 21:13:33 +010018 do {
19 const fetched = await (interaction.channel as TextChannel).messages.fetch({ limit: 100 });
20 const deleted = await (interaction.channel as TextChannel).bulkDelete(fetched, true);
21 deletedCount = deleted.size;
22 messages = messages.concat(Array.from(deleted.values()));
23 } while (deletedCount === 100);
24
pineafan63fc5e22022-08-04 22:04:10 +010025 let out = "";
Skyler Grey75ea9172022-08-06 10:22:23 +010026 messages.reverse().forEach((message) => {
pineafan813bdf42022-07-24 10:39:10 +010027 if (!message.author.bot) {
pineafan63fc5e22022-08-04 22:04:10 +010028 const sentDate = new Date(message.createdTimestamp);
Skyler Grey11236ba2022-08-08 21:13:33 +010029 out += `${message.author.username}#${message.author.discriminator} (${
30 message.author.id
31 }) [${sentDate.toUTCString()}]\n`;
pineafan63fc5e22022-08-04 22:04:10 +010032 const lines = message.content.split("\n");
Skyler Grey75ea9172022-08-06 10:22:23 +010033 lines.forEach((line) => {
34 out += `> ${line}\n`;
35 });
pineafan63fc5e22022-08-04 22:04:10 +010036 out += "\n\n";
pineafan813bdf42022-07-24 10:39:10 +010037 }
pineafan63fc5e22022-08-04 22:04:10 +010038 });
pineafan3a02ea32022-08-11 21:35:04 +010039 const topic = interaction.channel.topic;
40 let member: GuildMember | null = null;
41 if (topic !== null) {
42 const part = topic.split(" ")[0] ?? null;
43 if (part !== null) member = interaction.guild!.members.cache.get(part) ?? null;
44 }
45 let m: Message;
pineafan813bdf42022-07-24 10:39:10 +010046 if (out !== "") {
47 const url = await pbClient.createPaste({
48 code: out,
49 expireDate: ExpireDate.Never,
pineafan3a02ea32022-08-11 21:35:04 +010050 name:
51 `Ticket Transcript ${
52 member ? "for " + member.user.username + "#" + member.user.discriminator + " " : ""
53 }` + `(Created at ${new Date(interaction.channel.createdTimestamp).toDateString()})`,
pineafan63fc5e22022-08-04 22:04:10 +010054 publicity: Publicity.Unlisted
55 });
pineafan4e425942022-08-08 22:01:47 +010056 const guildConfig = await client.database.guilds.read(interaction.guild!.id);
pineafan3a02ea32022-08-11 21:35:04 +010057 m = (await interaction.reply({
Skyler Grey75ea9172022-08-06 10:22:23 +010058 embeds: [
59 new EmojiEmbed()
60 .setTitle("Transcript")
61 .setDescription(
62 "You can view the transcript using the link below. You can save the link for later" +
63 (guildConfig.logging.logs.channel
64 ? ` or find it in <#${guildConfig.logging.logs.channel}> once you press delete below. After this the channel will be deleted.`
65 : ".")
66 )
67 .setStatus("Success")
68 .setEmoji("CONTROL.DOWNLOAD")
69 ],
70 components: [
71 new MessageActionRow().addComponents([
Skyler Grey11236ba2022-08-08 21:13:33 +010072 new MessageButton().setLabel("View").setStyle("LINK").setURL(url),
Skyler Grey75ea9172022-08-06 10:22:23 +010073 new MessageButton()
74 .setLabel("Delete")
75 .setStyle("DANGER")
76 .setCustomId("close")
77 .setEmoji(getEmojiByName("CONTROL.CROSS", "id"))
78 ])
79 ],
80 fetchReply: true
pineafan3a02ea32022-08-11 21:35:04 +010081 })) as Message;
pineafan813bdf42022-07-24 10:39:10 +010082 } else {
pineafan3a02ea32022-08-11 21:35:04 +010083 m = (await interaction.reply({
Skyler Grey75ea9172022-08-06 10:22:23 +010084 embeds: [
85 new EmojiEmbed()
86 .setTitle("Transcript")
87 .setDescription(
88 "The transcript was empty, so no changes were made. To delete this ticket, press the delete button below."
89 )
90 .setStatus("Success")
91 .setEmoji("CONTROL.DOWNLOAD")
92 ],
93 components: [
94 new MessageActionRow().addComponents([
95 new MessageButton()
96 .setLabel("Delete")
97 .setStyle("DANGER")
98 .setCustomId("close")
99 .setEmoji(getEmojiByName("CONTROL.CROSS", "id"))
100 ])
101 ],
102 fetchReply: true
pineafan3a02ea32022-08-11 21:35:04 +0100103 })) as Message;
pineafan813bdf42022-07-24 10:39:10 +0100104 }
105 let i;
106 try {
107 i = await m.awaitMessageComponent({ time: 300000 });
pineafan63fc5e22022-08-04 22:04:10 +0100108 i.deferUpdate();
Skyler Grey75ea9172022-08-06 10:22:23 +0100109 } catch {
110 return;
111 }
pineafan63fc5e22022-08-04 22:04:10 +0100112 const data = {
Skyler Grey75ea9172022-08-06 10:22:23 +0100113 meta: {
pineafan63fc5e22022-08-04 22:04:10 +0100114 type: "ticketDeleted",
115 displayName: "Ticket Deleted",
pineafan813bdf42022-07-24 10:39:10 +0100116 calculateType: "ticketUpdate",
117 color: NucleusColors.red,
pineafan63fc5e22022-08-04 22:04:10 +0100118 emoji: "GUILD.TICKET.CLOSE",
pineafan813bdf42022-07-24 10:39:10 +0100119 timestamp: new Date().getTime()
120 },
121 list: {
pineafan3a02ea32022-08-11 21:35:04 +0100122 ticketFor: member ? entry(member.id, renderUser(member.user)) : entry(null, "*Unknown*"),
pineafan4e425942022-08-08 22:01:47 +0100123 deletedBy: entry(interaction.member!.user.id, renderUser(interaction.member!.user)),
Skyler Grey11236ba2022-08-08 21:13:33 +0100124 deleted: entry(new Date().getTime(), renderDelta(new Date().getTime()))
pineafan813bdf42022-07-24 10:39:10 +0100125 },
126 hidden: {
pineafan4e425942022-08-08 22:01:47 +0100127 guild: interaction.guild!.id
pineafan813bdf42022-07-24 10:39:10 +0100128 }
pineafan63fc5e22022-08-04 22:04:10 +0100129 };
pineafan813bdf42022-07-24 10:39:10 +0100130 log(data);
pineafan63fc5e22022-08-04 22:04:10 +0100131 await interaction.channel.delete();
132 return;
Skyler Grey75ea9172022-08-06 10:22:23 +0100133}