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