blob: af792bc106dec7e9a498a4b59febfb5fbe2ea110 [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;
10 if (!await isLogging(newThread.guild.id, "channelUpdate")) return;
TheCodedProfa16d1672023-01-18 18:58:34 -050011 const auditLog = (await getAuditLog(newThread.guild, AuditLogEvent.ThreadUpdate))
PineaFan638eb132023-01-19 10:41:22 +000012 .filter((entry: GuildAuditLogsEntry) => (entry.target as ThreadChannel)!.id === newThread.id)[0]!;
TheCodedProfa16d1672023-01-18 18:58:34 -050013 if (auditLog.executor!.id === client.user!.id) return;
pineafan3a02ea32022-08-11 21:35:04 +010014 const list: Record<string, ReturnType<typeof entry>> = {
TheCodedProfa16d1672023-01-18 18:58:34 -050015 threadId: entry(newThread.id, `\`${newThread.id}\``),
16 thread: entry(newThread.name, renderChannel(newThread)),
17 parentChannel: entry(newThread.parentId, renderChannel(newThread.parent!))
pineafan63fc5e22022-08-04 22:04:10 +010018 };
TheCodedProfa16d1672023-01-18 18:58:34 -050019 if (oldThread.name !== newThread.name) {
20 list["name"] = entry([oldThread.name, newThread.name], `${oldThread.name} -> ${newThread.name}`);
pineafan63fc5e22022-08-04 22:04:10 +010021 }
TheCodedProfa16d1672023-01-18 18:58:34 -050022 if (oldThread.autoArchiveDuration !== newThread.autoArchiveDuration) {
pineafan3a02ea32022-08-11 21:35:04 +010023 list["autoArchiveDuration"] = entry(
TheCodedProfa16d1672023-01-18 18:58:34 -050024 [oldThread.autoArchiveDuration!.toString(), newThread.autoArchiveDuration!.toString()],
25 `${humanizeDuration((oldThread.autoArchiveDuration ?? 0) * 60 * 1000, {
Skyler Grey75ea9172022-08-06 10:22:23 +010026 round: true
TheCodedProfa16d1672023-01-18 18:58:34 -050027 })} -> ${humanizeDuration((newThread.autoArchiveDuration ?? 0) * 60 * 1000, {
Skyler Grey75ea9172022-08-06 10:22:23 +010028 round: true
29 })}`
30 );
pineafan63fc5e22022-08-04 22:04:10 +010031 }
TheCodedProfa16d1672023-01-18 18:58:34 -050032 if (oldThread.rateLimitPerUser !== newThread.rateLimitPerUser) {
pineafan3a02ea32022-08-11 21:35:04 +010033 list["slowmode"] = entry(
TheCodedProfa16d1672023-01-18 18:58:34 -050034 [oldThread.rateLimitPerUser!.toString(), newThread.rateLimitPerUser!.toString()],
35 `${humanizeDuration((oldThread.rateLimitPerUser ?? 0) * 1000)} -> ${humanizeDuration(
36 (newThread.rateLimitPerUser ?? 0) * 1000
Skyler Greyf21323a2022-08-13 23:58:22 +010037 )}`
Skyler Grey75ea9172022-08-06 10:22:23 +010038 );
pineafan63fc5e22022-08-04 22:04:10 +010039 }
40 if (!(Object.keys(list).length - 3)) return;
TheCodedProf6ec331b2023-02-20 12:13:06 -050041 list["updated"] = entry(Date.now(), renderDelta(Date.now()));
TheCodedProfa16d1672023-01-18 18:58:34 -050042 list["updatedBy"] = entry(auditLog.executor!.id, renderUser(auditLog.executor!));
pineafan63fc5e22022-08-04 22:04:10 +010043 const data = {
44 meta: {
45 type: "channelUpdate",
46 displayName: "Thread Edited",
47 calculateType: "channelUpdate",
48 color: NucleusColors.yellow,
49 emoji: "CHANNEL.TEXT.EDIT",
TheCodedProf6ec331b2023-02-20 12:13:06 -050050 timestamp: Date.now()
pineafan63fc5e22022-08-04 22:04:10 +010051 },
52 list: list,
53 hidden: {
TheCodedProfa16d1672023-01-18 18:58:34 -050054 guild: newThread.guild.id
pineafane625d782022-05-09 18:04:32 +010055 }
pineafan63fc5e22022-08-04 22:04:10 +010056 };
57 log(data);
pineafane625d782022-05-09 18:04:32 +010058}