blob: 3d7f4c61fc8be80397c0e649c4dac1d68f97e4bf [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";
Skyler Grey75ea9172022-08-06 10:22:23 +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) {
Skyler Grey75ea9172022-08-06 10:22:23 +010011 const { log, NucleusColors, entry, renderUser, renderDelta } =
12 client.logger;
pineafan813bdf42022-07-24 10:39:10 +010013
pineafan63fc5e22022-08-04 22:04:10 +010014 let messages = [];
pineafan813bdf42022-07-24 10:39:10 +010015 let deleted = 100;
16
pineafane23c4ec2022-07-27 21:56:27 +010017 while (deleted === 100) {
pineafan813bdf42022-07-24 10:39:10 +010018 let fetched;
Skyler Grey75ea9172022-08-06 10:22:23 +010019 await (interaction.channel as TextChannel).messages
20 .fetch({ limit: 100 })
21 .then(async (ms) => {
22 fetched = await (interaction.channel as TextChannel).bulkDelete(
23 ms,
24 true
25 );
26 });
pineafan63fc5e22022-08-04 22:04:10 +010027 deleted = fetched.size;
pineafan813bdf42022-07-24 10:39:10 +010028 if (fetched) {
Skyler Grey75ea9172022-08-06 10:22:23 +010029 messages = messages.concat(fetched.map((m) => m));
pineafan813bdf42022-07-24 10:39:10 +010030 }
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 Grey75ea9172022-08-06 10:22:23 +010036 out += `${message.author.username}#${
37 message.author.discriminator
38 } (${message.author.id}) [${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 });
Skyler Grey75ea9172022-08-06 10:22:23 +010046 const member = interaction.channel.guild.members.cache.get(
47 interaction.channel.topic.split(" ")[0]
48 );
pineafan813bdf42022-07-24 10:39:10 +010049 let m;
50 if (out !== "") {
51 const url = await pbClient.createPaste({
52 code: out,
53 expireDate: ExpireDate.Never,
Skyler Grey75ea9172022-08-06 10:22:23 +010054 name: `Ticket Transcript for ${member.user.username}#${
55 member.user.discriminator
56 } (Created at ${new Date(
57 interaction.channel.createdTimestamp
58 ).toDateString()})`,
pineafan63fc5e22022-08-04 22:04:10 +010059 publicity: Publicity.Unlisted
60 });
Skyler Grey75ea9172022-08-06 10:22:23 +010061 const guildConfig = await client.database.guilds.read(
62 interaction.guild.id
63 );
64 m = await interaction.reply({
65 embeds: [
66 new EmojiEmbed()
67 .setTitle("Transcript")
68 .setDescription(
69 "You can view the transcript using the link below. You can save the link for later" +
70 (guildConfig.logging.logs.channel
71 ? ` or find it in <#${guildConfig.logging.logs.channel}> once you press delete below. After this the channel will be deleted.`
72 : ".")
73 )
74 .setStatus("Success")
75 .setEmoji("CONTROL.DOWNLOAD")
76 ],
77 components: [
78 new MessageActionRow().addComponents([
79 new MessageButton()
80 .setLabel("View")
81 .setStyle("LINK")
82 .setURL(url),
83 new MessageButton()
84 .setLabel("Delete")
85 .setStyle("DANGER")
86 .setCustomId("close")
87 .setEmoji(getEmojiByName("CONTROL.CROSS", "id"))
88 ])
89 ],
90 fetchReply: true
91 });
pineafan813bdf42022-07-24 10:39:10 +010092 } else {
Skyler Grey75ea9172022-08-06 10:22:23 +010093 m = await interaction.reply({
94 embeds: [
95 new EmojiEmbed()
96 .setTitle("Transcript")
97 .setDescription(
98 "The transcript was empty, so no changes were made. To delete this ticket, press the delete button below."
99 )
100 .setStatus("Success")
101 .setEmoji("CONTROL.DOWNLOAD")
102 ],
103 components: [
104 new MessageActionRow().addComponents([
105 new MessageButton()
106 .setLabel("Delete")
107 .setStyle("DANGER")
108 .setCustomId("close")
109 .setEmoji(getEmojiByName("CONTROL.CROSS", "id"))
110 ])
111 ],
112 fetchReply: true
113 });
pineafan813bdf42022-07-24 10:39:10 +0100114 }
115 let i;
116 try {
117 i = await m.awaitMessageComponent({ time: 300000 });
pineafan63fc5e22022-08-04 22:04:10 +0100118 i.deferUpdate();
Skyler Grey75ea9172022-08-06 10:22:23 +0100119 } catch {
120 return;
121 }
pineafan63fc5e22022-08-04 22:04:10 +0100122 const data = {
Skyler Grey75ea9172022-08-06 10:22:23 +0100123 meta: {
pineafan63fc5e22022-08-04 22:04:10 +0100124 type: "ticketDeleted",
125 displayName: "Ticket Deleted",
pineafan813bdf42022-07-24 10:39:10 +0100126 calculateType: "ticketUpdate",
127 color: NucleusColors.red,
pineafan63fc5e22022-08-04 22:04:10 +0100128 emoji: "GUILD.TICKET.CLOSE",
pineafan813bdf42022-07-24 10:39:10 +0100129 timestamp: new Date().getTime()
130 },
131 list: {
Skyler Grey75ea9172022-08-06 10:22:23 +0100132 ticketFor: entry(
133 interaction.channel.topic.split(" ")[0],
134 renderUser(
135 (
136 await interaction.guild.members.fetch(
137 interaction.channel.topic.split(" ")[0]
138 )
139 ).user
140 )
141 ),
142 deletedBy: entry(
143 interaction.member.user.id,
144 renderUser(interaction.member.user)
145 ),
146 deleted: entry(
147 new Date().getTime(),
148 renderDelta(new Date().getTime())
149 )
pineafan813bdf42022-07-24 10:39:10 +0100150 },
151 hidden: {
152 guild: interaction.guild.id
153 }
pineafan63fc5e22022-08-04 22:04:10 +0100154 };
pineafan813bdf42022-07-24 10:39:10 +0100155 log(data);
pineafan63fc5e22022-08-04 22:04:10 +0100156 await interaction.channel.delete();
157 return;
Skyler Grey75ea9172022-08-06 10:22:23 +0100158}