blob: 97d0d28b70b66a459ae1e0b69103f961816b2af7 [file] [log] [blame]
Skyler Greyf21323a2022-08-13 23:58:22 +01001import {
2 CommandInteraction,
3 GuildMember,
4 Message,
5 MessageActionRow,
6 MessageButton,
7 MessageComponentInteraction,
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
pineafan0f5cc782022-08-12 21:55:42 +010018export default async function (interaction: CommandInteraction | MessageComponentInteraction) {
Skyler Grey11236ba2022-08-08 21:13:33 +010019 if (interaction.channel === null) return;
pineafan4e425942022-08-08 22:01:47 +010020 if (!(interaction.channel instanceof TextChannel)) return;
Skyler Grey11236ba2022-08-08 21:13:33 +010021 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 });
pineafan3a02ea32022-08-11 21:35:04 +010047 const topic = interaction.channel.topic;
48 let member: GuildMember | null = null;
49 if (topic !== null) {
50 const part = topic.split(" ")[0] ?? null;
51 if (part !== null) member = interaction.guild!.members.cache.get(part) ?? null;
52 }
53 let m: Message;
pineafan813bdf42022-07-24 10:39:10 +010054 if (out !== "") {
55 const url = await pbClient.createPaste({
56 code: out,
57 expireDate: ExpireDate.Never,
pineafan3a02ea32022-08-11 21:35:04 +010058 name:
59 `Ticket Transcript ${
60 member ? "for " + member.user.username + "#" + member.user.discriminator + " " : ""
61 }` + `(Created at ${new Date(interaction.channel.createdTimestamp).toDateString()})`,
pineafan63fc5e22022-08-04 22:04:10 +010062 publicity: Publicity.Unlisted
63 });
pineafan4e425942022-08-08 22:01:47 +010064 const guildConfig = await client.database.guilds.read(interaction.guild!.id);
pineafan3a02ea32022-08-11 21:35:04 +010065 m = (await interaction.reply({
Skyler Grey75ea9172022-08-06 10:22:23 +010066 embeds: [
67 new EmojiEmbed()
68 .setTitle("Transcript")
69 .setDescription(
70 "You can view the transcript using the link below. You can save the link for later" +
71 (guildConfig.logging.logs.channel
72 ? ` or find it in <#${guildConfig.logging.logs.channel}> once you press delete below. After this the channel will be deleted.`
73 : ".")
74 )
75 .setStatus("Success")
76 .setEmoji("CONTROL.DOWNLOAD")
77 ],
78 components: [
79 new MessageActionRow().addComponents([
Skyler Grey11236ba2022-08-08 21:13:33 +010080 new MessageButton().setLabel("View").setStyle("LINK").setURL(url),
Skyler Grey75ea9172022-08-06 10:22:23 +010081 new MessageButton()
82 .setLabel("Delete")
83 .setStyle("DANGER")
84 .setCustomId("close")
85 .setEmoji(getEmojiByName("CONTROL.CROSS", "id"))
86 ])
87 ],
88 fetchReply: true
pineafan3a02ea32022-08-11 21:35:04 +010089 })) as Message;
pineafan813bdf42022-07-24 10:39:10 +010090 } else {
pineafan3a02ea32022-08-11 21:35:04 +010091 m = (await interaction.reply({
Skyler Grey75ea9172022-08-06 10:22:23 +010092 embeds: [
93 new EmojiEmbed()
94 .setTitle("Transcript")
95 .setDescription(
96 "The transcript was empty, so no changes were made. To delete this ticket, press the delete button below."
97 )
98 .setStatus("Success")
99 .setEmoji("CONTROL.DOWNLOAD")
100 ],
101 components: [
102 new MessageActionRow().addComponents([
103 new MessageButton()
104 .setLabel("Delete")
105 .setStyle("DANGER")
106 .setCustomId("close")
107 .setEmoji(getEmojiByName("CONTROL.CROSS", "id"))
108 ])
109 ],
110 fetchReply: true
pineafan3a02ea32022-08-11 21:35:04 +0100111 })) as Message;
pineafan813bdf42022-07-24 10:39:10 +0100112 }
113 let i;
114 try {
115 i = await m.awaitMessageComponent({ time: 300000 });
pineafan63fc5e22022-08-04 22:04:10 +0100116 i.deferUpdate();
Skyler Grey75ea9172022-08-06 10:22:23 +0100117 } catch {
118 return;
119 }
pineafan63fc5e22022-08-04 22:04:10 +0100120 const data = {
Skyler Grey75ea9172022-08-06 10:22:23 +0100121 meta: {
pineafan63fc5e22022-08-04 22:04:10 +0100122 type: "ticketDeleted",
123 displayName: "Ticket Deleted",
pineafan813bdf42022-07-24 10:39:10 +0100124 calculateType: "ticketUpdate",
125 color: NucleusColors.red,
pineafan63fc5e22022-08-04 22:04:10 +0100126 emoji: "GUILD.TICKET.CLOSE",
pineafan813bdf42022-07-24 10:39:10 +0100127 timestamp: new Date().getTime()
128 },
129 list: {
pineafan3a02ea32022-08-11 21:35:04 +0100130 ticketFor: member ? entry(member.id, renderUser(member.user)) : entry(null, "*Unknown*"),
pineafan4e425942022-08-08 22:01:47 +0100131 deletedBy: entry(interaction.member!.user.id, renderUser(interaction.member!.user)),
Skyler Grey11236ba2022-08-08 21:13:33 +0100132 deleted: entry(new Date().getTime(), renderDelta(new Date().getTime()))
pineafan813bdf42022-07-24 10:39:10 +0100133 },
134 hidden: {
pineafan4e425942022-08-08 22:01:47 +0100135 guild: interaction.guild!.id
pineafan813bdf42022-07-24 10:39:10 +0100136 }
pineafan63fc5e22022-08-04 22:04:10 +0100137 };
pineafan813bdf42022-07-24 10:39:10 +0100138 log(data);
pineafan63fc5e22022-08-04 22:04:10 +0100139 await interaction.channel.delete();
140 return;
Skyler Grey75ea9172022-08-06 10:22:23 +0100141}