blob: c48c82fed18700b4b56b0e16f6d094312be73c3a [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) {
pineafan6de4da52023-03-07 20:43:44 +00008 await statsChannelUpdate(after.members.me!.user, after);
TheCodedProf6ec331b2023-02-20 12:13:06 -05009 const { getAuditLog, isLogging, log, NucleusColors, entry, renderUser, renderDelta } = client.logger;
Skyler Greyda16adf2023-03-05 10:22:12 +000010 if (!(await isLogging(after.id, "guildUpdate"))) return;
11 const auditLog = (await getAuditLog(after, AuditLogEvent.GuildUpdate)).filter(
12 (entry: GuildAuditLogsEntry) => (entry.target as Guild)!.id === after.id
13 )[0]!;
TheCodedProfa16d1672023-01-18 18:58:34 -050014 if (auditLog.executor!.id === client.user!.id) return;
pineafan0f5cc782022-08-12 21:55:42 +010015 const list: Record<string, ReturnType<typeof entry>> = {};
pineafane625d782022-05-09 18:04:32 +010016
TheCodedProfa16d1672023-01-18 18:58:34 -050017 const verificationLevels = [
18 "Unrestricted",
19 "Verified email",
20 "Registered (5 minutes)",
21 "Member (10 minutes)",
22 "Verified phone"
23 ];
pineafan63fc5e22022-08-04 22:04:10 +010024
Skyler Greyda16adf2023-03-05 10:22:12 +000025 const explicitContentFilterLevels = ["Disabled", "Members without roles", "All members"];
pineafan63fc5e22022-08-04 22:04:10 +010026
Skyler Greyda16adf2023-03-05 10:22:12 +000027 const MFALevels = ["None", "Enabled"];
TheCodedProfa16d1672023-01-18 18:58:34 -050028
Skyler Greyf21323a2022-08-13 23:58:22 +010029 const beforeOwner = await before.fetchOwner();
30 const afterOwner = await after.fetchOwner();
pineafan63fc5e22022-08-04 22:04:10 +010031
pineafan0f5cc782022-08-12 21:55:42 +010032 if (before.name !== after.name) list["name"] = entry([before.name, after.name], `${before.name} -> ${after.name}`);
Skyler Grey75ea9172022-08-06 10:22:23 +010033 if (before.icon !== after.icon)
Skyler Greyda16adf2023-03-05 10:22:12 +000034 list["icon"] = entry(
35 [before.icon!, after.icon!],
36 `[Before](${before.iconURL()}) -> [After](${after.iconURL()})`
37 );
Skyler Grey75ea9172022-08-06 10:22:23 +010038 if (before.splash !== after.splash)
pineafan0f5cc782022-08-12 21:55:42 +010039 list["splash"] = entry(
TheCodedProfa16d1672023-01-18 18:58:34 -050040 [before.splash!, after.splash!],
Skyler Grey75ea9172022-08-06 10:22:23 +010041 `[Before](${before.splashURL()}) -> [After](${after.splashURL()})`
42 );
43 if (before.banner !== after.banner)
pineafan0f5cc782022-08-12 21:55:42 +010044 list["banner"] = entry(
TheCodedProfa16d1672023-01-18 18:58:34 -050045 [before.banner!, after.banner!],
Skyler Grey75ea9172022-08-06 10:22:23 +010046 `[Before](${before.bannerURL()}) -> [After](${after.bannerURL()})`
47 );
pineafan0f5cc782022-08-12 21:55:42 +010048 if (beforeOwner !== afterOwner)
49 list["owner"] = entry(
TheCodedProfa16d1672023-01-18 18:58:34 -050050 [beforeOwner.id, afterOwner.id],
pineafan0f5cc782022-08-12 21:55:42 +010051 `${renderUser(beforeOwner.user)} -> ${renderUser(afterOwner.user)}`
Skyler Grey75ea9172022-08-06 10:22:23 +010052 );
53 if (before.verificationLevel !== after.verificationLevel)
pineafan0f5cc782022-08-12 21:55:42 +010054 list["verificationLevel"] = entry(
Skyler Greyda16adf2023-03-05 10:22:12 +000055 [
56 verificationLevels[before.verificationLevel.valueOf()]!,
57 verificationLevels[before.verificationLevel.valueOf()]!
58 ],
59 `${verificationLevels[before.verificationLevel.valueOf()]} -> ${
60 verificationLevels[before.verificationLevel.valueOf()]
61 }`
Skyler Grey75ea9172022-08-06 10:22:23 +010062 );
63 if (before.explicitContentFilter !== after.explicitContentFilter)
pineafan0f5cc782022-08-12 21:55:42 +010064 list["explicitContentFilter"] = entry(
Skyler Grey75ea9172022-08-06 10:22:23 +010065 [
TheCodedProfa16d1672023-01-18 18:58:34 -050066 explicitContentFilterLevels[before.explicitContentFilter.valueOf()]!,
67 explicitContentFilterLevels[after.explicitContentFilter.valueOf()]!
Skyler Grey75ea9172022-08-06 10:22:23 +010068 ],
69 `${explicitContentFilterLevels[before.explicitContentFilter]} -> ${
70 explicitContentFilterLevels[after.explicitContentFilter]
71 }`
72 );
73 if (before.mfaLevel !== after.mfaLevel)
74 list["2 factor authentication"] = entry(
TheCodedProfa16d1672023-01-18 18:58:34 -050075 [MFALevels[before.mfaLevel.valueOf()]!, MFALevels[after.mfaLevel.valueOf()]!],
76 `${MFALevels[before.mfaLevel.valueOf()]} -> ${MFALevels[after.mfaLevel.valueOf()]}`
Skyler Grey75ea9172022-08-06 10:22:23 +010077 );
pineafan63fc5e22022-08-04 22:04:10 +010078
Skyler Grey75ea9172022-08-06 10:22:23 +010079 if (!Object.keys(list).length) return;
TheCodedProf6ec331b2023-02-20 12:13:06 -050080 list["updated"] = entry(Date.now(), renderDelta(Date.now()));
TheCodedProfa16d1672023-01-18 18:58:34 -050081 list["updatedBy"] = entry(auditLog.executor!.id, renderUser(auditLog.executor!));
pineafan63fc5e22022-08-04 22:04:10 +010082 const data = {
83 meta: {
84 type: "guildUpdate",
PineaFan538d3752023-01-12 21:48:23 +000085 displayName: "Server Edited",
pineafan63fc5e22022-08-04 22:04:10 +010086 calculateType: "guildUpdate",
87 color: NucleusColors.yellow,
88 emoji: "GUILD.YELLOW",
TheCodedProf6ec331b2023-02-20 12:13:06 -050089 timestamp: Date.now()
pineafan63fc5e22022-08-04 22:04:10 +010090 },
91 list: list,
92 hidden: {
93 guild: after.id
pineafane625d782022-05-09 18:04:32 +010094 }
pineafan63fc5e22022-08-04 22:04:10 +010095 };
96 log(data);
pineafane625d782022-05-09 18:04:32 +010097}