blob: fba3007a2f43a245ebacc22ecddba48bb71bdc03 [file] [log] [blame]
pineafan0f5cc782022-08-12 21:55:42 +01001import type { GuildAuditLogsEntry, ThreadChannel } from "discord.js";
2// @ts-expect-error
pineafan63fc5e22022-08-04 22:04:10 +01003import humanizeDuration from "humanize-duration";
pineafan0f5cc782022-08-12 21:55:42 +01004// @ts-expect-error
Skyler Greyf21323a2022-08-13 23:58:22 +01005import type { HaikuClient } from "jshaiku";
pineafan63fc5e22022-08-04 22:04:10 +01006export const event = "threadDelete";
pineafane625d782022-05-09 18:04:32 +01007
pineafan0f5cc782022-08-12 21:55:42 +01008export async function callback(client: HaikuClient, thread: ThreadChannel) {
9 const { getAuditLog, log, NucleusColors, entry, renderUser, renderDelta, renderChannel } = client.logger;
pineafan63fc5e22022-08-04 22:04:10 +010010 const auditLog = await getAuditLog(thread.guild, "THREAD_UPDATE");
pineafan0f5cc782022-08-12 21:55:42 +010011 const audit = auditLog.entries.filter((entry: GuildAuditLogsEntry) => entry.target!.id === thread.id).first();
pineafane23c4ec2022-07-27 21:56:27 +010012 if (audit.executor.id === client.user.id) return;
Skyler Greyf21323a2022-08-13 23:58:22 +010013 const category = thread.parent
14 ? entry(
15 thread.parent.parent ? thread.parent.parent.name : "None",
16 thread.parent.parent ? renderChannel(thread.parent.parent) : "None"
17 )
18 : entry(null, "Uncategorised");
pineafan63fc5e22022-08-04 22:04:10 +010019 const data = {
pineafane625d782022-05-09 18:04:32 +010020 meta: {
pineafan63fc5e22022-08-04 22:04:10 +010021 type: "channelDelete",
22 displayName: "Thread Deleted",
23 calculateType: "channelUpdate",
pineafane625d782022-05-09 18:04:32 +010024 color: NucleusColors.red,
25 emoji: "CHANNEL.TEXT.DELETE",
26 timestamp: new Date().getTime()
27 },
28 list: {
Skyler Grey75ea9172022-08-06 10:22:23 +010029 threadId: entry(thread.id, `\`${thread.id}\``),
pineafane625d782022-05-09 18:04:32 +010030 name: entry(thread.name, thread.name),
pineafan0f5cc782022-08-12 21:55:42 +010031 parentChannel: entry(thread.parentId, thread.parent ? renderChannel(thread.parent) : "*None*"),
32 category: category,
Skyler Grey75ea9172022-08-06 10:22:23 +010033 autoArchiveDuration: entry(
34 thread.autoArchiveDuration,
pineafan0f5cc782022-08-12 21:55:42 +010035 humanizeDuration((thread.autoArchiveDuration ?? 0) * 60 * 1000, {
Skyler Grey75ea9172022-08-06 10:22:23 +010036 round: true
37 })
38 ),
pineafane625d782022-05-09 18:04:32 +010039 membersInThread: entry(thread.memberCount, thread.memberCount),
40 deletedBy: entry(audit.executor.id, renderUser(audit.executor)),
Skyler Grey11236ba2022-08-08 21:13:33 +010041 created: entry(thread.createdTimestamp, renderDelta(thread.createdTimestamp)),
42 deleted: entry(new Date().getTime(), renderDelta(new Date().getTime()))
pineafane625d782022-05-09 18:04:32 +010043 },
44 hidden: {
45 guild: thread.guild.id
46 }
pineafan63fc5e22022-08-04 22:04:10 +010047 };
pineafan4edb7762022-06-26 19:21:04 +010048 log(data);
pineafane625d782022-05-09 18:04:32 +010049}