blob: 50bcbcf319259b8e9211e439be85b6d03c67d623 [file] [log] [blame]
Skyler Grey11236ba2022-08-08 21:13:33 +01001import {
2 CommandInteraction,
3 DMChannel,
4 Message,
5 MessageActionRow,
6 MessageButton,
Skyler Grey11236ba2022-08-08 21:13:33 +01007 TextChannel
8} from "discord.js";
pineafan813bdf42022-07-24 10:39:10 +01009import EmojiEmbed from "../utils/generateEmojiEmbed.js";
10import getEmojiByName from "../utils/getEmojiByName.js";
11import { PasteClient, Publicity, ExpireDate } from "pastebin-api";
Skyler Grey75ea9172022-08-06 10:22:23 +010012import config from "../config/main.json" assert { type: "json" };
pineafan813bdf42022-07-24 10:39:10 +010013import client from "../utils/client.js";
14
pineafan63fc5e22022-08-04 22:04:10 +010015const pbClient = new PasteClient(config.pastebinApiKey);
pineafan813bdf42022-07-24 10:39:10 +010016
Skyler Grey11236ba2022-08-08 21:13:33 +010017export default async function (interaction: CommandInteraction) {
18 if (interaction.channel === null) return;
pineafan4e425942022-08-08 22:01:47 +010019 if (!(interaction.channel instanceof TextChannel)) return;
Skyler Grey11236ba2022-08-08 21:13:33 +010020 const { log, NucleusColors, entry, renderUser, renderDelta } = client.logger;
pineafan813bdf42022-07-24 10:39:10 +010021
Skyler Grey11236ba2022-08-08 21:13:33 +010022 let messages: Message[] = [];
23 let deletedCount: number;
pineafan813bdf42022-07-24 10:39:10 +010024
Skyler Grey11236ba2022-08-08 21:13:33 +010025 do {
26 const fetched = await (interaction.channel as TextChannel).messages.fetch({ limit: 100 });
27 const deleted = await (interaction.channel as TextChannel).bulkDelete(fetched, true);
28 deletedCount = deleted.size;
29 messages = messages.concat(Array.from(deleted.values()));
30 } while (deletedCount === 100);
31
pineafan63fc5e22022-08-04 22:04:10 +010032 let out = "";
Skyler Grey75ea9172022-08-06 10:22:23 +010033 messages.reverse().forEach((message) => {
pineafan813bdf42022-07-24 10:39:10 +010034 if (!message.author.bot) {
pineafan63fc5e22022-08-04 22:04:10 +010035 const sentDate = new Date(message.createdTimestamp);
Skyler Grey11236ba2022-08-08 21:13:33 +010036 out += `${message.author.username}#${message.author.discriminator} (${
37 message.author.id
38 }) [${sentDate.toUTCString()}]\n`;
pineafan63fc5e22022-08-04 22:04:10 +010039 const lines = message.content.split("\n");
Skyler Grey75ea9172022-08-06 10:22:23 +010040 lines.forEach((line) => {
41 out += `> ${line}\n`;
42 });
pineafan63fc5e22022-08-04 22:04:10 +010043 out += "\n\n";
pineafan813bdf42022-07-24 10:39:10 +010044 }
pineafan63fc5e22022-08-04 22:04:10 +010045 });
pineafan4e425942022-08-08 22:01:47 +010046 const member = interaction.guild!.members.cache.get(interaction.channel.topic.split(" ")[0]);
pineafan813bdf42022-07-24 10:39:10 +010047 let m;
48 if (out !== "") {
49 const url = await pbClient.createPaste({
50 code: out,
51 expireDate: ExpireDate.Never,
pineafan4e425942022-08-08 22:01:47 +010052 name: `Ticket Transcript ${member ? ("for " + member.user.username + "#" + member.user.discriminator + " ") : ""}` +
53 `(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 });
pineafan4e425942022-08-08 22:01:47 +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: {
pineafan4e425942022-08-08 22:01:47 +0100124 ticketFor: member ? entry(
125 member.id,
126 renderUser(member.user)
127 ) : entry(null, "*Unknown*"),
128 deletedBy: entry(interaction.member!.user.id, renderUser(interaction.member!.user)),
Skyler Grey11236ba2022-08-08 21:13:33 +0100129 deleted: entry(new Date().getTime(), renderDelta(new Date().getTime()))
pineafan813bdf42022-07-24 10:39:10 +0100130 },
131 hidden: {
pineafan4e425942022-08-08 22:01:47 +0100132 guild: interaction.guild!.id
pineafan813bdf42022-07-24 10:39:10 +0100133 }
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}