PineaFan | 752af46 | 2022-12-31 21:59:38 +0000 | [diff] [blame] | 1 | import type { NucleusClient } from "../utils/client.js"; |
TheCodedProf | a16d167 | 2023-01-18 18:58:34 -0500 | [diff] [blame] | 2 | import { AuditLogEvent, Guild, GuildAuditLogsEntry } from "discord.js"; |
pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame] | 3 | import { callback as statsChannelUpdate } from "../reflex/statsChannelUpdate.js"; |
pineafan | e23c4ec | 2022-07-27 21:56:27 +0100 | [diff] [blame] | 4 | |
pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame] | 5 | export const event = "guildUpdate"; |
pineafan | e625d78 | 2022-05-09 18:04:32 +0100 | [diff] [blame] | 6 | |
PineaFan | 752af46 | 2022-12-31 21:59:38 +0000 | [diff] [blame] | 7 | export async function callback(client: NucleusClient, before: Guild, after: Guild) { |
TheCodedProf | a16d167 | 2023-01-18 18:58:34 -0500 | [diff] [blame] | 8 | await statsChannelUpdate(client, after.members.me!); |
TheCodedProf | 6ec331b | 2023-02-20 12:13:06 -0500 | [diff] [blame] | 9 | const { getAuditLog, isLogging, log, NucleusColors, entry, renderUser, renderDelta } = client.logger; |
| 10 | if (!await isLogging(after.id, "guildUpdate")) return; |
TheCodedProf | a16d167 | 2023-01-18 18:58:34 -0500 | [diff] [blame] | 11 | const auditLog = (await getAuditLog(after, AuditLogEvent.GuildUpdate)) |
| 12 | .filter((entry: GuildAuditLogsEntry) => (entry.target as Guild)!.id === after.id)[0]!; |
| 13 | if (auditLog.executor!.id === client.user!.id) return; |
pineafan | 0f5cc78 | 2022-08-12 21:55:42 +0100 | [diff] [blame] | 14 | const list: Record<string, ReturnType<typeof entry>> = {}; |
pineafan | e625d78 | 2022-05-09 18:04:32 +0100 | [diff] [blame] | 15 | |
TheCodedProf | a16d167 | 2023-01-18 18:58:34 -0500 | [diff] [blame] | 16 | const verificationLevels = [ |
| 17 | "Unrestricted", |
| 18 | "Verified email", |
| 19 | "Registered (5 minutes)", |
| 20 | "Member (10 minutes)", |
| 21 | "Verified phone" |
| 22 | ]; |
pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame] | 23 | |
TheCodedProf | a16d167 | 2023-01-18 18:58:34 -0500 | [diff] [blame] | 24 | const explicitContentFilterLevels = [ |
| 25 | "Disabled", |
| 26 | "Members without roles", |
| 27 | "All members" |
| 28 | ]; |
pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame] | 29 | |
TheCodedProf | a16d167 | 2023-01-18 18:58:34 -0500 | [diff] [blame] | 30 | const MFALevels = [ |
| 31 | "None", |
| 32 | "Enabled" |
| 33 | ]; |
| 34 | |
Skyler Grey | f21323a | 2022-08-13 23:58:22 +0100 | [diff] [blame] | 35 | const beforeOwner = await before.fetchOwner(); |
| 36 | const afterOwner = await after.fetchOwner(); |
pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame] | 37 | |
pineafan | 0f5cc78 | 2022-08-12 21:55:42 +0100 | [diff] [blame] | 38 | if (before.name !== after.name) list["name"] = entry([before.name, after.name], `${before.name} -> ${after.name}`); |
Skyler Grey | 75ea917 | 2022-08-06 10:22:23 +0100 | [diff] [blame] | 39 | if (before.icon !== after.icon) |
TheCodedProf | a16d167 | 2023-01-18 18:58:34 -0500 | [diff] [blame] | 40 | list["icon"] = entry([before.icon!, after.icon!], `[Before](${before.iconURL()}) -> [After](${after.iconURL()})`); |
Skyler Grey | 75ea917 | 2022-08-06 10:22:23 +0100 | [diff] [blame] | 41 | if (before.splash !== after.splash) |
pineafan | 0f5cc78 | 2022-08-12 21:55:42 +0100 | [diff] [blame] | 42 | list["splash"] = entry( |
TheCodedProf | a16d167 | 2023-01-18 18:58:34 -0500 | [diff] [blame] | 43 | [before.splash!, after.splash!], |
Skyler Grey | 75ea917 | 2022-08-06 10:22:23 +0100 | [diff] [blame] | 44 | `[Before](${before.splashURL()}) -> [After](${after.splashURL()})` |
| 45 | ); |
| 46 | if (before.banner !== after.banner) |
pineafan | 0f5cc78 | 2022-08-12 21:55:42 +0100 | [diff] [blame] | 47 | list["banner"] = entry( |
TheCodedProf | a16d167 | 2023-01-18 18:58:34 -0500 | [diff] [blame] | 48 | [before.banner!, after.banner!], |
Skyler Grey | 75ea917 | 2022-08-06 10:22:23 +0100 | [diff] [blame] | 49 | `[Before](${before.bannerURL()}) -> [After](${after.bannerURL()})` |
| 50 | ); |
pineafan | 0f5cc78 | 2022-08-12 21:55:42 +0100 | [diff] [blame] | 51 | if (beforeOwner !== afterOwner) |
| 52 | list["owner"] = entry( |
TheCodedProf | a16d167 | 2023-01-18 18:58:34 -0500 | [diff] [blame] | 53 | [beforeOwner.id, afterOwner.id], |
pineafan | 0f5cc78 | 2022-08-12 21:55:42 +0100 | [diff] [blame] | 54 | `${renderUser(beforeOwner.user)} -> ${renderUser(afterOwner.user)}` |
Skyler Grey | 75ea917 | 2022-08-06 10:22:23 +0100 | [diff] [blame] | 55 | ); |
| 56 | if (before.verificationLevel !== after.verificationLevel) |
pineafan | 0f5cc78 | 2022-08-12 21:55:42 +0100 | [diff] [blame] | 57 | list["verificationLevel"] = entry( |
TheCodedProf | a16d167 | 2023-01-18 18:58:34 -0500 | [diff] [blame] | 58 | [verificationLevels[before.verificationLevel.valueOf()]!, verificationLevels[before.verificationLevel.valueOf()]!], |
| 59 | `${verificationLevels[before.verificationLevel.valueOf()]} -> ${verificationLevels[before.verificationLevel.valueOf()]}` |
Skyler Grey | 75ea917 | 2022-08-06 10:22:23 +0100 | [diff] [blame] | 60 | ); |
| 61 | if (before.explicitContentFilter !== after.explicitContentFilter) |
pineafan | 0f5cc78 | 2022-08-12 21:55:42 +0100 | [diff] [blame] | 62 | list["explicitContentFilter"] = entry( |
Skyler Grey | 75ea917 | 2022-08-06 10:22:23 +0100 | [diff] [blame] | 63 | [ |
TheCodedProf | a16d167 | 2023-01-18 18:58:34 -0500 | [diff] [blame] | 64 | explicitContentFilterLevels[before.explicitContentFilter.valueOf()]!, |
| 65 | explicitContentFilterLevels[after.explicitContentFilter.valueOf()]! |
Skyler Grey | 75ea917 | 2022-08-06 10:22:23 +0100 | [diff] [blame] | 66 | ], |
| 67 | `${explicitContentFilterLevels[before.explicitContentFilter]} -> ${ |
| 68 | explicitContentFilterLevels[after.explicitContentFilter] |
| 69 | }` |
| 70 | ); |
| 71 | if (before.mfaLevel !== after.mfaLevel) |
| 72 | list["2 factor authentication"] = entry( |
TheCodedProf | a16d167 | 2023-01-18 18:58:34 -0500 | [diff] [blame] | 73 | [MFALevels[before.mfaLevel.valueOf()]!, MFALevels[after.mfaLevel.valueOf()]!], |
| 74 | `${MFALevels[before.mfaLevel.valueOf()]} -> ${MFALevels[after.mfaLevel.valueOf()]}` |
Skyler Grey | 75ea917 | 2022-08-06 10:22:23 +0100 | [diff] [blame] | 75 | ); |
pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame] | 76 | |
Skyler Grey | 75ea917 | 2022-08-06 10:22:23 +0100 | [diff] [blame] | 77 | if (!Object.keys(list).length) return; |
TheCodedProf | 6ec331b | 2023-02-20 12:13:06 -0500 | [diff] [blame] | 78 | list["updated"] = entry(Date.now(), renderDelta(Date.now())); |
TheCodedProf | a16d167 | 2023-01-18 18:58:34 -0500 | [diff] [blame] | 79 | list["updatedBy"] = entry(auditLog.executor!.id, renderUser(auditLog.executor!)); |
pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame] | 80 | const data = { |
| 81 | meta: { |
| 82 | type: "guildUpdate", |
PineaFan | 538d375 | 2023-01-12 21:48:23 +0000 | [diff] [blame] | 83 | displayName: "Server Edited", |
pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame] | 84 | calculateType: "guildUpdate", |
| 85 | color: NucleusColors.yellow, |
| 86 | emoji: "GUILD.YELLOW", |
TheCodedProf | 6ec331b | 2023-02-20 12:13:06 -0500 | [diff] [blame] | 87 | timestamp: Date.now() |
pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame] | 88 | }, |
| 89 | list: list, |
| 90 | hidden: { |
| 91 | guild: after.id |
pineafan | e625d78 | 2022-05-09 18:04:32 +0100 | [diff] [blame] | 92 | } |
pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame] | 93 | }; |
| 94 | log(data); |
pineafan | e625d78 | 2022-05-09 18:04:32 +0100 | [diff] [blame] | 95 | } |