blob: e755b835e31e8c8bf91e7b823c9212d136be0643 [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,
10 User
Skyler Greyf21323a2022-08-13 23:58:22 +010011} from "discord.js";
pineafan813bdf42022-07-24 10:39:10 +010012import EmojiEmbed from "../utils/generateEmojiEmbed.js";
13import getEmojiByName from "../utils/getEmojiByName.js";
14import { PasteClient, Publicity, ExpireDate } from "pastebin-api";
pineafan813bdf42022-07-24 10:39:10 +010015import client from "../utils/client.js";
16
PineaFan752af462022-12-31 21:59:38 +000017const pbClient = new PasteClient(client.config.pastebinApiKey);
pineafan813bdf42022-07-24 10:39:10 +010018
pineafan0f5cc782022-08-12 21:55:42 +010019export default async function (interaction: CommandInteraction | MessageComponentInteraction) {
Skyler Grey11236ba2022-08-08 21:13:33 +010020 if (interaction.channel === null) return;
pineafan4e425942022-08-08 22:01:47 +010021 if (!(interaction.channel instanceof TextChannel)) return;
Skyler Grey11236ba2022-08-08 21:13:33 +010022 const { log, NucleusColors, entry, renderUser, renderDelta } = client.logger;
pineafan813bdf42022-07-24 10:39:10 +010023
Skyler Grey11236ba2022-08-08 21:13:33 +010024 let messages: Message[] = [];
25 let deletedCount: number;
pineafan813bdf42022-07-24 10:39:10 +010026
Skyler Grey11236ba2022-08-08 21:13:33 +010027 do {
28 const fetched = await (interaction.channel as TextChannel).messages.fetch({ limit: 100 });
29 const deleted = await (interaction.channel as TextChannel).bulkDelete(fetched, true);
30 deletedCount = deleted.size;
31 messages = messages.concat(Array.from(deleted.values()));
32 } while (deletedCount === 100);
33
pineafan63fc5e22022-08-04 22:04:10 +010034 let out = "";
Skyler Grey75ea9172022-08-06 10:22:23 +010035 messages.reverse().forEach((message) => {
pineafan813bdf42022-07-24 10:39:10 +010036 if (!message.author.bot) {
pineafan63fc5e22022-08-04 22:04:10 +010037 const sentDate = new Date(message.createdTimestamp);
Skyler Grey11236ba2022-08-08 21:13:33 +010038 out += `${message.author.username}#${message.author.discriminator} (${
39 message.author.id
40 }) [${sentDate.toUTCString()}]\n`;
pineafan63fc5e22022-08-04 22:04:10 +010041 const lines = message.content.split("\n");
Skyler Grey75ea9172022-08-06 10:22:23 +010042 lines.forEach((line) => {
43 out += `> ${line}\n`;
44 });
pineafan63fc5e22022-08-04 22:04:10 +010045 out += "\n\n";
pineafan813bdf42022-07-24 10:39:10 +010046 }
pineafan63fc5e22022-08-04 22:04:10 +010047 });
pineafan3a02ea32022-08-11 21:35:04 +010048 const topic = interaction.channel.topic;
49 let member: GuildMember | null = null;
50 if (topic !== null) {
51 const part = topic.split(" ")[0] ?? null;
52 if (part !== null) member = interaction.guild!.members.cache.get(part) ?? null;
53 }
54 let m: Message;
pineafan813bdf42022-07-24 10:39:10 +010055 if (out !== "") {
56 const url = await pbClient.createPaste({
57 code: out,
58 expireDate: ExpireDate.Never,
pineafan3a02ea32022-08-11 21:35:04 +010059 name:
60 `Ticket Transcript ${
61 member ? "for " + member.user.username + "#" + member.user.discriminator + " " : ""
62 }` + `(Created at ${new Date(interaction.channel.createdTimestamp).toDateString()})`,
pineafan63fc5e22022-08-04 22:04:10 +010063 publicity: Publicity.Unlisted
64 });
pineafan4e425942022-08-08 22:01:47 +010065 const guildConfig = await client.database.guilds.read(interaction.guild!.id);
pineafan3a02ea32022-08-11 21:35:04 +010066 m = (await interaction.reply({
Skyler Grey75ea9172022-08-06 10:22:23 +010067 embeds: [
68 new EmojiEmbed()
69 .setTitle("Transcript")
70 .setDescription(
71 "You can view the transcript using the link below. You can save the link for later" +
72 (guildConfig.logging.logs.channel
73 ? ` or find it in <#${guildConfig.logging.logs.channel}> once you press delete below. After this the channel will be deleted.`
74 : ".")
75 )
76 .setStatus("Success")
77 .setEmoji("CONTROL.DOWNLOAD")
78 ],
79 components: [
PineaFan538d3752023-01-12 21:48:23 +000080 new ActionRowBuilder<ButtonBuilder>().addComponents([
TheCodedProf21c08592022-09-13 14:14:43 -040081 new ButtonBuilder().setLabel("View").setStyle(ButtonStyle.Link).setURL(url),
82 new ButtonBuilder()
Skyler Grey75ea9172022-08-06 10:22:23 +010083 .setLabel("Delete")
TheCodedProf21c08592022-09-13 14:14:43 -040084 .setStyle(ButtonStyle.Danger)
Skyler Grey75ea9172022-08-06 10:22:23 +010085 .setCustomId("close")
86 .setEmoji(getEmojiByName("CONTROL.CROSS", "id"))
87 ])
88 ],
89 fetchReply: true
pineafan3a02ea32022-08-11 21:35:04 +010090 })) as Message;
pineafan813bdf42022-07-24 10:39:10 +010091 } else {
pineafan3a02ea32022-08-11 21:35:04 +010092 m = (await interaction.reply({
Skyler Grey75ea9172022-08-06 10:22:23 +010093 embeds: [
94 new EmojiEmbed()
95 .setTitle("Transcript")
96 .setDescription(
97 "The transcript was empty, so no changes were made. To delete this ticket, press the delete button below."
98 )
99 .setStatus("Success")
100 .setEmoji("CONTROL.DOWNLOAD")
101 ],
102 components: [
PineaFan538d3752023-01-12 21:48:23 +0000103 new ActionRowBuilder<ButtonBuilder>().addComponents([
TheCodedProf21c08592022-09-13 14:14:43 -0400104 new ButtonBuilder()
Skyler Grey75ea9172022-08-06 10:22:23 +0100105 .setLabel("Delete")
TheCodedProf21c08592022-09-13 14:14:43 -0400106 .setStyle(ButtonStyle.Danger)
Skyler Grey75ea9172022-08-06 10:22:23 +0100107 .setCustomId("close")
108 .setEmoji(getEmojiByName("CONTROL.CROSS", "id"))
109 ])
110 ],
111 fetchReply: true
pineafan3a02ea32022-08-11 21:35:04 +0100112 })) as Message;
pineafan813bdf42022-07-24 10:39:10 +0100113 }
114 let i;
115 try {
116 i = await m.awaitMessageComponent({ time: 300000 });
pineafan63fc5e22022-08-04 22:04:10 +0100117 i.deferUpdate();
Skyler Grey75ea9172022-08-06 10:22:23 +0100118 } catch {
119 return;
120 }
pineafan63fc5e22022-08-04 22:04:10 +0100121 const data = {
Skyler Grey75ea9172022-08-06 10:22:23 +0100122 meta: {
pineafan63fc5e22022-08-04 22:04:10 +0100123 type: "ticketDeleted",
124 displayName: "Ticket Deleted",
pineafan813bdf42022-07-24 10:39:10 +0100125 calculateType: "ticketUpdate",
126 color: NucleusColors.red,
pineafan63fc5e22022-08-04 22:04:10 +0100127 emoji: "GUILD.TICKET.CLOSE",
pineafan813bdf42022-07-24 10:39:10 +0100128 timestamp: new Date().getTime()
129 },
130 list: {
pineafan3a02ea32022-08-11 21:35:04 +0100131 ticketFor: member ? entry(member.id, renderUser(member.user)) : entry(null, "*Unknown*"),
PineaFan538d3752023-01-12 21:48:23 +0000132 deletedBy: entry(interaction.member!.user.id, renderUser(interaction.member!.user as User)),
133 deleted: entry(new Date().getTime().toString(), renderDelta(new Date().getTime()))
pineafan813bdf42022-07-24 10:39:10 +0100134 },
135 hidden: {
pineafan4e425942022-08-08 22:01:47 +0100136 guild: interaction.guild!.id
pineafan813bdf42022-07-24 10:39:10 +0100137 }
pineafan63fc5e22022-08-04 22:04:10 +0100138 };
pineafan813bdf42022-07-24 10:39:10 +0100139 log(data);
pineafan63fc5e22022-08-04 22:04:10 +0100140 await interaction.channel.delete();
141 return;
Skyler Grey75ea9172022-08-06 10:22:23 +0100142}