blob: dbd747fee047fc99b266b60597e18f1a52981932 [file] [log] [blame]
import type { NucleusClient } from "../utils/client.js";
import { AuditLogEvent, Guild, GuildAuditLogsEntry } from "discord.js";
import { callback as statsChannelUpdate } from "../reflex/statsChannelUpdate.js";
export const event = "guildUpdate";
export async function callback(client: NucleusClient, before: Guild, after: Guild) {
await statsChannelUpdate(client, after.members.me!);
const { getAuditLog, isLogging, log, NucleusColors, entry, renderUser, renderDelta } = client.logger;
if (!(await isLogging(after.id, "guildUpdate"))) return;
const auditLog = (await getAuditLog(after, AuditLogEvent.GuildUpdate)).filter(
(entry: GuildAuditLogsEntry) => (entry.target as Guild)!.id === after.id
)[0]!;
if (auditLog.executor!.id === client.user!.id) return;
const list: Record<string, ReturnType<typeof entry>> = {};
const verificationLevels = [
"Unrestricted",
"Verified email",
"Registered (5 minutes)",
"Member (10 minutes)",
"Verified phone"
];
const explicitContentFilterLevels = ["Disabled", "Members without roles", "All members"];
const MFALevels = ["None", "Enabled"];
const beforeOwner = await before.fetchOwner();
const afterOwner = await after.fetchOwner();
if (before.name !== after.name) list["name"] = entry([before.name, after.name], `${before.name} -> ${after.name}`);
if (before.icon !== after.icon)
list["icon"] = entry(
[before.icon!, after.icon!],
`[Before](${before.iconURL()}) -> [After](${after.iconURL()})`
);
if (before.splash !== after.splash)
list["splash"] = entry(
[before.splash!, after.splash!],
`[Before](${before.splashURL()}) -> [After](${after.splashURL()})`
);
if (before.banner !== after.banner)
list["banner"] = entry(
[before.banner!, after.banner!],
`[Before](${before.bannerURL()}) -> [After](${after.bannerURL()})`
);
if (beforeOwner !== afterOwner)
list["owner"] = entry(
[beforeOwner.id, afterOwner.id],
`${renderUser(beforeOwner.user)} -> ${renderUser(afterOwner.user)}`
);
if (before.verificationLevel !== after.verificationLevel)
list["verificationLevel"] = entry(
[
verificationLevels[before.verificationLevel.valueOf()]!,
verificationLevels[before.verificationLevel.valueOf()]!
],
`${verificationLevels[before.verificationLevel.valueOf()]} -> ${
verificationLevels[before.verificationLevel.valueOf()]
}`
);
if (before.explicitContentFilter !== after.explicitContentFilter)
list["explicitContentFilter"] = entry(
[
explicitContentFilterLevels[before.explicitContentFilter.valueOf()]!,
explicitContentFilterLevels[after.explicitContentFilter.valueOf()]!
],
`${explicitContentFilterLevels[before.explicitContentFilter]} -> ${
explicitContentFilterLevels[after.explicitContentFilter]
}`
);
if (before.mfaLevel !== after.mfaLevel)
list["2 factor authentication"] = entry(
[MFALevels[before.mfaLevel.valueOf()]!, MFALevels[after.mfaLevel.valueOf()]!],
`${MFALevels[before.mfaLevel.valueOf()]} -> ${MFALevels[after.mfaLevel.valueOf()]}`
);
if (!Object.keys(list).length) return;
list["updated"] = entry(Date.now(), renderDelta(Date.now()));
list["updatedBy"] = entry(auditLog.executor!.id, renderUser(auditLog.executor!));
const data = {
meta: {
type: "guildUpdate",
displayName: "Server Edited",
calculateType: "guildUpdate",
color: NucleusColors.yellow,
emoji: "GUILD.YELLOW",
timestamp: Date.now()
},
list: list,
hidden: {
guild: after.id
}
};
log(data);
}