blob: 43de7672cef28f731e35338b8add7e7b17675495 [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,
TheCodedProf2e54a772023-02-14 16:26:47 -050010 User,
11 ComponentType
Skyler Greyf21323a2022-08-13 23:58:22 +010012} from "discord.js";
pineafan813bdf42022-07-24 10:39:10 +010013import EmojiEmbed from "../utils/generateEmojiEmbed.js";
14import getEmojiByName from "../utils/getEmojiByName.js";
15import { PasteClient, Publicity, ExpireDate } from "pastebin-api";
pineafan813bdf42022-07-24 10:39:10 +010016import client from "../utils/client.js";
17
PineaFan752af462022-12-31 21:59:38 +000018const pbClient = new PasteClient(client.config.pastebinApiKey);
pineafan813bdf42022-07-24 10:39:10 +010019
TheCodedProf2e54a772023-02-14 16:26:47 -050020interface TranscriptEmbed {
21 title?: string;
22 description?: string;
23 fields?: {
24 name: string;
25 value: string;
26 inline: boolean;
27 }[];
28 footer?: {
29 text: string;
30 iconURL?: string;
31 };
32}
33
34interface TranscriptComponent {
35 type: number;
36 style?: ButtonStyle;
37 label?: string;
38 description?: string;
39 placeholder?: string;
40 emojiURL?: string;
41}
42
43interface TranscriptAuthor {
44 username: string;
45 discriminator: number;
46 nickname?: string;
47 id: string;
48 iconURL?: string;
49 topRole: {
50 color: number;
51 badgeURL?: string;
52 }
53}
54
55interface TranscriptMessage {
56 id: string;
57 author: TranscriptAuthor;
58 content?: string;
59 embeds?: TranscriptEmbed[];
60 components?: TranscriptComponent[][];
61 editedTimestamp?: number;
62 createdTimestamp: number;
63 flags?: string[];
64 attachments?: unknown; //FIXME
65 stickerURLs?: string[];
66 referencedMessage?: string | [string, string, string];
67}
68
69interface Transcript {
70 type: "ticket" | "purge"
71 guild: string;
72 channel: string;
73 messages: TranscriptMessage[];
74 createdTimestamp: number;
75 createdBy: TranscriptAuthor;
76}
77
pineafan0f5cc782022-08-12 21:55:42 +010078export default async function (interaction: CommandInteraction | MessageComponentInteraction) {
Skyler Grey11236ba2022-08-08 21:13:33 +010079 if (interaction.channel === null) return;
pineafan4e425942022-08-08 22:01:47 +010080 if (!(interaction.channel instanceof TextChannel)) return;
Skyler Grey11236ba2022-08-08 21:13:33 +010081 const { log, NucleusColors, entry, renderUser, renderDelta } = client.logger;
pineafan813bdf42022-07-24 10:39:10 +010082
Skyler Grey11236ba2022-08-08 21:13:33 +010083 let messages: Message[] = [];
84 let deletedCount: number;
pineafan813bdf42022-07-24 10:39:10 +010085
Skyler Grey11236ba2022-08-08 21:13:33 +010086 do {
87 const fetched = await (interaction.channel as TextChannel).messages.fetch({ limit: 100 });
88 const deleted = await (interaction.channel as TextChannel).bulkDelete(fetched, true);
89 deletedCount = deleted.size;
TheCodedProfbc94a5c2023-01-18 18:59:31 -050090 messages = messages.concat(Array.from(deleted.values() as Iterable<Message>));
Skyler Grey11236ba2022-08-08 21:13:33 +010091 } while (deletedCount === 100);
92
pineafan63fc5e22022-08-04 22:04:10 +010093 let out = "";
Skyler Grey75ea9172022-08-06 10:22:23 +010094 messages.reverse().forEach((message) => {
pineafan813bdf42022-07-24 10:39:10 +010095 if (!message.author.bot) {
pineafan63fc5e22022-08-04 22:04:10 +010096 const sentDate = new Date(message.createdTimestamp);
Skyler Grey11236ba2022-08-08 21:13:33 +010097 out += `${message.author.username}#${message.author.discriminator} (${
98 message.author.id
99 }) [${sentDate.toUTCString()}]\n`;
pineafan63fc5e22022-08-04 22:04:10 +0100100 const lines = message.content.split("\n");
Skyler Grey75ea9172022-08-06 10:22:23 +0100101 lines.forEach((line) => {
102 out += `> ${line}\n`;
103 });
pineafan63fc5e22022-08-04 22:04:10 +0100104 out += "\n\n";
pineafan813bdf42022-07-24 10:39:10 +0100105 }
pineafan63fc5e22022-08-04 22:04:10 +0100106 });
TheCodedProf2e54a772023-02-14 16:26:47 -0500107
108 let interactionMember = await interaction.guild?.members.fetch(interaction.user.id)
109
110 let newOut: Transcript = {
111 type: "ticket",
112 guild: interaction.guild!.id,
113 channel: interaction.channel!.id,
114 messages: [],
115 createdTimestamp: Date.now(),
116 createdBy: {
117 username: interaction.user.username,
118 discriminator: parseInt(interaction.user.discriminator),
119 id: interaction.user.id,
120 topRole: {
121 color: interactionMember?.roles.highest.color ?? 0x000000
122 }
123 }
124 }
125 if(interactionMember?.roles.icon) newOut.createdBy.topRole.badgeURL = interactionMember.roles.icon.iconURL()!;
126 messages.reverse().forEach((message) => {
127 let msg: TranscriptMessage = {
128 id: message.id,
129 author: {
130 username: message.author.username,
131 discriminator: parseInt(message.author.discriminator),
132 id: message.author.id,
133 topRole: {
134 color: message.member!.roles.highest.color
135 }
136 },
137 createdTimestamp: message.createdTimestamp
138 };
139 if (message.member!.roles.icon) msg.author.topRole.badgeURL = message.member!.roles.icon.iconURL()!;
140 if (message.content) msg.content = message.content;
141 if (message.embeds.length > 0) msg.embeds = message.embeds.map(embed => {
142 let obj: TranscriptEmbed = {};
143 if (embed.title) obj.title = embed.title;
144 if (embed.description) obj.description = embed.description;
145 if (embed.fields.length > 0) obj.fields = embed.fields.map(field => {
146 return {
147 name: field.name,
148 value: field.value,
149 inline: field.inline ?? false
150 }
151 });
152 if (embed.footer) obj.footer = {
153 text: embed.footer.text,
154 };
155 if (embed.footer?.iconURL) obj.footer!.iconURL = embed.footer.iconURL;
156 return obj;
157 });
158 if (message.components.length > 0) msg.components = message.components.map(component => component.components.map(child => {
159 let obj: TranscriptComponent = {
160 type: child.type
161 }
162 if (child.type === ComponentType.Button) {
163 obj.style = child.style;
164 obj.label = child.label ?? "";
165 }
166 return obj
167 }));
168 if (message.editedTimestamp) msg.editedTimestamp = message.editedTimestamp;
169 if (message.flags) msg.flags = message.flags.toArray();
170
171 if (message.stickers.size > 0) msg.stickerURLs = message.stickers.map(sticker => sticker.url);
172 if (message.reference) msg.referencedMessage = [message.reference.guildId ?? "", message.reference.channelId, message.reference.messageId ?? ""];
173
174 });
175
pineafan3a02ea32022-08-11 21:35:04 +0100176 const topic = interaction.channel.topic;
177 let member: GuildMember | null = null;
178 if (topic !== null) {
179 const part = topic.split(" ")[0] ?? null;
180 if (part !== null) member = interaction.guild!.members.cache.get(part) ?? null;
181 }
182 let m: Message;
pineafan813bdf42022-07-24 10:39:10 +0100183 if (out !== "") {
184 const url = await pbClient.createPaste({
185 code: out,
186 expireDate: ExpireDate.Never,
pineafan3a02ea32022-08-11 21:35:04 +0100187 name:
188 `Ticket Transcript ${
189 member ? "for " + member.user.username + "#" + member.user.discriminator + " " : ""
190 }` + `(Created at ${new Date(interaction.channel.createdTimestamp).toDateString()})`,
pineafan63fc5e22022-08-04 22:04:10 +0100191 publicity: Publicity.Unlisted
192 });
pineafan4e425942022-08-08 22:01:47 +0100193 const guildConfig = await client.database.guilds.read(interaction.guild!.id);
pineafan3a02ea32022-08-11 21:35:04 +0100194 m = (await interaction.reply({
Skyler Grey75ea9172022-08-06 10:22:23 +0100195 embeds: [
196 new EmojiEmbed()
197 .setTitle("Transcript")
198 .setDescription(
199 "You can view the transcript using the link below. You can save the link for later" +
200 (guildConfig.logging.logs.channel
201 ? ` or find it in <#${guildConfig.logging.logs.channel}> once you press delete below. After this the channel will be deleted.`
202 : ".")
203 )
204 .setStatus("Success")
205 .setEmoji("CONTROL.DOWNLOAD")
206 ],
207 components: [
PineaFan538d3752023-01-12 21:48:23 +0000208 new ActionRowBuilder<ButtonBuilder>().addComponents([
TheCodedProf21c08592022-09-13 14:14:43 -0400209 new ButtonBuilder().setLabel("View").setStyle(ButtonStyle.Link).setURL(url),
210 new ButtonBuilder()
Skyler Grey75ea9172022-08-06 10:22:23 +0100211 .setLabel("Delete")
TheCodedProf21c08592022-09-13 14:14:43 -0400212 .setStyle(ButtonStyle.Danger)
Skyler Grey75ea9172022-08-06 10:22:23 +0100213 .setCustomId("close")
214 .setEmoji(getEmojiByName("CONTROL.CROSS", "id"))
215 ])
216 ],
217 fetchReply: true
pineafan3a02ea32022-08-11 21:35:04 +0100218 })) as Message;
pineafan813bdf42022-07-24 10:39:10 +0100219 } else {
pineafan3a02ea32022-08-11 21:35:04 +0100220 m = (await interaction.reply({
Skyler Grey75ea9172022-08-06 10:22:23 +0100221 embeds: [
222 new EmojiEmbed()
223 .setTitle("Transcript")
224 .setDescription(
225 "The transcript was empty, so no changes were made. To delete this ticket, press the delete button below."
226 )
227 .setStatus("Success")
228 .setEmoji("CONTROL.DOWNLOAD")
229 ],
230 components: [
PineaFan538d3752023-01-12 21:48:23 +0000231 new ActionRowBuilder<ButtonBuilder>().addComponents([
TheCodedProf21c08592022-09-13 14:14:43 -0400232 new ButtonBuilder()
Skyler Grey75ea9172022-08-06 10:22:23 +0100233 .setLabel("Delete")
TheCodedProf21c08592022-09-13 14:14:43 -0400234 .setStyle(ButtonStyle.Danger)
Skyler Grey75ea9172022-08-06 10:22:23 +0100235 .setCustomId("close")
236 .setEmoji(getEmojiByName("CONTROL.CROSS", "id"))
237 ])
238 ],
239 fetchReply: true
pineafan3a02ea32022-08-11 21:35:04 +0100240 })) as Message;
pineafan813bdf42022-07-24 10:39:10 +0100241 }
242 let i;
243 try {
PineaFan0d06edc2023-01-17 22:10:31 +0000244 i = await m.awaitMessageComponent({
245 time: 300000,
TheCodedProf267563a2023-01-21 17:00:57 -0500246 filter: (i) => { return i.user.id === interaction.user.id && i.channel!.id === interaction.channel!.id && i.message.id === m.id }
PineaFan0d06edc2023-01-17 22:10:31 +0000247 });
TheCodedProf267563a2023-01-21 17:00:57 -0500248 await i.deferUpdate();
Skyler Grey75ea9172022-08-06 10:22:23 +0100249 } catch {
250 return;
251 }
pineafan63fc5e22022-08-04 22:04:10 +0100252 const data = {
Skyler Grey75ea9172022-08-06 10:22:23 +0100253 meta: {
pineafan63fc5e22022-08-04 22:04:10 +0100254 type: "ticketDeleted",
255 displayName: "Ticket Deleted",
pineafan813bdf42022-07-24 10:39:10 +0100256 calculateType: "ticketUpdate",
257 color: NucleusColors.red,
pineafan63fc5e22022-08-04 22:04:10 +0100258 emoji: "GUILD.TICKET.CLOSE",
pineafan813bdf42022-07-24 10:39:10 +0100259 timestamp: new Date().getTime()
260 },
261 list: {
pineafan3a02ea32022-08-11 21:35:04 +0100262 ticketFor: member ? entry(member.id, renderUser(member.user)) : entry(null, "*Unknown*"),
PineaFan538d3752023-01-12 21:48:23 +0000263 deletedBy: entry(interaction.member!.user.id, renderUser(interaction.member!.user as User)),
264 deleted: entry(new Date().getTime().toString(), renderDelta(new Date().getTime()))
pineafan813bdf42022-07-24 10:39:10 +0100265 },
266 hidden: {
pineafan4e425942022-08-08 22:01:47 +0100267 guild: interaction.guild!.id
pineafan813bdf42022-07-24 10:39:10 +0100268 }
pineafan63fc5e22022-08-04 22:04:10 +0100269 };
pineafan813bdf42022-07-24 10:39:10 +0100270 log(data);
pineafan63fc5e22022-08-04 22:04:10 +0100271 await interaction.channel.delete();
272 return;
Skyler Grey75ea9172022-08-06 10:22:23 +0100273}