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