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) { |
TheCodedProf | 6ec331b | 2023-02-20 12:13:06 -0500 | [diff] [blame^] | 9 | const { getAuditLog, isLogging, log, NucleusColors, entry, renderUser, renderDelta, renderChannel } = client.logger; |
| 10 | if (!await isLogging(newThread.guild.id, "channelUpdate")) return; |
TheCodedProf | a16d167 | 2023-01-18 18:58:34 -0500 | [diff] [blame] | 11 | const auditLog = (await getAuditLog(newThread.guild, AuditLogEvent.ThreadUpdate)) |
PineaFan | 638eb13 | 2023-01-19 10:41:22 +0000 | [diff] [blame] | 12 | .filter((entry: GuildAuditLogsEntry) => (entry.target as ThreadChannel)!.id === newThread.id)[0]!; |
TheCodedProf | a16d167 | 2023-01-18 18:58:34 -0500 | [diff] [blame] | 13 | if (auditLog.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>> = { |
TheCodedProf | a16d167 | 2023-01-18 18:58:34 -0500 | [diff] [blame] | 15 | threadId: entry(newThread.id, `\`${newThread.id}\``), |
| 16 | thread: entry(newThread.name, renderChannel(newThread)), |
| 17 | parentChannel: entry(newThread.parentId, renderChannel(newThread.parent!)) |
pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame] | 18 | }; |
TheCodedProf | a16d167 | 2023-01-18 18:58:34 -0500 | [diff] [blame] | 19 | if (oldThread.name !== newThread.name) { |
| 20 | list["name"] = entry([oldThread.name, newThread.name], `${oldThread.name} -> ${newThread.name}`); |
pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame] | 21 | } |
TheCodedProf | a16d167 | 2023-01-18 18:58:34 -0500 | [diff] [blame] | 22 | if (oldThread.autoArchiveDuration !== newThread.autoArchiveDuration) { |
pineafan | 3a02ea3 | 2022-08-11 21:35:04 +0100 | [diff] [blame] | 23 | list["autoArchiveDuration"] = entry( |
TheCodedProf | a16d167 | 2023-01-18 18:58:34 -0500 | [diff] [blame] | 24 | [oldThread.autoArchiveDuration!.toString(), newThread.autoArchiveDuration!.toString()], |
| 25 | `${humanizeDuration((oldThread.autoArchiveDuration ?? 0) * 60 * 1000, { |
Skyler Grey | 75ea917 | 2022-08-06 10:22:23 +0100 | [diff] [blame] | 26 | round: true |
TheCodedProf | a16d167 | 2023-01-18 18:58:34 -0500 | [diff] [blame] | 27 | })} -> ${humanizeDuration((newThread.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 | } |
TheCodedProf | a16d167 | 2023-01-18 18:58:34 -0500 | [diff] [blame] | 32 | if (oldThread.rateLimitPerUser !== newThread.rateLimitPerUser) { |
pineafan | 3a02ea3 | 2022-08-11 21:35:04 +0100 | [diff] [blame] | 33 | list["slowmode"] = entry( |
TheCodedProf | a16d167 | 2023-01-18 18:58:34 -0500 | [diff] [blame] | 34 | [oldThread.rateLimitPerUser!.toString(), newThread.rateLimitPerUser!.toString()], |
| 35 | `${humanizeDuration((oldThread.rateLimitPerUser ?? 0) * 1000)} -> ${humanizeDuration( |
| 36 | (newThread.rateLimitPerUser ?? 0) * 1000 |
Skyler Grey | f21323a | 2022-08-13 23:58:22 +0100 | [diff] [blame] | 37 | )}` |
Skyler Grey | 75ea917 | 2022-08-06 10:22:23 +0100 | [diff] [blame] | 38 | ); |
pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame] | 39 | } |
| 40 | if (!(Object.keys(list).length - 3)) return; |
TheCodedProf | 6ec331b | 2023-02-20 12:13:06 -0500 | [diff] [blame^] | 41 | list["updated"] = entry(Date.now(), renderDelta(Date.now())); |
TheCodedProf | a16d167 | 2023-01-18 18:58:34 -0500 | [diff] [blame] | 42 | list["updatedBy"] = entry(auditLog.executor!.id, renderUser(auditLog.executor!)); |
pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame] | 43 | const data = { |
| 44 | meta: { |
| 45 | type: "channelUpdate", |
| 46 | displayName: "Thread Edited", |
| 47 | calculateType: "channelUpdate", |
| 48 | color: NucleusColors.yellow, |
| 49 | emoji: "CHANNEL.TEXT.EDIT", |
TheCodedProf | 6ec331b | 2023-02-20 12:13:06 -0500 | [diff] [blame^] | 50 | timestamp: Date.now() |
pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame] | 51 | }, |
| 52 | list: list, |
| 53 | hidden: { |
TheCodedProf | a16d167 | 2023-01-18 18:58:34 -0500 | [diff] [blame] | 54 | guild: newThread.guild.id |
pineafan | e625d78 | 2022-05-09 18:04:32 +0100 | [diff] [blame] | 55 | } |
pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame] | 56 | }; |
| 57 | log(data); |
pineafan | e625d78 | 2022-05-09 18:04:32 +0100 | [diff] [blame] | 58 | } |