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