blob: bcf73fac10ffdf2a23b1a53d2ce9e081e8d86999 [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) {
pineafan3a02ea32022-08-11 21:35:04 +01009 const { getAuditLog, log, NucleusColors, entry, renderUser, renderDelta, renderChannel } = client.logger;
TheCodedProfa16d1672023-01-18 18:58:34 -050010 const auditLog = (await getAuditLog(newThread.guild, AuditLogEvent.ThreadUpdate))
11 .filter((entry: GuildAuditLogsEntry) => (entry.target as ThreadChannel)!.id === newThread.id)[0] as GuildAuditLogsEntry;
12 if (auditLog.executor!.id === client.user!.id) return;
pineafan3a02ea32022-08-11 21:35:04 +010013 const list: Record<string, ReturnType<typeof entry>> = {
TheCodedProfa16d1672023-01-18 18:58:34 -050014 threadId: entry(newThread.id, `\`${newThread.id}\``),
15 thread: entry(newThread.name, renderChannel(newThread)),
16 parentChannel: entry(newThread.parentId, renderChannel(newThread.parent!))
pineafan63fc5e22022-08-04 22:04:10 +010017 };
TheCodedProfa16d1672023-01-18 18:58:34 -050018 if (oldThread.name !== newThread.name) {
19 list["name"] = entry([oldThread.name, newThread.name], `${oldThread.name} -> ${newThread.name}`);
pineafan63fc5e22022-08-04 22:04:10 +010020 }
TheCodedProfa16d1672023-01-18 18:58:34 -050021 if (oldThread.autoArchiveDuration !== newThread.autoArchiveDuration) {
pineafan3a02ea32022-08-11 21:35:04 +010022 list["autoArchiveDuration"] = entry(
TheCodedProfa16d1672023-01-18 18:58:34 -050023 [oldThread.autoArchiveDuration!.toString(), newThread.autoArchiveDuration!.toString()],
24 `${humanizeDuration((oldThread.autoArchiveDuration ?? 0) * 60 * 1000, {
Skyler Grey75ea9172022-08-06 10:22:23 +010025 round: true
TheCodedProfa16d1672023-01-18 18:58:34 -050026 })} -> ${humanizeDuration((newThread.autoArchiveDuration ?? 0) * 60 * 1000, {
Skyler Grey75ea9172022-08-06 10:22:23 +010027 round: true
28 })}`
29 );
pineafan63fc5e22022-08-04 22:04:10 +010030 }
TheCodedProfa16d1672023-01-18 18:58:34 -050031 if (oldThread.rateLimitPerUser !== newThread.rateLimitPerUser) {
pineafan3a02ea32022-08-11 21:35:04 +010032 list["slowmode"] = entry(
TheCodedProfa16d1672023-01-18 18:58:34 -050033 [oldThread.rateLimitPerUser!.toString(), newThread.rateLimitPerUser!.toString()],
34 `${humanizeDuration((oldThread.rateLimitPerUser ?? 0) * 1000)} -> ${humanizeDuration(
35 (newThread.rateLimitPerUser ?? 0) * 1000
Skyler Greyf21323a2022-08-13 23:58:22 +010036 )}`
Skyler Grey75ea9172022-08-06 10:22:23 +010037 );
pineafan63fc5e22022-08-04 22:04:10 +010038 }
39 if (!(Object.keys(list).length - 3)) return;
pineafan3a02ea32022-08-11 21:35:04 +010040 list["updated"] = entry(new Date().getTime(), renderDelta(new Date().getTime()));
TheCodedProfa16d1672023-01-18 18:58:34 -050041 list["updatedBy"] = entry(auditLog.executor!.id, renderUser(auditLog.executor!));
pineafan63fc5e22022-08-04 22:04:10 +010042 const data = {
43 meta: {
44 type: "channelUpdate",
45 displayName: "Thread Edited",
46 calculateType: "channelUpdate",
47 color: NucleusColors.yellow,
48 emoji: "CHANNEL.TEXT.EDIT",
49 timestamp: new Date().getTime()
50 },
51 list: list,
52 hidden: {
TheCodedProfa16d1672023-01-18 18:58:34 -050053 guild: newThread.guild.id
pineafane625d782022-05-09 18:04:32 +010054 }
pineafan63fc5e22022-08-04 22:04:10 +010055 };
56 log(data);
pineafane625d782022-05-09 18:04:32 +010057}