blob: e463007821a9a13e631f324b0cd2e1906a22e29b [file] [log] [blame]
PineaFan752af462022-12-31 21:59:38 +00001import type { NucleusClient } from "../utils/client.js";
Skyler Greyf21323a2022-08-13 23:58:22 +01002import type { Guild, GuildAuditLogsEntry } from "discord.js";
pineafan63fc5e22022-08-04 22:04:10 +01003import { callback as statsChannelUpdate } from "../reflex/statsChannelUpdate.js";
pineafane23c4ec2022-07-27 21:56:27 +01004
pineafan63fc5e22022-08-04 22:04:10 +01005export const event = "guildUpdate";
pineafane625d782022-05-09 18:04:32 +01006
PineaFan752af462022-12-31 21:59:38 +00007export async function callback(client: NucleusClient, before: Guild, after: Guild) {
pineafan0f5cc782022-08-12 21:55:42 +01008 await statsChannelUpdate(client, after.me!);
9 const { getAuditLog, log, NucleusColors, entry, renderUser, renderDelta } = client.logger;
pineafan63fc5e22022-08-04 22:04:10 +010010 const auditLog = await getAuditLog(after, "GUILD_UPDATE");
pineafan0f5cc782022-08-12 21:55:42 +010011 const audit = auditLog.entries.filter((entry: GuildAuditLogsEntry) => entry.target!.id === after.id).first();
pineafan63fc5e22022-08-04 22:04:10 +010012 if (audit.executor.id === client.user.id) return;
pineafan0f5cc782022-08-12 21:55:42 +010013 const list: Record<string, ReturnType<typeof entry>> = {};
pineafane625d782022-05-09 18:04:32 +010014
pineafan63fc5e22022-08-04 22:04:10 +010015 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 Greyf21323a2022-08-13 23:58:22 +010033 const beforeOwner = await before.fetchOwner();
34 const afterOwner = await after.fetchOwner();
pineafan63fc5e22022-08-04 22:04:10 +010035
pineafan0f5cc782022-08-12 21:55:42 +010036 if (before.name !== after.name) list["name"] = entry([before.name, after.name], `${before.name} -> ${after.name}`);
Skyler Grey75ea9172022-08-06 10:22:23 +010037 if (before.icon !== after.icon)
pineafan0f5cc782022-08-12 21:55:42 +010038 list["icon"] = entry([before.icon, after.icon], `[Before](${before.iconURL()}) -> [After](${after.iconURL()})`);
Skyler Grey75ea9172022-08-06 10:22:23 +010039 if (before.splash !== after.splash)
pineafan0f5cc782022-08-12 21:55:42 +010040 list["splash"] = entry(
Skyler Grey75ea9172022-08-06 10:22:23 +010041 [before.splash, after.splash],
42 `[Before](${before.splashURL()}) -> [After](${after.splashURL()})`
43 );
44 if (before.banner !== after.banner)
pineafan0f5cc782022-08-12 21:55:42 +010045 list["banner"] = entry(
Skyler Grey75ea9172022-08-06 10:22:23 +010046 [before.banner, after.banner],
47 `[Before](${before.bannerURL()}) -> [After](${after.bannerURL()})`
48 );
pineafan0f5cc782022-08-12 21:55:42 +010049 if (beforeOwner !== afterOwner)
50 list["owner"] = entry(
51 [beforeOwner, afterOwner],
52 `${renderUser(beforeOwner.user)} -> ${renderUser(afterOwner.user)}`
Skyler Grey75ea9172022-08-06 10:22:23 +010053 );
54 if (before.verificationLevel !== after.verificationLevel)
pineafan0f5cc782022-08-12 21:55:42 +010055 list["verificationLevel"] = entry(
Skyler Grey11236ba2022-08-08 21:13:33 +010056 [verificationLevels[before.verificationLevel], verificationLevels[after.verificationLevel]],
57 `${verificationLevels[before.verificationLevel]} -> ${verificationLevels[after.verificationLevel]}`
Skyler Grey75ea9172022-08-06 10:22:23 +010058 );
59 if (before.explicitContentFilter !== after.explicitContentFilter)
pineafan0f5cc782022-08-12 21:55:42 +010060 list["explicitContentFilter"] = entry(
Skyler Grey75ea9172022-08-06 10:22:23 +010061 [
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 );
pineafan63fc5e22022-08-04 22:04:10 +010074
Skyler Grey75ea9172022-08-06 10:22:23 +010075 if (!Object.keys(list).length) return;
pineafan0f5cc782022-08-12 21:55:42 +010076 list["updated"] = entry(new Date().getTime(), renderDelta(new Date().getTime()));
77 list["updatedBy"] = entry(audit.executor.id, renderUser(audit.executor));
pineafan63fc5e22022-08-04 22:04:10 +010078 const data = {
79 meta: {
80 type: "guildUpdate",
81 displayName: "Guild Edited",
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
pineafane625d782022-05-09 18:04:32 +010090 }
pineafan63fc5e22022-08-04 22:04:10 +010091 };
92 log(data);
pineafane625d782022-05-09 18:04:32 +010093}