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