pineafan | 3a02ea3 | 2022-08-11 21:35:04 +0100 | [diff] [blame] | 1 | import type { GuildAuditLogsEntry, ThreadChannel } from "discord.js"; |
| 2 | // @ts-expect-error |
pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame] | 3 | import humanizeDuration from "humanize-duration"; |
pineafan | 3a02ea3 | 2022-08-11 21:35:04 +0100 | [diff] [blame] | 4 | // @ts-expect-error |
| 5 | import type { HaikuClient } from "jshaiku"; |
| 6 | |
pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame] | 7 | export const event = "threadUpdate"; |
pineafan | e625d78 | 2022-05-09 18:04:32 +0100 | [diff] [blame] | 8 | |
pineafan | 3a02ea3 | 2022-08-11 21:35:04 +0100 | [diff] [blame] | 9 | export async function callback(client: HaikuClient, before: ThreadChannel, after: ThreadChannel) { |
| 10 | const { getAuditLog, log, NucleusColors, entry, renderUser, renderDelta, renderChannel } = client.logger; |
pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame] | 11 | const auditLog = await getAuditLog(after.guild, "THREAD_UPDATE"); |
pineafan | 3a02ea3 | 2022-08-11 21:35:04 +0100 | [diff] [blame] | 12 | const audit = auditLog.entries.filter((entry: GuildAuditLogsEntry) => entry.target!.id === after.id).first(); |
pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame] | 13 | if (audit.executor.id === client.user.id) return; |
pineafan | 3a02ea3 | 2022-08-11 21:35:04 +0100 | [diff] [blame] | 14 | const list: Record<string, ReturnType<typeof entry>> = { |
Skyler Grey | 75ea917 | 2022-08-06 10:22:23 +0100 | [diff] [blame] | 15 | threadId: entry(after.id, `\`${after.id}\``), |
pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame] | 16 | thread: entry(after.name, renderChannel(after)), |
| 17 | parentChannel: entry(after.parentId, renderChannel(after.parent)) |
| 18 | }; |
| 19 | if (before.name !== after.name) { |
pineafan | 3a02ea3 | 2022-08-11 21:35:04 +0100 | [diff] [blame] | 20 | list["name"] = entry([before.name, after.name], `${before.name} -> ${after.name}`); |
pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame] | 21 | } |
| 22 | if (before.autoArchiveDuration !== after.autoArchiveDuration) { |
pineafan | 3a02ea3 | 2022-08-11 21:35:04 +0100 | [diff] [blame] | 23 | list["autoArchiveDuration"] = entry( |
Skyler Grey | 75ea917 | 2022-08-06 10:22:23 +0100 | [diff] [blame] | 24 | [before.autoArchiveDuration, after.autoArchiveDuration], |
pineafan | 3a02ea3 | 2022-08-11 21:35:04 +0100 | [diff] [blame] | 25 | `${humanizeDuration((before.autoArchiveDuration ?? 0) * 60 * 1000, { |
Skyler Grey | 75ea917 | 2022-08-06 10:22:23 +0100 | [diff] [blame] | 26 | round: true |
pineafan | 3a02ea3 | 2022-08-11 21:35:04 +0100 | [diff] [blame] | 27 | })} -> ${humanizeDuration((after.autoArchiveDuration ?? 0) * 60 * 1000, { |
Skyler Grey | 75ea917 | 2022-08-06 10:22:23 +0100 | [diff] [blame] | 28 | round: true |
| 29 | })}` |
| 30 | ); |
pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame] | 31 | } |
| 32 | if (before.rateLimitPerUser !== after.rateLimitPerUser) { |
pineafan | 3a02ea3 | 2022-08-11 21:35:04 +0100 | [diff] [blame] | 33 | list["slowmode"] = entry( |
Skyler Grey | 75ea917 | 2022-08-06 10:22:23 +0100 | [diff] [blame] | 34 | [before.rateLimitPerUser, after.rateLimitPerUser], |
pineafan | 3a02ea3 | 2022-08-11 21:35:04 +0100 | [diff] [blame] | 35 | `${humanizeDuration((before.rateLimitPerUser ?? 0) * 1000)} -> ${humanizeDuration((after.rateLimitPerUser ?? 0) * 1000)}` |
Skyler Grey | 75ea917 | 2022-08-06 10:22:23 +0100 | [diff] [blame] | 36 | ); |
pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame] | 37 | } |
| 38 | if (!(Object.keys(list).length - 3)) return; |
pineafan | 3a02ea3 | 2022-08-11 21:35:04 +0100 | [diff] [blame] | 39 | list["updated"] = entry(new Date().getTime(), renderDelta(new Date().getTime())); |
| 40 | list["updatedBy"] = entry(audit.executor.id, renderUser(audit.executor)); |
pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame] | 41 | 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 |
pineafan | e625d78 | 2022-05-09 18:04:32 +0100 | [diff] [blame] | 53 | } |
pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame] | 54 | }; |
| 55 | log(data); |
pineafan | e625d78 | 2022-05-09 18:04:32 +0100 | [diff] [blame] | 56 | } |