blob: 6b25e4872c80273bcb113afc02926af0c6673011 [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!);
Samuel Shuert27bf3cd2023-03-03 15:51:25 -05009 const { getAuditLog, isLogging, log, NucleusColors, entry, renderUser, renderDelta } = client.logger;
10 if (!await isLogging(after.id, "guildUpdate")) return;
TheCodedProfa16d1672023-01-18 18:58:34 -050011 const auditLog = (await getAuditLog(after, AuditLogEvent.GuildUpdate))
12 .filter((entry: GuildAuditLogsEntry) => (entry.target as Guild)!.id === after.id)[0]!;
13 if (auditLog.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
TheCodedProfa16d1672023-01-18 18:58:34 -050016 const verificationLevels = [
17 "Unrestricted",
18 "Verified email",
19 "Registered (5 minutes)",
20 "Member (10 minutes)",
21 "Verified phone"
22 ];
pineafan63fc5e22022-08-04 22:04:10 +010023
TheCodedProfa16d1672023-01-18 18:58:34 -050024 const explicitContentFilterLevels = [
25 "Disabled",
26 "Members without roles",
27 "All members"
28 ];
pineafan63fc5e22022-08-04 22:04:10 +010029
TheCodedProfa16d1672023-01-18 18:58:34 -050030 const MFALevels = [
31 "None",
32 "Enabled"
33 ];
34
Skyler Greyf21323a2022-08-13 23:58:22 +010035 const beforeOwner = await before.fetchOwner();
36 const afterOwner = await after.fetchOwner();
pineafan63fc5e22022-08-04 22:04:10 +010037
pineafan0f5cc782022-08-12 21:55:42 +010038 if (before.name !== after.name) list["name"] = entry([before.name, after.name], `${before.name} -> ${after.name}`);
Skyler Grey75ea9172022-08-06 10:22:23 +010039 if (before.icon !== after.icon)
TheCodedProfa16d1672023-01-18 18:58:34 -050040 list["icon"] = entry([before.icon!, after.icon!], `[Before](${before.iconURL()}) -> [After](${after.iconURL()})`);
Skyler Grey75ea9172022-08-06 10:22:23 +010041 if (before.splash !== after.splash)
pineafan0f5cc782022-08-12 21:55:42 +010042 list["splash"] = entry(
TheCodedProfa16d1672023-01-18 18:58:34 -050043 [before.splash!, after.splash!],
Skyler Grey75ea9172022-08-06 10:22:23 +010044 `[Before](${before.splashURL()}) -> [After](${after.splashURL()})`
45 );
46 if (before.banner !== after.banner)
pineafan0f5cc782022-08-12 21:55:42 +010047 list["banner"] = entry(
TheCodedProfa16d1672023-01-18 18:58:34 -050048 [before.banner!, after.banner!],
Skyler Grey75ea9172022-08-06 10:22:23 +010049 `[Before](${before.bannerURL()}) -> [After](${after.bannerURL()})`
50 );
pineafan0f5cc782022-08-12 21:55:42 +010051 if (beforeOwner !== afterOwner)
52 list["owner"] = entry(
TheCodedProfa16d1672023-01-18 18:58:34 -050053 [beforeOwner.id, afterOwner.id],
pineafan0f5cc782022-08-12 21:55:42 +010054 `${renderUser(beforeOwner.user)} -> ${renderUser(afterOwner.user)}`
Skyler Grey75ea9172022-08-06 10:22:23 +010055 );
56 if (before.verificationLevel !== after.verificationLevel)
pineafan0f5cc782022-08-12 21:55:42 +010057 list["verificationLevel"] = entry(
TheCodedProfa16d1672023-01-18 18:58:34 -050058 [verificationLevels[before.verificationLevel.valueOf()]!, verificationLevels[before.verificationLevel.valueOf()]!],
59 `${verificationLevels[before.verificationLevel.valueOf()]} -> ${verificationLevels[before.verificationLevel.valueOf()]}`
Skyler Grey75ea9172022-08-06 10:22:23 +010060 );
61 if (before.explicitContentFilter !== after.explicitContentFilter)
pineafan0f5cc782022-08-12 21:55:42 +010062 list["explicitContentFilter"] = entry(
Skyler Grey75ea9172022-08-06 10:22:23 +010063 [
TheCodedProfa16d1672023-01-18 18:58:34 -050064 explicitContentFilterLevels[before.explicitContentFilter.valueOf()]!,
65 explicitContentFilterLevels[after.explicitContentFilter.valueOf()]!
Skyler Grey75ea9172022-08-06 10:22:23 +010066 ],
67 `${explicitContentFilterLevels[before.explicitContentFilter]} -> ${
68 explicitContentFilterLevels[after.explicitContentFilter]
69 }`
70 );
71 if (before.mfaLevel !== after.mfaLevel)
72 list["2 factor authentication"] = entry(
TheCodedProfa16d1672023-01-18 18:58:34 -050073 [MFALevels[before.mfaLevel.valueOf()]!, MFALevels[after.mfaLevel.valueOf()]!],
74 `${MFALevels[before.mfaLevel.valueOf()]} -> ${MFALevels[after.mfaLevel.valueOf()]}`
Skyler Grey75ea9172022-08-06 10:22:23 +010075 );
pineafan63fc5e22022-08-04 22:04:10 +010076
Skyler Grey75ea9172022-08-06 10:22:23 +010077 if (!Object.keys(list).length) return;
Samuel Shuert27bf3cd2023-03-03 15:51:25 -050078 list["updated"] = entry(Date.now(), renderDelta(Date.now()));
TheCodedProfa16d1672023-01-18 18:58:34 -050079 list["updatedBy"] = entry(auditLog.executor!.id, renderUser(auditLog.executor!));
pineafan63fc5e22022-08-04 22:04:10 +010080 const data = {
81 meta: {
82 type: "guildUpdate",
PineaFan538d3752023-01-12 21:48:23 +000083 displayName: "Server Edited",
pineafan63fc5e22022-08-04 22:04:10 +010084 calculateType: "guildUpdate",
85 color: NucleusColors.yellow,
86 emoji: "GUILD.YELLOW",
Samuel Shuert27bf3cd2023-03-03 15:51:25 -050087 timestamp: Date.now()
pineafan63fc5e22022-08-04 22:04:10 +010088 },
89 list: list,
90 hidden: {
91 guild: after.id
pineafane625d782022-05-09 18:04:32 +010092 }
pineafan63fc5e22022-08-04 22:04:10 +010093 };
94 log(data);
pineafane625d782022-05-09 18:04:32 +010095}