logging: more cleanup and consistency

This commit is contained in:
Cynthia Foxwell 2025-05-13 11:17:30 -06:00
parent 2b75ef0ec2
commit 1d891b5932
Signed by: Cynosphere
SSH key fingerprint: SHA256:H3SM8ufP/uxqLwKSH7xY89TDnbR9uOHzjLoBr0tlajk

View file

@ -24,7 +24,7 @@ async function getLoggingChannel(guild) {
return guild.channels.get(channelId) ?? guild.threads.get(channelId); return guild.channels.get(channelId) ?? guild.threads.get(channelId);
} }
function formatNameChange(before, after) { function formatChange(before, after) {
const beforeFormatted = before != null ? before : "<no value>"; const beforeFormatted = before != null ? before : "<no value>";
const afterFormatted = after != null ? after : "<no value>"; const afterFormatted = after != null ? after : "<no value>";
@ -147,14 +147,6 @@ events.add("guildAuditLogEntryCreate", "logging", async function (entry) {
for (const [key, value] of Object.entries(isCreate ? after : before)) { for (const [key, value] of Object.entries(isCreate ? after : before)) {
switch (key) { switch (key) {
case "name": {
fields.push({
name: "Name",
value: `#${value}`,
inline: true,
});
break;
}
case "type": { case "type": {
const typeName = ChannelTypeNames[value]; const typeName = ChannelTypeNames[value];
fields.push({ fields.push({
@ -213,14 +205,14 @@ events.add("guildAuditLogEntryCreate", "logging", async function (entry) {
case "name": case "name":
fields.push({ fields.push({
name: "Name", name: "Name",
value: formatNameChange(oldValue, newValue), value: formatChange(oldValue, newValue),
inline: true, inline: true,
}); });
break; break;
case "description": case "description":
fields.push({ fields.push({
name: "Description", name: "Description",
value: formatNameChange(oldValue, newValue), value: formatChange(oldValue, newValue),
inline: true, inline: true,
}); });
break; break;
@ -228,8 +220,8 @@ events.add("guildAuditLogEntryCreate", "logging", async function (entry) {
const oldIconURL = CDNEndpoints.GUILD_ICON(entry.targetID, oldValue); const oldIconURL = CDNEndpoints.GUILD_ICON(entry.targetID, oldValue);
const newIconURL = CDNEndpoints.GUILD_ICON(entry.targetID, newValue); const newIconURL = CDNEndpoints.GUILD_ICON(entry.targetID, newValue);
const oldIconFormatted = `[Old](${oldIconURL})`; const oldIconFormatted = oldValue != null ? `[Old](${oldIconURL})` : "None";
const newIconFormatted = `[New](${newIconURL})`; const newIconFormatted = newValue != null ? `[New](${newIconURL})` : "None";
fields.push({ fields.push({
name: "Icon", name: "Icon",
@ -237,15 +229,15 @@ events.add("guildAuditLogEntryCreate", "logging", async function (entry) {
inline: true, inline: true,
}); });
thumbnail = newIconURL; thumbnail = newValue ? newIconURL : null;
break; break;
} }
case "banner_hash": { case "banner_hash": {
const oldBannerURL = CDNEndpoints.BANNER(entry.targetID, oldValue); const oldBannerURL = CDNEndpoints.BANNER(entry.targetID, oldValue);
const newBannerURL = CDNEndpoints.BANNER(entry.targetID, newValue); const newBannerURL = CDNEndpoints.BANNER(entry.targetID, newValue);
const oldBannerFormatted = `[Old](${oldBannerURL})`; const oldBannerFormatted = oldValue != null ? `[Old](${oldBannerURL})` : "None";
const newBannerFormated = `[New](${newBannerURL})`; const newBannerFormated = newValue != null ? `[New](${newBannerURL})` : "None";
fields.push({ fields.push({
name: "Banner", name: "Banner",
@ -253,13 +245,17 @@ events.add("guildAuditLogEntryCreate", "logging", async function (entry) {
inline: true, inline: true,
}); });
image = newBannerFormated; image = newValue ? newBannerURL : null;
break; break;
} }
case "system_channel_id": { case "system_channel_id": {
fields.push({ fields.push({
name: "System Channel", name: "System Channel",
value: `${oldValue ?? "<no value>"} -> ${newValue}`, value: `${
oldValue != null
? `<#${oldValue}> (${entry.guild.channels.get(oldValue)?.name ?? "<uncached>"})`
: "None"
} -> <#${newValue}> (${entry.guild.channels.get(newValue)?.name ?? "<uncached>"})`,
inline: true, inline: true,
}); });
break; break;
@ -293,7 +289,7 @@ events.add("guildAuditLogEntryCreate", "logging", async function (entry) {
fields.push({ fields.push({
name: "Updated by", name: "Updated by",
value: `<@${entry.user.id}>`, value: `<@${entry.user.id}> (${formatUsername(entry.user)})`,
inline: false, inline: false,
}); });
@ -304,13 +300,13 @@ events.add("guildAuditLogEntryCreate", "logging", async function (entry) {
title: `Server updated`, title: `Server updated`,
fields, fields,
thumbnail: thumbnail:
thumbnail != undefined thumbnail != null
? { ? {
url: thumbnail, url: thumbnail,
} }
: null, : null,
image: image:
image != undefined image != null
? { ? {
url: image, url: image,
} }
@ -341,7 +337,7 @@ events.add("guildAuditLogEntryCreate", "logging", async function (entry) {
fields.push({ fields.push({
name: "Visibility", name: "Visibility",
value: formatNameChange(oldVisibility, newVisibility), value: formatChange(oldVisibility, newVisibility),
inline: true, inline: true,
}); });
} }
@ -350,7 +346,7 @@ events.add("guildAuditLogEntryCreate", "logging", async function (entry) {
fields.push({ fields.push({
name: "Updated by", name: "Updated by",
value: `<@${entry.user.id}>`, value: `<@${entry.user.id}> (${formatUsername(entry.user)})`,
inline: true, inline: true,
}); });
@ -364,7 +360,7 @@ events.add("guildAuditLogEntryCreate", "logging", async function (entry) {
embeds: [ embeds: [
{ {
color: COLOR_CHANGED, color: COLOR_CHANGED,
title: `Server Profile updated`, title: `Server Profile Updated`,
fields, fields,
footer: { footer: {
text: `Guild ID: ${entry.targetID}`, text: `Guild ID: ${entry.targetID}`,
@ -388,7 +384,7 @@ events.add("guildAuditLogEntryCreate", "logging", async function (entry) {
if (oldNickname != newNickname) { if (oldNickname != newNickname) {
fields.push({ fields.push({
name: "Nickname", name: "Nickname",
value: formatNameChange(oldNickname, newNickname), value: formatChange(oldNickname, newNickname),
inline: true, inline: true,
}); });
} }
@ -399,20 +395,31 @@ events.add("guildAuditLogEntryCreate", "logging", async function (entry) {
const timeoutDuration = new Date(timeout); const timeoutDuration = new Date(timeout);
fields.push({ fields.push({
name: isTimedOut ? "Timed out" : "Was timed out until", name: isTimedOut ? "Timed out until" : "Was timed out until",
value: `<t:${Math.round(timeoutDuration.getTime() / 1000)}>`, value: `<t:${Math.round(timeoutDuration.getTime() / 1000)}>`,
inline: true, inline: true,
}); });
} }
let verb = "updated";
if (after.bypasses_verification) {
verb = "bypassed verification for";
} else if (after.communication_disabled_until != null) {
verb = "timed out";
} else if (before.communication_disabled_until != null) {
verb = "cleared timeout for";
}
channel.createMessage({ channel.createMessage({
embeds: [ embeds: [
{ {
color: COLOR_CHANGED, color: COLOR_CHANGED,
title: `Member updated`, title: `Member Updated`,
description: `<@${entry.user.id}> (${formatUsername(entry.user)}) updated member <@${ description: `<@${entry.user.id}> (${formatUsername(entry.user)}) ${verb} ${
entry.targetID entry.targetID === entry.user.id
}> (${formatUsername(target)})`, ? "themselves"
: `member <@${entry.targetID}> (${formatUsername(target)})`
}`,
fields, fields,
footer: { footer: {
text: `User ID: ${entry.targetID}`, text: `User ID: ${entry.targetID}`,
@ -424,11 +431,11 @@ events.add("guildAuditLogEntryCreate", "logging", async function (entry) {
break; break;
} }
case AuditLogActions.MEMBER_ROLE_UPDATE: { case AuditLogActions.MEMBER_ROLE_UPDATE: {
const isAdd = entry.after.$add != null; const isAdd = after.$add != null;
const isSelf = entry.user.id === entry.targetID; const isSelf = entry.user.id === entry.targetID;
const added = entry.after.$add; const added = after.$add;
const removed = entry.after.$remove; const removed = after.$remove;
let roles = added ?? removed; let roles = added ?? removed;
const addAndRemove = added != null && removed != null; const addAndRemove = added != null && removed != null;
@ -436,128 +443,122 @@ events.add("guildAuditLogEntryCreate", "logging", async function (entry) {
const plural = roles.length > 1 ? "s" : ""; const plural = roles.length > 1 ? "s" : "";
channel channel.createMessage({
.createMessage({ embeds: [
embeds: [ {
{ color: addAndRemove ? COLOR_CHANGED : isAdd ? COLOR_ADDED : COLOR_REMOVED,
color: addAndRemove ? COLOR_CHANGED : isAdd ? COLOR_ADDED : COLOR_REMOVED, title: `Member Role${plural} Updated`,
title: `Member Role${plural} Updated`, description: `<@${entry.user.id}> (${formatUsername(entry.user)}) ${
description: `<@${entry.user.id}> (${formatUsername(entry.user)}) ${ addAndRemove ? "added and removed" : isAdd ? "added" : "removed"
addAndRemove ? "added and removed" : isAdd ? "added" : "removed" } ${roles.length > 1 ? `${roles.length} ` : ""}role${plural} ${added ? "to" : "from"} ${
} ${roles.length > 1 ? `${roles.length} ` : ""}role${plural} ${added ? "to" : "from"} ${ isSelf ? "self" : `<@${entry.targetID}> (${formatUsername(entry.target.user)})`
isSelf ? "self" : `<@${entry.targetID}> (${formatUsername(entry.target.user)})` }`,
}`, timestamp: new Date().toISOString(),
timestamp: new Date().toISOString(), fields: [
fields: [ !addAndRemove && {
!addAndRemove && { name: `Role${plural}`,
name: `Role${plural}`, value: roles.map((role) => `<@&${role.id}> (${role.name})`).join("\n"),
value: roles.map((role) => `<@&${role.id}> (${role.name})`).join("\n"),
},
addAndRemove && {
name: "Added",
value: added.map((role) => `<@&${role.id}> (${role.name})`).join("\n"),
inline: true,
},
addAndRemove && {
name: "Removed",
value: removed.map((role) => `<@&${role.id}> (${role.name})`).join("\n"),
inline: true,
},
entry.reason != null && {
name: "Reason",
value: `\`${entry.reason.replaceAll("`", "\u02cb")}\``,
},
].filter((x) => !!x),
footer: {
text: `Role ID${plural}: ${roles.map((role) => role.id).join(", ")}`,
}, },
addAndRemove && {
name: "Added",
value: added.map((role) => `<@&${role.id}> (${role.name})`).join("\n"),
inline: true,
},
addAndRemove && {
name: "Removed",
value: removed.map((role) => `<@&${role.id}> (${role.name})`).join("\n"),
inline: true,
},
entry.reason != null && {
name: "Reason",
value: `\`${entry.reason.replaceAll("`", "\u02cb")}\``,
},
].filter((x) => !!x),
footer: {
text: `Role ID${plural}: ${roles.map((role) => role.id).join(", ")}`,
}, },
], },
}) ],
.catch(() => {}); });
break; break;
} }
case AuditLogActions.WEBHOOK_CREATE: { case AuditLogActions.WEBHOOK_CREATE: {
channel channel.createMessage({
.createMessage({ embeds: [
embeds: [ {
{ color: COLOR_ADDED,
color: COLOR_ADDED, title: `${after.application_id != null ? "Application " : ""}Webhook Created`,
title: `${entry.after.application_id != null ? "Application " : ""}Webhook Created`, description: `<@${entry.user.id}> (${formatUsername(
description: `<@${entry.user.id}> (${formatUsername( entry.user
entry.user )}) created webhook \`${after.name.replaceAll("`", "\u02cb")}\` in <#${entry.after.channel_id}> (${
)}) created webhook \`${entry.after.name.replaceAll("`", "\u02cb")}\` in <#${ entry.guild.channels.get(after.channel_id)?.name ?? "<uncached>"
entry.after.channel_id })`,
}> (${entry.guild.channels.get(entry.after.channel_id)?.name ?? "<uncached>"})`, fields: [
fields: [ after.application_id != null && {
entry.after.application_id != null && { name: "Application ID",
name: "Application ID", value: after.application_id,
value: entry.after.application_id, inline: true,
inline: true,
},
entry.reason != null && {
name: "Reason",
value: `\`${entry.reason.replaceAll("`", "\u02cb")}\``,
},
].filter((x) => !!x),
footer: {
text: `Webhook ID: ${entry.targetID}`,
}, },
thumbnail: entry.reason != null && {
entry.after.avatar_hash != null name: "Reason",
? { value: `\`${entry.reason.replaceAll("`", "\u02cb")}\``,
url: `https://cdn.discordapp.com/avatars/${entry.targetID}/${entry.after.avatar_hash}.png?size=4096`, },
} ].filter((x) => !!x),
: null, footer: {
text: `Webhook ID: ${entry.targetID}`,
}, },
], thumbnail:
}) after.avatar_hash != null
.catch(() => {}); ? {
url: `https://cdn.discordapp.com/avatars/${entry.targetID}/${after.avatar_hash}.png?size=4096`,
}
: null,
},
],
});
break; break;
} }
case AuditLogActions.THREAD_CREATE: { case AuditLogActions.THREAD_CREATE: {
channel channel.createMessage({
.createMessage({ embeds: [
embeds: [ {
{ color: COLOR_ADDED,
color: COLOR_ADDED, title: `${after.invitable != null ? "Private " : ""}Thread Created`,
title: `${entry.after.invitable != null ? "Private " : ""}Thread Created`, description: `<#${entry.targetID}> (${after.name})`,
description: `<#${entry.targetID}> (${entry.after.name})`, timestamp: new Date().toISOString(),
timestamp: new Date().toISOString(), fields: [
fields: [ {
{ name: "Created by",
name: "Created by", value: `<@${entry.user.id}> (${formatUsername(entry.user)})`,
value: `<@${entry.user.id}> (${formatUsername(entry.user)})`, inline: true,
inline: true,
},
entry.target?.parentID != null && {
name: "Parent Channel",
value: `<#${entry.target.parentID}> (${
entry.guild.channels.get(entry.target.parentID)?.name ?? "<uncached>"
})`,
inline: true,
},
entry.reason != null && {
name: "Reason",
value: `\`${entry.reason.replaceAll("`", "\u02cb")}\``,
},
].filter((x) => !!x),
footer: {
text: `Thread ID: ${entry.targetID}`,
}, },
entry.target?.parentID != null && {
name: "Parent Channel",
value: `<#${entry.target.parentID}> (${
entry.guild.channels.get(entry.target.parentID)?.name ?? "<uncached>"
})`,
inline: true,
},
entry.reason != null && {
name: "Reason",
value: `\`${entry.reason.replaceAll("`", "\u02cb")}\``,
},
].filter((x) => !!x),
footer: {
text: `Thread ID: ${entry.targetID}`,
}, },
], },
}) ],
.catch(() => {}); });
break; break;
} }
case AuditLogActions.THREAD_UPDATE: { case AuditLogActions.THREAD_UPDATE: {
const parentChannel = entry.guild.channels.get(entry.target.parentID); const parentChannel = entry.guild.channels.get(entry.target.parentID);
let tagDiff; let tagDiff;
if (entry.after.applied_tags != null) { if (after.applied_tags != null) {
const newTags = new Set(entry.after.applied_tags); const newTags = new Set(after.applied_tags);
const oldTags = new Set(entry.before.applied_tags ?? []); const oldTags = new Set(before.applied_tags ?? []);
let addedTags = Array.from(newTags.difference(oldTags)); let addedTags = Array.from(newTags.difference(oldTags));
let removedTags = Array.from(oldTags.difference(newTags)); let removedTags = Array.from(oldTags.difference(newTags));
@ -587,104 +588,103 @@ events.add("guildAuditLogEntryCreate", "logging", async function (entry) {
tagDiff += "```"; tagDiff += "```";
} }
channel let verb = "updated";
.createMessage({ if (before.locked === true && after.locked === false) {
embeds: [ verb = "unlocked";
{ } else if (before.locked === false && after.locked === true) {
color: COLOR_CHANGED, verb = "locked";
title: `${ } else if (before.archived === true && after.archived === false) {
entry.target != null && entry.target instanceof PrivateThreadChannel ? "Private " : "" verb = "unarchived";
}Thread Updated`, } else if (before.archived === false && after.archived === true) {
description: `<#${entry.targetID}>${entry.target?.name != null ? ` (${entry.target.name})` : ""} ${ verb = "archived";
entry.before.locked === true && entry.after.locked === false } else if (before.invitable === false && after.invitable === true) {
? "unlocked" verb = "enabled mention inviting for";
: entry.before.locked === false && entry.after.locked === true } else if (before.invitable === true && after.invitable === false) {
? "locked" verb = "disabled mention inviting for";
: entry.before.archived === true && entry.after.archived === false }
? "unarchived"
: entry.before.archived === false && entry.after.archived === true channel.createMessage({
? "archived" embeds: [
: entry.before.invitable === false && entry.after.invitable === true {
? "mention inviting enabled" color: COLOR_CHANGED,
: entry.before.invitable === true && entry.after.invitable === false title: `${
? "mention inviting disabled" entry.target != null && entry.target instanceof PrivateThreadChannel ? "Private " : ""
: "updated" }Thread Updated`,
} by <@${entry.user.id}> (${formatUsername(entry.user)})`, description: `<@${entry.user.id}> (${formatUsername(entry.user)}) ${verb} <#${entry.targetID}>${
timestamp: new Date().toISOString(), entry.target?.name != null ? ` (${entry.target.name})` : ""
fields: [ }`,
entry.after.name != null && { timestamp: new Date().toISOString(),
name: "Name", fields: [
value: `\`${entry.before.name}\` -> \`${entry.after.name}\``, after.name != null && {
inline: true, name: "Name",
}, value: formatChange(before.name, after.name),
entry.target?.parentID != null && { inline: true,
name: "Parent Channel",
value: `<#${entry.target.parentID}> (${parentChannel?.name ?? "<uncached>"})`,
inline: true,
},
entry.after.auto_archive_duration != null && {
name: "Hide After Inactivity",
value: `\`${entry.before.auto_archive_duration}\` -> \`${entry.after.auto_archive_duration}\``,
inline: true,
},
entry.after.rate_limit_per_user != null && {
name: "Slowmode",
value: `\`${entry.before.rate_limit_per_user}\` -> \`${entry.after.rate_limit_per_user}\``,
inline: true,
},
tagDiff != null && {
name: "Tags",
value: tagDiff,
inline: false,
},
entry.reason != null && {
name: "Reason",
value: `\`${entry.reason.replaceAll("`", "\u02cb")}\``,
},
].filter((x) => !!x),
footer: {
text: `Thread ID: ${entry.targetID}`,
}, },
entry.target?.parentID != null && {
name: "Parent Channel",
value: `<#${entry.target.parentID}> (${parentChannel?.name ?? "<uncached>"})`,
inline: true,
},
entry.after.auto_archive_duration != null && {
name: "Hide After Inactivity",
value: `\`${entry.before.auto_archive_duration}\` -> \`${entry.after.auto_archive_duration}\``,
inline: true,
},
entry.after.rate_limit_per_user != null && {
name: "Slowmode",
value: `\`${entry.before.rate_limit_per_user}\` -> \`${entry.after.rate_limit_per_user}\``,
inline: true,
},
tagDiff != null && {
name: "Tags",
value: tagDiff,
inline: false,
},
entry.reason != null && {
name: "Reason",
value: `\`${entry.reason.replaceAll("`", "\u02cb")}\``,
},
].filter((x) => !!x),
footer: {
text: `Thread ID: ${entry.targetID}`,
}, },
], },
}) ],
.catch(() => {}); });
break; break;
} }
case AuditLogActions.THREAD_DELETE: { case AuditLogActions.THREAD_DELETE: {
channel channel.createMessage({
.createMessage({ embeds: [
embeds: [ {
{ color: COLOR_REMOVED,
color: COLOR_REMOVED, title: `${entry.before.invitable != null ? "Private " : ""}Thread Deleted`,
title: `${entry.before.invitable != null ? "Private " : ""}Thread Deleted`, description: `<#${entry.targetID}> (${entry.before.name})`,
description: `<#${entry.targetID}> (${entry.before.name})`, timestamp: new Date().toISOString(),
timestamp: new Date().toISOString(), fields: [
fields: [ {
{ name: "Deleted by",
name: "Deleted by", value: `<@${entry.user.id}> (${formatUsername(entry.user)})`,
value: `<@${entry.user.id}> (${formatUsername(entry.user)})`, inline: true,
inline: true,
},
entry.target?.parentID != null && {
name: "Parent Channel",
value: `<#${entry.target.parentID}> (${
entry.guild.channels.get(entry.target.parentID)?.name ?? "<uncached>"
})`,
inline: true,
},
entry.reason != null && {
name: "Reason",
value: `\`${entry.reason.replaceAll("`", "\u02cb")}\``,
},
].filter((x) => !!x),
footer: {
text: `Thread ID: ${entry.targetID}`,
}, },
entry.target?.parentID != null && {
name: "Parent Channel",
value: `<#${entry.target.parentID}> (${
entry.guild.channels.get(entry.target.parentID)?.name ?? "<uncached>"
})`,
inline: true,
},
entry.reason != null && {
name: "Reason",
value: `\`${entry.reason.replaceAll("`", "\u02cb")}\``,
},
].filter((x) => !!x),
footer: {
text: `Thread ID: ${entry.targetID}`,
}, },
], },
}) ],
.catch(() => {}); });
break; break;
} }
} }