diff --git a/src/modules/logging.js b/src/modules/logging.js index 0c3d361..0710346 100644 --- a/src/modules/logging.js +++ b/src/modules/logging.js @@ -4,14 +4,8 @@ const logger = require("#lib/logger.js"); const {getGuildData} = require("#lib/guildData.js"); const events = require("#lib/events.js"); -const { - AuditLogActions, - APIEndpoints, - CDNEndpoints, - GuildIntegrationTypes, - GuildProfileVisibility, -} = require("#util/dconstants.js"); -const {JoinSourceTypeNames, ChannelTypeNames} = require("#util/constants.js"); +const {AuditLogActions, APIEndpoints, CDNEndpoints, GuildIntegrationTypes} = require("#util/dconstants.js"); +const {JoinSourceTypeNames} = require("#util/constants.js"); const {formatUsername, getDefaultAvatar} = require("#util/misc.js"); const {snowflakeToTimestamp} = require("#util/time.js"); @@ -24,13 +18,6 @@ async function getLoggingChannel(guild) { return guild.channels.get(channelId) ?? guild.threads.get(channelId); } -function formatNameChange(before, after) { - const beforeFormatted = before != null ? before : ""; - const afterFormatted = after != null ? after : ""; - - return `\`${beforeFormatted}\` -> \`${afterFormatted}\``; -} - const WHITELISTED_EVENTS = new Set([ "GUILD_UPDATE", "CHANNEL_CREATE", @@ -86,17 +73,18 @@ events.add("guildAuditLogEntryCreate", "logging", async function (entry) { if (!channel) return; try { - const before = entry.before; - const after = entry.after; - switch (entry.actionType) { + case AuditLogActions.GUILD_UPDATE: + case AuditLogActions.CHANNEL_CREATE: case AuditLogActions.CHANNEL_UPDATE: + case AuditLogActions.CHANNEL_DELETE: case AuditLogActions.CHANNEL_OVERWRITE_CREATE: case AuditLogActions.CHANNEL_OVERWRITE_UPDATE: case AuditLogActions.CHANNEL_OVERWRITE_DELETE: case AuditLogActions.MEMBER_KICK: case AuditLogActions.MEMBER_BAN_ADD: case AuditLogActions.MEMBER_BAN_REMOVE: + case AuditLogActions.MEMBER_UPDATE: case AuditLogActions.BOT_ADD: case AuditLogActions.ROLE_CREATE: case AuditLogActions.ROLE_UPDATE: @@ -126,7 +114,8 @@ events.add("guildAuditLogEntryCreate", "logging", async function (entry) { case AuditLogActions.CLYDE_AI_PROFILE_UPDATE: case AuditLogActions.GUILD_SCHEDULED_EVENT_EXCEPTION_CREATE: case AuditLogActions.GUILD_SCHEDULED_EVENT_EXCEPTION_UPDATE: - case AuditLogActions.GUILD_SCHEDULED_EVENT_EXCEPTION_DELETE: { + case AuditLogActions.GUILD_SCHEDULED_EVENT_EXCEPTION_DELETE: + case AuditLogActions.GUILD_PROFILE_UPDATE: { channel .createMessage({ content: Object.entries(AuditLogActions).find(([name, val]) => val === entry.actionType)[0], @@ -140,289 +129,6 @@ events.add("guildAuditLogEntryCreate", "logging", async function (entry) { .catch(() => {}); break; } - case AuditLogActions.CHANNEL_DELETE: - case AuditLogActions.CHANNEL_CREATE: { - const fields = []; - const isCreate = entry.actionType == AuditLogActions.CHANNEL_CREATE; - - 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({ - name: "Type", - value: typeName, - inline: true, - }); - break; - } - case "nsfw": { - fields.push({ - name: "NSFW", - value: value == true ? "Yes" : "No", - inline: true, - }); - break; - } - } - } - - if (entry.reason != null) - fields.push({ - name: "Reason", - value: `\`${entry.reason.replaceAll("`", "\u02cb")}\``, - }); - - channel.createMessage({ - embeds: [ - { - color: isCreate ? COLOR_ADDED : COLOR_REMOVED, - title: `Channel ${isCreate ? "Created" : "Deleted"}`, - description: `<@${entry.user.id}> (${formatUsername(entry.user)}) ${isCreate ? "created" : "deleted"} <#${ - entry.targetID - }>${entry.target != null ? ` (${entry.target.name})` : ""}`, - fields, - footer: { - text: `Channel ID: ${entry.targetID}`, - }, - timestamp: new Date().toISOString(), - }, - ], - }); - - break; - } - case AuditLogActions.GUILD_UPDATE: { - const fields = []; - - let thumbnail; - let image; - - for (const [key, newValue] of Object.entries(after)) { - const oldValue = before[key]; - - switch (key) { - case "name": - fields.push({ - name: "Name", - value: formatNameChange(oldValue, newValue), - inline: true, - }); - break; - case "description": - fields.push({ - name: "Description", - value: formatNameChange(oldValue, newValue), - inline: true, - }); - break; - case "icon_hash": { - const oldIconURL = CDNEndpoints.GUILD_ICON(entry.targetID, oldValue); - const newIconURL = CDNEndpoints.GUILD_ICON(entry.targetID, newValue); - - const oldIconFormatted = `[Old](${oldIconURL})`; - const newIconFormatted = `[New](${newIconURL})`; - - fields.push({ - name: "Icon", - value: `${oldIconFormatted} -> ${newIconFormatted}`, - inline: true, - }); - - thumbnail = newIconURL; - 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})`; - - fields.push({ - name: "Banner", - value: `${oldBannerFormatted} -> ${newBannerFormated}`, - inline: true, - }); - - image = newBannerFormated; - break; - } - case "system_channel_id": { - fields.push({ - name: "System Channel", - value: `${oldValue ?? ""} -> ${newValue}`, - inline: true, - }); - break; - } - case "widget_enabled": { - fields.push({ - name: "Widget", - value: newValue == true ? "Enabled" : "Disabled", - inline: true, - }); - break; - } - case "max_members": { - fields.push({ - name: "Max Members", - value: newValue, - inline: true, - }); - break; - } - default: { - fields.push({ - name: key, - value: `\`${oldValue}\` -> \`${newValue}\``, - inline: true, - }); - break; - } - } - } - - fields.push({ - name: "Updated by", - value: `<@${entry.user.id}>`, - inline: false, - }); - - channel.createMessage({ - embeds: [ - { - color: COLOR_CHANGED, - title: `Server updated`, - fields, - thumbnail: - thumbnail != undefined - ? { - url: thumbnail, - } - : null, - image: - image != undefined - ? { - url: image, - } - : null, - footer: { - text: `Guild ID: ${entry.targetID}`, - }, - timestamp: new Date().toISOString(), - }, - ], - }); - - break; - } - case AuditLogActions.GUILD_PROFILE_UPDATE: { - const fields = []; - - for (const [key, newValue] of Object.entries(after)) { - const oldValue = before[key]; - switch (key) { - case "visibility": { - const newVisibility = GuildProfileVisibility[newValue] - ? GuildProfileVisibility[newValue] - : `Unknown ${newValue}`; - const oldVisibility = GuildProfileVisibility[oldValue] - ? GuildProfileVisibility[oldValue] - : `Unknown ${oldValue}`; - - fields.push({ - name: "Visibility", - value: formatNameChange(oldVisibility, newVisibility), - inline: true, - }); - } - } - } - - fields.push({ - name: "Updated by", - value: `<@${entry.user.id}>`, - inline: true, - }); - - if (entry.reason != null) - fields.push({ - name: "Reason", - value: `\`${entry.reason.replaceAll("`", "\u02cb")}\``, - }); - - channel.createMessage({ - embeds: [ - { - color: COLOR_CHANGED, - title: `Server Profile updated`, - fields, - footer: { - text: `Guild ID: ${entry.targetID}`, - }, - timestamp: new Date().toISOString(), - }, - ], - }); - - break; - } - case AuditLogActions.MEMBER_UPDATE: { - const fields = []; - - const bot = hf.bot; - const target = bot.users.get(entry.targetID); - - const oldNickname = before?.nick; - const newNickname = after?.nick; - - const isTimedOut = after?.communication_disabled_until != undefined; - - if (oldNickname != newNickname) { - fields.push({ - name: "Nickname", - value: formatNameChange(oldNickname, newNickname), - inline: true, - }); - } - - const timeoutDuration = new Date( - isTimedOut ? after?.communication_disabled_until : before?.communication_disabled_until - ); - - fields.push({ - name: isTimedOut ? "Timed out" : "Was timed out until", - value: ``, - inline: true, - }); - - channel.createMessage({ - embeds: [ - { - color: COLOR_CHANGED, - title: `Member updated`, - description: `<@${entry.user.id}> (${formatUsername(entry.user)}) updated member <@${ - entry.targetID - }> (${formatUsername(target)})`, - fields, - footer: { - text: `User ID: ${entry.targetID}`, - }, - timestamp: new Date().toISOString(), - }, - ], - }); - break; - } case AuditLogActions.MEMBER_ROLE_UPDATE: { const isAdd = entry.after.$add != null; const isSelf = entry.user.id === entry.targetID; diff --git a/src/util/dconstants.js b/src/util/dconstants.js index c4fbd26..c028052 100644 --- a/src/util/dconstants.js +++ b/src/util/dconstants.js @@ -207,9 +207,3 @@ module.exports.VerificationLevelStrings = [ "(╯°□°)╯︵ ┻━┻ (High)", "┻━┻ ミヽ(ಠ益ಠ)ノ彡┻━┻ (Very High/Phone)", ]; - -module.exports.GuildProfileVisibility = { - 1: "Public", - 2: "Restricted", - 3: "Public (application-only)", -};