blob: 00773306951c1dcc884728cf82e601b3bcdbb678 [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],
Skyler Greyf21323a2022-08-13 23:58:22 +010035 `${humanizeDuration((before.rateLimitPerUser ?? 0) * 1000)} -> ${humanizeDuration(
36 (after.rateLimitPerUser ?? 0) * 1000
37 )}`
Skyler Grey75ea9172022-08-06 10:22:23 +010038 );
pineafan63fc5e22022-08-04 22:04:10 +010039 }
40 if (!(Object.keys(list).length - 3)) return;
pineafan3a02ea32022-08-11 21:35:04 +010041 list["updated"] = entry(new Date().getTime(), renderDelta(new Date().getTime()));
42 list["updatedBy"] = entry(audit.executor.id, renderUser(audit.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",
50 timestamp: new Date().getTime()
51 },
52 list: list,
53 hidden: {
54 guild: after.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}