blob: 8690af23b325959c9f571f4c6a4b37f96bb3793f [file] [log] [blame]
PineaFan752af462022-12-31 21:59:38 +00001import type { NucleusClient } from "../utils/client.js";
TheCodedProfa16d1672023-01-18 18:58:34 -05002import { AuditLogEvent, 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) {
TheCodedProfa16d1672023-01-18 18:58:34 -05008 await statsChannelUpdate(client, after.members.me!);
pineafan0f5cc782022-08-12 21:55:42 +01009 const { getAuditLog, log, NucleusColors, entry, renderUser, renderDelta } = client.logger;
TheCodedProfa16d1672023-01-18 18:58:34 -050010 const auditLog = (await getAuditLog(after, AuditLogEvent.GuildUpdate))
11 .filter((entry: GuildAuditLogsEntry) => (entry.target as Guild)!.id === after.id)[0]!;
12 if (auditLog.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
TheCodedProfa16d1672023-01-18 18:58:34 -050015 const verificationLevels = [
16 "Unrestricted",
17 "Verified email",
18 "Registered (5 minutes)",
19 "Member (10 minutes)",
20 "Verified phone"
21 ];
pineafan63fc5e22022-08-04 22:04:10 +010022
TheCodedProfa16d1672023-01-18 18:58:34 -050023 const explicitContentFilterLevels = [
24 "Disabled",
25 "Members without roles",
26 "All members"
27 ];
pineafan63fc5e22022-08-04 22:04:10 +010028
TheCodedProfa16d1672023-01-18 18:58:34 -050029 const MFALevels = [
30 "None",
31 "Enabled"
32 ];
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)
TheCodedProfa16d1672023-01-18 18:58:34 -050039 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(
TheCodedProfa16d1672023-01-18 18:58:34 -050042 [before.splash!, after.splash!],
Skyler Grey75ea9172022-08-06 10:22:23 +010043 `[Before](${before.splashURL()}) -> [After](${after.splashURL()})`
44 );
45 if (before.banner !== after.banner)
pineafan0f5cc782022-08-12 21:55:42 +010046 list["banner"] = entry(
TheCodedProfa16d1672023-01-18 18:58:34 -050047 [before.banner!, after.banner!],
Skyler Grey75ea9172022-08-06 10:22:23 +010048 `[Before](${before.bannerURL()}) -> [After](${after.bannerURL()})`
49 );
pineafan0f5cc782022-08-12 21:55:42 +010050 if (beforeOwner !== afterOwner)
51 list["owner"] = entry(
TheCodedProfa16d1672023-01-18 18:58:34 -050052 [beforeOwner.id, afterOwner.id],
pineafan0f5cc782022-08-12 21:55:42 +010053 `${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(
TheCodedProfa16d1672023-01-18 18:58:34 -050057 [verificationLevels[before.verificationLevel.valueOf()]!, verificationLevels[before.verificationLevel.valueOf()]!],
58 `${verificationLevels[before.verificationLevel.valueOf()]} -> ${verificationLevels[before.verificationLevel.valueOf()]}`
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 [
TheCodedProfa16d1672023-01-18 18:58:34 -050063 explicitContentFilterLevels[before.explicitContentFilter.valueOf()]!,
64 explicitContentFilterLevels[after.explicitContentFilter.valueOf()]!
Skyler Grey75ea9172022-08-06 10:22:23 +010065 ],
66 `${explicitContentFilterLevels[before.explicitContentFilter]} -> ${
67 explicitContentFilterLevels[after.explicitContentFilter]
68 }`
69 );
70 if (before.mfaLevel !== after.mfaLevel)
71 list["2 factor authentication"] = entry(
TheCodedProfa16d1672023-01-18 18:58:34 -050072 [MFALevels[before.mfaLevel.valueOf()]!, MFALevels[after.mfaLevel.valueOf()]!],
73 `${MFALevels[before.mfaLevel.valueOf()]} -> ${MFALevels[after.mfaLevel.valueOf()]}`
Skyler Grey75ea9172022-08-06 10:22:23 +010074 );
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()));
TheCodedProfa16d1672023-01-18 18:58:34 -050078 list["updatedBy"] = entry(auditLog.executor!.id, renderUser(auditLog.executor!));
pineafan63fc5e22022-08-04 22:04:10 +010079 const data = {
80 meta: {
81 type: "guildUpdate",
PineaFan538d3752023-01-12 21:48:23 +000082 displayName: "Server Edited",
pineafan63fc5e22022-08-04 22:04:10 +010083 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}