blob: 4df2587bb6058dc5aa34da91e10185722d313c8b [file] [log] [blame]
pineafan3a02ea32022-08-11 21:35:04 +01001import type { GuildAuditLogsEntry, ThreadChannel } from "discord.js";
2// @ts-expect-error
pineafan63fc5e22022-08-04 22:04:10 +01003import humanizeDuration from "humanize-duration";
pineafan3a02ea32022-08-11 21:35:04 +01004// @ts-expect-error
5import type { HaikuClient } from "jshaiku";
6
pineafan63fc5e22022-08-04 22:04:10 +01007export const event = "threadUpdate";
pineafane625d782022-05-09 18:04:32 +01008
pineafan3a02ea32022-08-11 21:35:04 +01009export async function callback(client: HaikuClient, before: ThreadChannel, after: ThreadChannel) {
10 const { getAuditLog, log, NucleusColors, entry, renderUser, renderDelta, renderChannel } = client.logger;
pineafan63fc5e22022-08-04 22:04:10 +010011 const auditLog = await getAuditLog(after.guild, "THREAD_UPDATE");
pineafan3a02ea32022-08-11 21:35:04 +010012 const audit = auditLog.entries.filter((entry: GuildAuditLogsEntry) => entry.target!.id === after.id).first();
pineafan63fc5e22022-08-04 22:04:10 +010013 if (audit.executor.id === client.user.id) return;
pineafan3a02ea32022-08-11 21:35:04 +010014 const list: Record<string, ReturnType<typeof entry>> = {
Skyler Grey75ea9172022-08-06 10:22:23 +010015 threadId: entry(after.id, `\`${after.id}\``),
pineafan63fc5e22022-08-04 22:04:10 +010016 thread: entry(after.name, renderChannel(after)),
17 parentChannel: entry(after.parentId, renderChannel(after.parent))
18 };
19 if (before.name !== after.name) {
pineafan3a02ea32022-08-11 21:35:04 +010020 list["name"] = entry([before.name, after.name], `${before.name} -> ${after.name}`);
pineafan63fc5e22022-08-04 22:04:10 +010021 }
22 if (before.autoArchiveDuration !== after.autoArchiveDuration) {
pineafan3a02ea32022-08-11 21:35:04 +010023 list["autoArchiveDuration"] = entry(
Skyler Grey75ea9172022-08-06 10:22:23 +010024 [before.autoArchiveDuration, after.autoArchiveDuration],
pineafan3a02ea32022-08-11 21:35:04 +010025 `${humanizeDuration((before.autoArchiveDuration ?? 0) * 60 * 1000, {
Skyler Grey75ea9172022-08-06 10:22:23 +010026 round: true
pineafan3a02ea32022-08-11 21:35:04 +010027 })} -> ${humanizeDuration((after.autoArchiveDuration ?? 0) * 60 * 1000, {
Skyler Grey75ea9172022-08-06 10:22:23 +010028 round: true
29 })}`
30 );
pineafan63fc5e22022-08-04 22:04:10 +010031 }
32 if (before.rateLimitPerUser !== after.rateLimitPerUser) {
pineafan3a02ea32022-08-11 21:35:04 +010033 list["slowmode"] = entry(
Skyler Grey75ea9172022-08-06 10:22:23 +010034 [before.rateLimitPerUser, after.rateLimitPerUser],
pineafan3a02ea32022-08-11 21:35:04 +010035 `${humanizeDuration((before.rateLimitPerUser ?? 0) * 1000)} -> ${humanizeDuration((after.rateLimitPerUser ?? 0) * 1000)}`
Skyler Grey75ea9172022-08-06 10:22:23 +010036 );
pineafan63fc5e22022-08-04 22:04:10 +010037 }
38 if (!(Object.keys(list).length - 3)) return;
pineafan3a02ea32022-08-11 21:35:04 +010039 list["updated"] = entry(new Date().getTime(), renderDelta(new Date().getTime()));
40 list["updatedBy"] = entry(audit.executor.id, renderUser(audit.executor));
pineafan63fc5e22022-08-04 22:04:10 +010041 const data = {
42 meta: {
43 type: "channelUpdate",
44 displayName: "Thread Edited",
45 calculateType: "channelUpdate",
46 color: NucleusColors.yellow,
47 emoji: "CHANNEL.TEXT.EDIT",
48 timestamp: new Date().getTime()
49 },
50 list: list,
51 hidden: {
52 guild: after.guild.id
pineafane625d782022-05-09 18:04:32 +010053 }
pineafan63fc5e22022-08-04 22:04:10 +010054 };
55 log(data);
pineafane625d782022-05-09 18:04:32 +010056}