blob: d5d7bb8f29281fd40dea6af2ea9f9fb3c85d3bd2 [file] [log] [blame]
pineafan813bdf42022-07-24 10:39:10 +01001import { MessageActionRow, MessageButton, TextChannel } from "discord.js";
2import EmojiEmbed from "../utils/generateEmojiEmbed.js";
3import getEmojiByName from "../utils/getEmojiByName.js";
4import { PasteClient, Publicity, ExpireDate } from "pastebin-api";
pineafan63fc5e22022-08-04 22:04:10 +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
10export default async function (interaction) {
pineafan63fc5e22022-08-04 22:04:10 +010011 const { log, NucleusColors, entry, renderUser, renderDelta } = client.logger;
pineafan813bdf42022-07-24 10:39:10 +010012
pineafan63fc5e22022-08-04 22:04:10 +010013 let messages = [];
pineafan813bdf42022-07-24 10:39:10 +010014 let deleted = 100;
15
pineafane23c4ec2022-07-27 21:56:27 +010016 while (deleted === 100) {
pineafan813bdf42022-07-24 10:39:10 +010017 let fetched;
18 await (interaction.channel as TextChannel).messages.fetch({limit: 100}).then(async (ms) => {
19 fetched = await (interaction.channel as TextChannel).bulkDelete(ms, true);
pineafan63fc5e22022-08-04 22:04:10 +010020 });
21 deleted = fetched.size;
pineafan813bdf42022-07-24 10:39:10 +010022 if (fetched) {
pineafan63fc5e22022-08-04 22:04:10 +010023 messages = messages.concat(fetched.map(m => m));
pineafan813bdf42022-07-24 10:39:10 +010024 }
25 }
pineafan63fc5e22022-08-04 22:04:10 +010026 let out = "";
pineafan813bdf42022-07-24 10:39:10 +010027 messages.reverse().forEach(message => {
28 if (!message.author.bot) {
pineafan63fc5e22022-08-04 22:04:10 +010029 const sentDate = new Date(message.createdTimestamp);
30 out += `${message.author.username}#${message.author.discriminator} (${message.author.id}) [${sentDate.toUTCString()}]\n`;
31 const lines = message.content.split("\n");
32 lines.forEach(line => {out += `> ${line}\n`;});
33 out += "\n\n";
pineafan813bdf42022-07-24 10:39:10 +010034 }
pineafan63fc5e22022-08-04 22:04:10 +010035 });
36 const member = interaction.channel.guild.members.cache.get(interaction.channel.topic.split(" ")[0]);
pineafan813bdf42022-07-24 10:39:10 +010037 let m;
38 if (out !== "") {
39 const url = await pbClient.createPaste({
40 code: out,
41 expireDate: ExpireDate.Never,
42 name: `Ticket Transcript for ${member.user.username}#${member.user.discriminator} (Created at ${new Date(interaction.channel.createdTimestamp).toDateString()})`,
pineafan63fc5e22022-08-04 22:04:10 +010043 publicity: Publicity.Unlisted
44 });
45 const guildConfig = await client.database.guilds.read(interaction.guild.id);
pineafan813bdf42022-07-24 10:39:10 +010046 m = await interaction.reply({embeds: [new EmojiEmbed()
47 .setTitle("Transcript")
pineafan63fc5e22022-08-04 22:04:10 +010048 .setDescription("You can view the transcript using the link below. You can save the link for later" + (guildConfig.logging.logs.channel ?
pineafan813bdf42022-07-24 10:39:10 +010049 ` or find it in <#${guildConfig.logging.logs.channel}> once you press delete below. After this the channel will be deleted.`
50 : "."))
51 .setStatus("Success")
52 .setEmoji("CONTROL.DOWNLOAD")
53 ], components: [new MessageActionRow().addComponents([
54 new MessageButton()
55 .setLabel("View")
56 .setStyle("LINK")
57 .setURL(url),
58 new MessageButton()
59 .setLabel("Delete")
60 .setStyle("DANGER")
61 .setCustomId("close")
62 .setEmoji(getEmojiByName("CONTROL.CROSS", "id"))
63 ])], fetchReply: true});
64 } else {
65 m = await interaction.reply({embeds: [new EmojiEmbed()
66 .setTitle("Transcript")
pineafan63fc5e22022-08-04 22:04:10 +010067 .setDescription("The transcript was empty, so no changes were made. To delete this ticket, press the delete button below.")
pineafan813bdf42022-07-24 10:39:10 +010068 .setStatus("Success")
69 .setEmoji("CONTROL.DOWNLOAD")
70 ], components: [new MessageActionRow().addComponents([
71 new MessageButton()
72 .setLabel("Delete")
73 .setStyle("DANGER")
74 .setCustomId("close")
75 .setEmoji(getEmojiByName("CONTROL.CROSS", "id"))
76 ])], fetchReply: true});
77 }
78 let i;
79 try {
80 i = await m.awaitMessageComponent({ time: 300000 });
pineafan63fc5e22022-08-04 22:04:10 +010081 i.deferUpdate();
82 } catch { return; }
83 const data = {
pineafan813bdf42022-07-24 10:39:10 +010084 meta:{
pineafan63fc5e22022-08-04 22:04:10 +010085 type: "ticketDeleted",
86 displayName: "Ticket Deleted",
pineafan813bdf42022-07-24 10:39:10 +010087 calculateType: "ticketUpdate",
88 color: NucleusColors.red,
pineafan63fc5e22022-08-04 22:04:10 +010089 emoji: "GUILD.TICKET.CLOSE",
pineafan813bdf42022-07-24 10:39:10 +010090 timestamp: new Date().getTime()
91 },
92 list: {
93 ticketFor: entry(interaction.channel.topic.split(" ")[0], renderUser((await interaction.guild.members.fetch(interaction.channel.topic.split(" ")[0])).user)),
94 deletedBy: entry(interaction.member.user.id, renderUser(interaction.member.user)),
95 deleted: entry(new Date().getTime(), renderDelta(new Date().getTime()))
96 },
97 hidden: {
98 guild: interaction.guild.id
99 }
pineafan63fc5e22022-08-04 22:04:10 +0100100 };
pineafan813bdf42022-07-24 10:39:10 +0100101 log(data);
pineafan63fc5e22022-08-04 22:04:10 +0100102 await interaction.channel.delete();
103 return;
pineafan813bdf42022-07-24 10:39:10 +0100104}