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