blob: 3274727e42c0f8bcd45ae45bde4ddb94960f542e [file] [log] [blame]
pineafan0f5cc782022-08-12 21:55:42 +01001// @ts-expect-error
2import type { HaikuClient } from "jshaiku";
Skyler Greyf21323a2022-08-13 23:58:22 +01003import type { Guild, GuildAuditLogsEntry } from "discord.js";
pineafan63fc5e22022-08-04 22:04:10 +01004import { callback as statsChannelUpdate } from "../reflex/statsChannelUpdate.js";
pineafane23c4ec2022-07-27 21:56:27 +01005
pineafan63fc5e22022-08-04 22:04:10 +01006export const event = "guildUpdate";
pineafane625d782022-05-09 18:04:32 +01007
pineafan0f5cc782022-08-12 21:55:42 +01008export 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;
pineafan63fc5e22022-08-04 22:04:10 +010011 const auditLog = await getAuditLog(after, "GUILD_UPDATE");
pineafan0f5cc782022-08-12 21:55:42 +010012 const audit = auditLog.entries.filter((entry: GuildAuditLogsEntry) => entry.target!.id === after.id).first();
pineafan63fc5e22022-08-04 22:04:10 +010013 if (audit.executor.id === client.user.id) return;
pineafan0f5cc782022-08-12 21:55:42 +010014 const list: Record<string, ReturnType<typeof entry>> = {};
pineafane625d782022-05-09 18:04:32 +010015
pineafan63fc5e22022-08-04 22:04:10 +010016 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 };
Skyler Greyf21323a2022-08-13 23:58:22 +010034 const beforeOwner = await before.fetchOwner();
35 const afterOwner = await after.fetchOwner();
pineafan63fc5e22022-08-04 22:04:10 +010036
pineafan0f5cc782022-08-12 21:55:42 +010037 if (before.name !== after.name) list["name"] = entry([before.name, after.name], `${before.name} -> ${after.name}`);
Skyler Grey75ea9172022-08-06 10:22:23 +010038 if (before.icon !== after.icon)
pineafan0f5cc782022-08-12 21:55:42 +010039 list["icon"] = entry([before.icon, after.icon], `[Before](${before.iconURL()}) -> [After](${after.iconURL()})`);
Skyler Grey75ea9172022-08-06 10:22:23 +010040 if (before.splash !== after.splash)
pineafan0f5cc782022-08-12 21:55:42 +010041 list["splash"] = entry(
Skyler Grey75ea9172022-08-06 10:22:23 +010042 [before.splash, after.splash],
43 `[Before](${before.splashURL()}) -> [After](${after.splashURL()})`
44 );
45 if (before.banner !== after.banner)
pineafan0f5cc782022-08-12 21:55:42 +010046 list["banner"] = entry(
Skyler Grey75ea9172022-08-06 10:22:23 +010047 [before.banner, after.banner],
48 `[Before](${before.bannerURL()}) -> [After](${after.bannerURL()})`
49 );
pineafan0f5cc782022-08-12 21:55:42 +010050 if (beforeOwner !== afterOwner)
51 list["owner"] = entry(
52 [beforeOwner, afterOwner],
53 `${renderUser(beforeOwner.user)} -> ${renderUser(afterOwner.user)}`
Skyler Grey75ea9172022-08-06 10:22:23 +010054 );
55 if (before.verificationLevel !== after.verificationLevel)
pineafan0f5cc782022-08-12 21:55:42 +010056 list["verificationLevel"] = entry(
Skyler Grey11236ba2022-08-08 21:13:33 +010057 [verificationLevels[before.verificationLevel], verificationLevels[after.verificationLevel]],
58 `${verificationLevels[before.verificationLevel]} -> ${verificationLevels[after.verificationLevel]}`
Skyler Grey75ea9172022-08-06 10:22:23 +010059 );
60 if (before.explicitContentFilter !== after.explicitContentFilter)
pineafan0f5cc782022-08-12 21:55:42 +010061 list["explicitContentFilter"] = entry(
Skyler Grey75ea9172022-08-06 10:22:23 +010062 [
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 );
pineafan63fc5e22022-08-04 22:04:10 +010075
Skyler Grey75ea9172022-08-06 10:22:23 +010076 if (!Object.keys(list).length) return;
pineafan0f5cc782022-08-12 21:55:42 +010077 list["updated"] = entry(new Date().getTime(), renderDelta(new Date().getTime()));
78 list["updatedBy"] = entry(audit.executor.id, renderUser(audit.executor));
pineafan63fc5e22022-08-04 22:04:10 +010079 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
pineafane625d782022-05-09 18:04:32 +010091 }
pineafan63fc5e22022-08-04 22:04:10 +010092 };
93 log(data);
pineafane625d782022-05-09 18:04:32 +010094}