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