blob: 92b268c5822e87de1acae1204face422d719b462 [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";
5import config from '../config/main.json' assert {type: 'json'};
6import client from "../utils/client.js";
7
8const pbClient = new PasteClient(config.pastebinApiKey)
9
10export default async function (interaction) {
11 const { log, NucleusColors, entry, renderUser, renderChannel, renderDelta } = client.logger
12
13 let messages = []
14 let deleted = 100;
15
16 while (deleted == 100) {
17 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);
20 })
21 deleted = fetched.size
22 if (fetched) {
23 messages = messages.concat(fetched.map(m => m))
24 }
25 }
26 let out = ""
27 messages.reverse().forEach(message => {
28 if (!message.author.bot) {
29 let sentDate = new Date(message.createdTimestamp)
30 out += `${message.author.username}#${message.author.discriminator} (${message.author.id}) [${sentDate.toUTCString()}]\n`
31 let lines = message.content.split("\n")
32 lines.forEach(line => {out += `> ${line}\n`})
33 out += `\n\n`
34 }
35 })
36 let member = interaction.channel.guild.members.cache.get(interaction.channel.topic.split(" ")[0])
37 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()})`,
43 publicity: Publicity.Unlisted,
44 })
45 let guildConfig = await client.database.guilds.read(interaction.guild.id);
46 m = await interaction.reply({embeds: [new EmojiEmbed()
47 .setTitle("Transcript")
48 .setDescription(`You can view the transcript using the link below. You can save the link for later` + (guildConfig.logging.logs.channel ?
49 ` 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")
67 .setDescription(`The transcript was empty, so no changes were made. To delete this ticket, press the delete button below.`)
68 .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 });
81 i.deferUpdate()
82 } catch (e) { }
83 let data = {
84 meta:{
85 type: 'ticketDeleted',
86 displayName: 'Ticket Deleted',
87 calculateType: "ticketUpdate",
88 color: NucleusColors.red,
89 emoji: 'GUILD.TICKET.CLOSE',
90 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 }
100 }
101 log(data);
102 await interaction.channel.delete()
103 return
104}