TheCodedProf | a16d167 | 2023-01-18 18:58:34 -0500 | [diff] [blame^] | 1 | import { AuditLogEvent, GuildAuditLogsEntry, ThreadChannel } from "discord.js"; |
pineafan | 3a02ea3 | 2022-08-11 21:35:04 +0100 | [diff] [blame] | 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 | |
TheCodedProf | a16d167 | 2023-01-18 18:58:34 -0500 | [diff] [blame^] | 8 | export async function callback(client: NucleusClient, oldThread: ThreadChannel, newThread: ThreadChannel) { |
pineafan | 3a02ea3 | 2022-08-11 21:35:04 +0100 | [diff] [blame] | 9 | const { getAuditLog, log, NucleusColors, entry, renderUser, renderDelta, renderChannel } = client.logger; |
TheCodedProf | a16d167 | 2023-01-18 18:58:34 -0500 | [diff] [blame^] | 10 | const auditLog = (await getAuditLog(newThread.guild, AuditLogEvent.ThreadUpdate)) |
| 11 | .filter((entry: GuildAuditLogsEntry) => (entry.target as ThreadChannel)!.id === newThread.id)[0] as GuildAuditLogsEntry; |
| 12 | if (auditLog.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>> = { |
TheCodedProf | a16d167 | 2023-01-18 18:58:34 -0500 | [diff] [blame^] | 14 | threadId: entry(newThread.id, `\`${newThread.id}\``), |
| 15 | thread: entry(newThread.name, renderChannel(newThread)), |
| 16 | parentChannel: entry(newThread.parentId, renderChannel(newThread.parent!)) |
pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame] | 17 | }; |
TheCodedProf | a16d167 | 2023-01-18 18:58:34 -0500 | [diff] [blame^] | 18 | if (oldThread.name !== newThread.name) { |
| 19 | list["name"] = entry([oldThread.name, newThread.name], `${oldThread.name} -> ${newThread.name}`); |
pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame] | 20 | } |
TheCodedProf | a16d167 | 2023-01-18 18:58:34 -0500 | [diff] [blame^] | 21 | if (oldThread.autoArchiveDuration !== newThread.autoArchiveDuration) { |
pineafan | 3a02ea3 | 2022-08-11 21:35:04 +0100 | [diff] [blame] | 22 | list["autoArchiveDuration"] = entry( |
TheCodedProf | a16d167 | 2023-01-18 18:58:34 -0500 | [diff] [blame^] | 23 | [oldThread.autoArchiveDuration!.toString(), newThread.autoArchiveDuration!.toString()], |
| 24 | `${humanizeDuration((oldThread.autoArchiveDuration ?? 0) * 60 * 1000, { |
Skyler Grey | 75ea917 | 2022-08-06 10:22:23 +0100 | [diff] [blame] | 25 | round: true |
TheCodedProf | a16d167 | 2023-01-18 18:58:34 -0500 | [diff] [blame^] | 26 | })} -> ${humanizeDuration((newThread.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 | } |
TheCodedProf | a16d167 | 2023-01-18 18:58:34 -0500 | [diff] [blame^] | 31 | if (oldThread.rateLimitPerUser !== newThread.rateLimitPerUser) { |
pineafan | 3a02ea3 | 2022-08-11 21:35:04 +0100 | [diff] [blame] | 32 | list["slowmode"] = entry( |
TheCodedProf | a16d167 | 2023-01-18 18:58:34 -0500 | [diff] [blame^] | 33 | [oldThread.rateLimitPerUser!.toString(), newThread.rateLimitPerUser!.toString()], |
| 34 | `${humanizeDuration((oldThread.rateLimitPerUser ?? 0) * 1000)} -> ${humanizeDuration( |
| 35 | (newThread.rateLimitPerUser ?? 0) * 1000 |
Skyler Grey | f21323a | 2022-08-13 23:58:22 +0100 | [diff] [blame] | 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())); |
TheCodedProf | a16d167 | 2023-01-18 18:58:34 -0500 | [diff] [blame^] | 41 | list["updatedBy"] = entry(auditLog.executor!.id, renderUser(auditLog.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: { |
TheCodedProf | a16d167 | 2023-01-18 18:58:34 -0500 | [diff] [blame^] | 53 | guild: newThread.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 | } |