blob: 510ec306cf9ea8ffe91579a5f3fc7917448c300d [file] [log] [blame]
TheCodedProfa16d1672023-01-18 18:58:34 -05001import { AuditLogEvent, GuildAuditLogsEntry, ThreadChannel } from "discord.js";
pineafan3a02ea32022-08-11 21:35:04 +01002// @ts-expect-error
pineafan63fc5e22022-08-04 22:04:10 +01003import humanizeDuration from "humanize-duration";
PineaFan752af462022-12-31 21:59:38 +00004import type { NucleusClient } from "../utils/client.js";
pineafan3a02ea32022-08-11 21:35:04 +01005
pineafan63fc5e22022-08-04 22:04:10 +01006export const event = "threadUpdate";
pineafane625d782022-05-09 18:04:32 +01007
TheCodedProfa16d1672023-01-18 18:58:34 -05008export async function callback(client: NucleusClient, oldThread: ThreadChannel, newThread: ThreadChannel) {
TheCodedProf6ec331b2023-02-20 12:13:06 -05009 const { getAuditLog, isLogging, log, NucleusColors, entry, renderUser, renderDelta, renderChannel } = client.logger;
Skyler Greyda16adf2023-03-05 10:22:12 +000010 if (!(await isLogging(newThread.guild.id, "channelUpdate"))) return;
11 const auditLog = (await getAuditLog(newThread.guild, AuditLogEvent.ThreadUpdate)).filter(
12 (entry: GuildAuditLogsEntry) => (entry.target as ThreadChannel)!.id === newThread.id
13 )[0]!;
TheCodedProfa16d1672023-01-18 18:58:34 -050014 if (auditLog.executor!.id === client.user!.id) return;
pineafan3a02ea32022-08-11 21:35:04 +010015 const list: Record<string, ReturnType<typeof entry>> = {
TheCodedProfa16d1672023-01-18 18:58:34 -050016 threadId: entry(newThread.id, `\`${newThread.id}\``),
17 thread: entry(newThread.name, renderChannel(newThread)),
18 parentChannel: entry(newThread.parentId, renderChannel(newThread.parent!))
pineafan63fc5e22022-08-04 22:04:10 +010019 };
TheCodedProfa16d1672023-01-18 18:58:34 -050020 if (oldThread.name !== newThread.name) {
21 list["name"] = entry([oldThread.name, newThread.name], `${oldThread.name} -> ${newThread.name}`);
pineafan63fc5e22022-08-04 22:04:10 +010022 }
TheCodedProfa16d1672023-01-18 18:58:34 -050023 if (oldThread.autoArchiveDuration !== newThread.autoArchiveDuration) {
pineafan3a02ea32022-08-11 21:35:04 +010024 list["autoArchiveDuration"] = entry(
TheCodedProfa16d1672023-01-18 18:58:34 -050025 [oldThread.autoArchiveDuration!.toString(), newThread.autoArchiveDuration!.toString()],
26 `${humanizeDuration((oldThread.autoArchiveDuration ?? 0) * 60 * 1000, {
Skyler Grey75ea9172022-08-06 10:22:23 +010027 round: true
TheCodedProfa16d1672023-01-18 18:58:34 -050028 })} -> ${humanizeDuration((newThread.autoArchiveDuration ?? 0) * 60 * 1000, {
Skyler Grey75ea9172022-08-06 10:22:23 +010029 round: true
30 })}`
31 );
pineafan63fc5e22022-08-04 22:04:10 +010032 }
TheCodedProfa16d1672023-01-18 18:58:34 -050033 if (oldThread.rateLimitPerUser !== newThread.rateLimitPerUser) {
pineafan3a02ea32022-08-11 21:35:04 +010034 list["slowmode"] = entry(
TheCodedProfa16d1672023-01-18 18:58:34 -050035 [oldThread.rateLimitPerUser!.toString(), newThread.rateLimitPerUser!.toString()],
36 `${humanizeDuration((oldThread.rateLimitPerUser ?? 0) * 1000)} -> ${humanizeDuration(
37 (newThread.rateLimitPerUser ?? 0) * 1000
Skyler Greyf21323a2022-08-13 23:58:22 +010038 )}`
Skyler Grey75ea9172022-08-06 10:22:23 +010039 );
pineafan63fc5e22022-08-04 22:04:10 +010040 }
41 if (!(Object.keys(list).length - 3)) return;
TheCodedProf6ec331b2023-02-20 12:13:06 -050042 list["updated"] = entry(Date.now(), renderDelta(Date.now()));
TheCodedProfa16d1672023-01-18 18:58:34 -050043 list["updatedBy"] = entry(auditLog.executor!.id, renderUser(auditLog.executor!));
pineafan63fc5e22022-08-04 22:04:10 +010044 const data = {
45 meta: {
46 type: "channelUpdate",
47 displayName: "Thread Edited",
48 calculateType: "channelUpdate",
49 color: NucleusColors.yellow,
50 emoji: "CHANNEL.TEXT.EDIT",
TheCodedProf6ec331b2023-02-20 12:13:06 -050051 timestamp: Date.now()
pineafan63fc5e22022-08-04 22:04:10 +010052 },
53 list: list,
54 hidden: {
TheCodedProfa16d1672023-01-18 18:58:34 -050055 guild: newThread.guild.id
pineafane625d782022-05-09 18:04:32 +010056 }
pineafan63fc5e22022-08-04 22:04:10 +010057 };
58 log(data);
pineafane625d782022-05-09 18:04:32 +010059}