blob: 20247d631a149fd625671ea1efce28882667fe97 [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";
PineaFan64486c42022-12-28 09:21:04 +00004import type { HaikuClient } from "../utils/haiku/index.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
pineafan3a02ea32022-08-11 21:35:04 +01008export async function callback(client: HaikuClient, before: ThreadChannel, after: ThreadChannel) {
9 const { getAuditLog, log, NucleusColors, entry, renderUser, renderDelta, renderChannel } = client.logger;
pineafan63fc5e22022-08-04 22:04:10 +010010 const auditLog = await getAuditLog(after.guild, "THREAD_UPDATE");
pineafan3a02ea32022-08-11 21:35:04 +010011 const audit = auditLog.entries.filter((entry: GuildAuditLogsEntry) => entry.target!.id === after.id).first();
pineafan63fc5e22022-08-04 22:04:10 +010012 if (audit.executor.id === client.user.id) return;
pineafan3a02ea32022-08-11 21:35:04 +010013 const list: Record<string, ReturnType<typeof entry>> = {
Skyler Grey75ea9172022-08-06 10:22:23 +010014 threadId: entry(after.id, `\`${after.id}\``),
pineafan63fc5e22022-08-04 22:04:10 +010015 thread: entry(after.name, renderChannel(after)),
16 parentChannel: entry(after.parentId, renderChannel(after.parent))
17 };
18 if (before.name !== after.name) {
pineafan3a02ea32022-08-11 21:35:04 +010019 list["name"] = entry([before.name, after.name], `${before.name} -> ${after.name}`);
pineafan63fc5e22022-08-04 22:04:10 +010020 }
21 if (before.autoArchiveDuration !== after.autoArchiveDuration) {
pineafan3a02ea32022-08-11 21:35:04 +010022 list["autoArchiveDuration"] = entry(
Skyler Grey75ea9172022-08-06 10:22:23 +010023 [before.autoArchiveDuration, after.autoArchiveDuration],
pineafan3a02ea32022-08-11 21:35:04 +010024 `${humanizeDuration((before.autoArchiveDuration ?? 0) * 60 * 1000, {
Skyler Grey75ea9172022-08-06 10:22:23 +010025 round: true
pineafan3a02ea32022-08-11 21:35:04 +010026 })} -> ${humanizeDuration((after.autoArchiveDuration ?? 0) * 60 * 1000, {
Skyler Grey75ea9172022-08-06 10:22:23 +010027 round: true
28 })}`
29 );
pineafan63fc5e22022-08-04 22:04:10 +010030 }
31 if (before.rateLimitPerUser !== after.rateLimitPerUser) {
pineafan3a02ea32022-08-11 21:35:04 +010032 list["slowmode"] = entry(
Skyler Grey75ea9172022-08-06 10:22:23 +010033 [before.rateLimitPerUser, after.rateLimitPerUser],
Skyler Greyf21323a2022-08-13 23:58:22 +010034 `${humanizeDuration((before.rateLimitPerUser ?? 0) * 1000)} -> ${humanizeDuration(
35 (after.rateLimitPerUser ?? 0) * 1000
36 )}`
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()));
41 list["updatedBy"] = entry(audit.executor.id, renderUser(audit.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: {
53 guild: after.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}