blob: 5fbb8adf74634bb0b53b6684385697ee958bbaf7 [file] [log] [blame]
Skyler Grey11236ba2022-08-08 21:13:33 +01001import {
2 CommandInteraction,
3 DMChannel,
4 Message,
5 MessageActionRow,
6 MessageButton,
7 PartialGroupDMChannel,
8 TextChannel
9} from "discord.js";
pineafan813bdf42022-07-24 10:39:10 +010010import EmojiEmbed from "../utils/generateEmojiEmbed.js";
11import getEmojiByName from "../utils/getEmojiByName.js";
12import { PasteClient, Publicity, ExpireDate } from "pastebin-api";
Skyler Grey75ea9172022-08-06 10:22:23 +010013import config from "../config/main.json" assert { type: "json" };
pineafan813bdf42022-07-24 10:39:10 +010014import client from "../utils/client.js";
15
pineafan63fc5e22022-08-04 22:04:10 +010016const pbClient = new PasteClient(config.pastebinApiKey);
pineafan813bdf42022-07-24 10:39:10 +010017
Skyler Grey11236ba2022-08-08 21:13:33 +010018export default async function (interaction: CommandInteraction) {
19 if (interaction.channel === null) return;
20 if (interaction.channel instanceof DMChannel) return;
21 const { log, NucleusColors, entry, renderUser, renderDelta } = client.logger;
pineafan813bdf42022-07-24 10:39:10 +010022
Skyler Grey11236ba2022-08-08 21:13:33 +010023 let messages: Message[] = [];
24 let deletedCount: number;
pineafan813bdf42022-07-24 10:39:10 +010025
Skyler Grey11236ba2022-08-08 21:13:33 +010026 do {
27 const fetched = await (interaction.channel as TextChannel).messages.fetch({ limit: 100 });
28 const deleted = await (interaction.channel as TextChannel).bulkDelete(fetched, true);
29 deletedCount = deleted.size;
30 messages = messages.concat(Array.from(deleted.values()));
31 } while (deletedCount === 100);
32
pineafan63fc5e22022-08-04 22:04:10 +010033 let out = "";
Skyler Grey75ea9172022-08-06 10:22:23 +010034 messages.reverse().forEach((message) => {
pineafan813bdf42022-07-24 10:39:10 +010035 if (!message.author.bot) {
pineafan63fc5e22022-08-04 22:04:10 +010036 const sentDate = new Date(message.createdTimestamp);
Skyler Grey11236ba2022-08-08 21:13:33 +010037 out += `${message.author.username}#${message.author.discriminator} (${
38 message.author.id
39 }) [${sentDate.toUTCString()}]\n`;
pineafan63fc5e22022-08-04 22:04:10 +010040 const lines = message.content.split("\n");
Skyler Grey75ea9172022-08-06 10:22:23 +010041 lines.forEach((line) => {
42 out += `> ${line}\n`;
43 });
pineafan63fc5e22022-08-04 22:04:10 +010044 out += "\n\n";
pineafan813bdf42022-07-24 10:39:10 +010045 }
pineafan63fc5e22022-08-04 22:04:10 +010046 });
Skyler Grey11236ba2022-08-08 21:13:33 +010047 const member = interaction.channel.guild.members.cache.get(interaction.channel.topic.split(" ")[0]);
pineafan813bdf42022-07-24 10:39:10 +010048 let m;
49 if (out !== "") {
50 const url = await pbClient.createPaste({
51 code: out,
52 expireDate: ExpireDate.Never,
Skyler Grey11236ba2022-08-08 21:13:33 +010053 name: `Ticket Transcript for ${member.user.username}#${member.user.discriminator} (Created at ${new Date(
Skyler Grey75ea9172022-08-06 10:22:23 +010054 interaction.channel.createdTimestamp
55 ).toDateString()})`,
pineafan63fc5e22022-08-04 22:04:10 +010056 publicity: Publicity.Unlisted
57 });
Skyler Grey11236ba2022-08-08 21:13:33 +010058 const guildConfig = await client.database.guilds.read(interaction.guild.id);
Skyler Grey75ea9172022-08-06 10:22:23 +010059 m = await interaction.reply({
60 embeds: [
61 new EmojiEmbed()
62 .setTitle("Transcript")
63 .setDescription(
64 "You can view the transcript using the link below. You can save the link for later" +
65 (guildConfig.logging.logs.channel
66 ? ` or find it in <#${guildConfig.logging.logs.channel}> once you press delete below. After this the channel will be deleted.`
67 : ".")
68 )
69 .setStatus("Success")
70 .setEmoji("CONTROL.DOWNLOAD")
71 ],
72 components: [
73 new MessageActionRow().addComponents([
Skyler Grey11236ba2022-08-08 21:13:33 +010074 new MessageButton().setLabel("View").setStyle("LINK").setURL(url),
Skyler Grey75ea9172022-08-06 10:22:23 +010075 new MessageButton()
76 .setLabel("Delete")
77 .setStyle("DANGER")
78 .setCustomId("close")
79 .setEmoji(getEmojiByName("CONTROL.CROSS", "id"))
80 ])
81 ],
82 fetchReply: true
83 });
pineafan813bdf42022-07-24 10:39:10 +010084 } else {
Skyler Grey75ea9172022-08-06 10:22:23 +010085 m = await interaction.reply({
86 embeds: [
87 new EmojiEmbed()
88 .setTitle("Transcript")
89 .setDescription(
90 "The transcript was empty, so no changes were made. To delete this ticket, press the delete button below."
91 )
92 .setStatus("Success")
93 .setEmoji("CONTROL.DOWNLOAD")
94 ],
95 components: [
96 new MessageActionRow().addComponents([
97 new MessageButton()
98 .setLabel("Delete")
99 .setStyle("DANGER")
100 .setCustomId("close")
101 .setEmoji(getEmojiByName("CONTROL.CROSS", "id"))
102 ])
103 ],
104 fetchReply: true
105 });
pineafan813bdf42022-07-24 10:39:10 +0100106 }
107 let i;
108 try {
109 i = await m.awaitMessageComponent({ time: 300000 });
pineafan63fc5e22022-08-04 22:04:10 +0100110 i.deferUpdate();
Skyler Grey75ea9172022-08-06 10:22:23 +0100111 } catch {
112 return;
113 }
pineafan63fc5e22022-08-04 22:04:10 +0100114 const data = {
Skyler Grey75ea9172022-08-06 10:22:23 +0100115 meta: {
pineafan63fc5e22022-08-04 22:04:10 +0100116 type: "ticketDeleted",
117 displayName: "Ticket Deleted",
pineafan813bdf42022-07-24 10:39:10 +0100118 calculateType: "ticketUpdate",
119 color: NucleusColors.red,
pineafan63fc5e22022-08-04 22:04:10 +0100120 emoji: "GUILD.TICKET.CLOSE",
pineafan813bdf42022-07-24 10:39:10 +0100121 timestamp: new Date().getTime()
122 },
123 list: {
Skyler Grey75ea9172022-08-06 10:22:23 +0100124 ticketFor: entry(
125 interaction.channel.topic.split(" ")[0],
Skyler Grey11236ba2022-08-08 21:13:33 +0100126 renderUser((await interaction.guild.members.fetch(interaction.channel.topic.split(" ")[0])).user)
Skyler Grey75ea9172022-08-06 10:22:23 +0100127 ),
Skyler Grey11236ba2022-08-08 21:13:33 +0100128 deletedBy: entry(interaction.member.user.id, renderUser(interaction.member.user)),
129 deleted: entry(new Date().getTime(), renderDelta(new Date().getTime()))
pineafan813bdf42022-07-24 10:39:10 +0100130 },
131 hidden: {
132 guild: interaction.guild.id
133 }
pineafan63fc5e22022-08-04 22:04:10 +0100134 };
pineafan813bdf42022-07-24 10:39:10 +0100135 log(data);
pineafan63fc5e22022-08-04 22:04:10 +0100136 await interaction.channel.delete();
137 return;
Skyler Grey75ea9172022-08-06 10:22:23 +0100138}