From 968889e529b9d7809c696f29caaafc563f22f962 Mon Sep 17 00:00:00 2001 From: Dark <7890309+DarkView@users.noreply.github.com> Date: Thu, 1 Jul 2021 04:40:39 +0200 Subject: [PATCH] Add missing logtypes, add change visualization --- backend/src/data/DefaultLogMessages.json | 8 +++--- backend/src/data/LogType.ts | 2 ++ backend/src/plugins/Logs/LogsPlugin.ts | 6 +++-- .../Logs/events/LogsChannelModifyEvts.ts | 17 +++++++++++- .../plugins/Logs/events/LogsRoleModifyEvts.ts | 17 +++++++++++- .../events/LogsStageInstanceModifyEvts.ts | 7 ++++- .../Logs/events/LogsThreadModifyEvts.ts | 6 ++++- backend/src/utils.ts | 27 +++++++++++++++++++ 8 files changed, 80 insertions(+), 10 deletions(-) diff --git a/backend/src/data/DefaultLogMessages.json b/backend/src/data/DefaultLogMessages.json index 2f00d39c..fccdb1ed 100644 --- a/backend/src/data/DefaultLogMessages.json +++ b/backend/src/data/DefaultLogMessages.json @@ -24,15 +24,15 @@ "CHANNEL_CREATE": "🖊 Channel {channelMention(channel)} was created", "CHANNEL_DELETE": "🗑 Channel {channelMention(channel)} was deleted", - "CHANNEL_EDIT": "✏ Channel {channelMention(channel)} was edited", + "CHANNEL_UPDATE": "✏ Channel {channelMention(newChannel)} was edited. Changes:\n{differenceString}", "THREAD_CREATE": "🖊 Thread {channelMention(thread)} was created in channel <#{thread.parentID}>", "THREAD_DELETE": "🗑 Thread {channelMention(thread)} was deleted/archived from channel <#{thread.parentID}>", - "THREAD_UPDATE": "✏ Thread {channelMention(newThread)} was edited. Previous name: `{oldThread.name}`", + "THREAD_UPDATE": "✏ Thread {channelMention(newThread)} was edited. Changes:\n{differenceString}", "ROLE_CREATE": "🖊 Role **{role.name}** (`{role.id}`) was created", "ROLE_DELETE": "🖊 Role **{role.name}** (`{role.id}`) was deleted", - "ROLE_EDIT": "🖊 Role **{role.name}** (`{role.id}`) was edited", + "ROLE_UPDATE": "🖊 Role **{newRole.name}** (`{newRole.id}`) was edited. Changes:\n{differenceString}", "MESSAGE_EDIT": "✏ {userMention(user)} edited their message (`{after.id}`) in {channelMention(channel)}:\n**Before:**{messageSummary(before)}**After:**{messageSummary(after)}", "MESSAGE_DELETE": "🗑 Message (`{message.id}`) from {userMention(user)} deleted in {channelMention(channel)} (originally posted at **{messageDate}**):{messageSummary(message)}", @@ -48,7 +48,7 @@ "STAGE_INSTANCE_CREATE": "📣 Stage Instance `{stageInstance.topic}` was created in Stage Channel <#{stageChannel.id}>", "STAGE_INSTANCE_DELETE": "📣 Stage Instance `{stageInstance.topic}` was deleted in Stage Channel <#{stageChannel.id}>", - "STAGE_INSTANCE_UPDATE": "📣 Stage Instance `{newStageInstance.topic}` was edited in Stage Channel <#{stageChannel.id}>. Previous topic: `{oldStageInstance.topic}`", + "STAGE_INSTANCE_UPDATE": "📣 Stage Instance `{newStageInstance.topic}` was edited in Stage Channel <#{stageChannel.id}>. Changes:\n{differenceString}", "COMMAND": "🤖 {userMention(member)} used command in {channelMention(channel)}:\n`{command}`", diff --git a/backend/src/data/LogType.ts b/backend/src/data/LogType.ts index c01610e2..eb6e66e5 100644 --- a/backend/src/data/LogType.ts +++ b/backend/src/data/LogType.ts @@ -18,6 +18,7 @@ export enum LogType { CHANNEL_CREATE, CHANNEL_DELETE, + CHANNEL_UPDATE, THREAD_CREATE, THREAD_DELETE, @@ -25,6 +26,7 @@ export enum LogType { ROLE_CREATE, ROLE_DELETE, + ROLE_UPDATE, MESSAGE_EDIT, MESSAGE_DELETE, diff --git a/backend/src/plugins/Logs/LogsPlugin.ts b/backend/src/plugins/Logs/LogsPlugin.ts index 8c8520be..64c62444 100644 --- a/backend/src/plugins/Logs/LogsPlugin.ts +++ b/backend/src/plugins/Logs/LogsPlugin.ts @@ -11,10 +11,10 @@ import { disableCodeBlocks } from "../../utils"; import { CasesPlugin } from "../Cases/CasesPlugin"; import { TimeAndDatePlugin } from "../TimeAndDate/TimeAndDatePlugin"; import { zeppelinGuildPlugin } from "../ZeppelinPluginBlueprint"; -import { LogsChannelCreateEvt, LogsChannelDeleteEvt } from "./events/LogsChannelModifyEvts"; +import { LogsChannelCreateEvt, LogsChannelDeleteEvt, LogsChannelUpdateEvt } from "./events/LogsChannelModifyEvts"; import { LogsGuildMemberAddEvt } from "./events/LogsGuildMemberAddEvt"; import { LogsGuildMemberRemoveEvt } from "./events/LogsGuildMemberRemoveEvt"; -import { LogsRoleCreateEvt, LogsRoleDeleteEvt } from "./events/LogsRoleModifyEvts"; +import { LogsRoleCreateEvt, LogsRoleDeleteEvt, LogsRoleUpdateEvt } from "./events/LogsRoleModifyEvts"; import { LogsStageInstanceCreateEvt, LogsStageInstanceDeleteEvt, @@ -70,8 +70,10 @@ export const LogsPlugin = zeppelinGuildPlugin()({ LogsGuildMemberUpdateEvt, LogsChannelCreateEvt, LogsChannelDeleteEvt, + LogsChannelUpdateEvt, LogsRoleCreateEvt, LogsRoleDeleteEvt, + LogsRoleUpdateEvt, LogsVoiceStateUpdateEvt, LogsStageInstanceCreateEvt, LogsStageInstanceDeleteEvt, diff --git a/backend/src/plugins/Logs/events/LogsChannelModifyEvts.ts b/backend/src/plugins/Logs/events/LogsChannelModifyEvts.ts index fb7b9e74..8c5f7c0a 100644 --- a/backend/src/plugins/Logs/events/LogsChannelModifyEvts.ts +++ b/backend/src/plugins/Logs/events/LogsChannelModifyEvts.ts @@ -1,5 +1,5 @@ import { LogType } from "../../../data/LogType"; -import { stripObjectToScalars } from "../../../utils"; +import { differenceToString, getScalarDifference, stripObjectToScalars } from "../../../utils"; import { logsEvt } from "../types"; export const LogsChannelCreateEvt = logsEvt({ @@ -21,3 +21,18 @@ export const LogsChannelDeleteEvt = logsEvt({ }); }, }); + +export const LogsChannelUpdateEvt = logsEvt({ + event: "channelUpdate", + + async listener(meta) { + const diff = getScalarDifference(meta.args.oldChannel, meta.args.newChannel); + const differenceString = differenceToString(diff); + + meta.pluginData.state.guildLogs.log(LogType.CHANNEL_UPDATE, { + oldChannel: stripObjectToScalars(meta.args.oldChannel), + newChannel: stripObjectToScalars(meta.args.newChannel), + differenceString, + }); + }, +}); diff --git a/backend/src/plugins/Logs/events/LogsRoleModifyEvts.ts b/backend/src/plugins/Logs/events/LogsRoleModifyEvts.ts index 7c5f2108..729deaab 100644 --- a/backend/src/plugins/Logs/events/LogsRoleModifyEvts.ts +++ b/backend/src/plugins/Logs/events/LogsRoleModifyEvts.ts @@ -1,5 +1,5 @@ import { LogType } from "../../../data/LogType"; -import { stripObjectToScalars } from "../../../utils"; +import { differenceToString, getScalarDifference, stripObjectToScalars } from "../../../utils"; import { logsEvt } from "../types"; export const LogsRoleCreateEvt = logsEvt({ @@ -21,3 +21,18 @@ export const LogsRoleDeleteEvt = logsEvt({ }); }, }); + +export const LogsRoleUpdateEvt = logsEvt({ + event: "roleUpdate", + + async listener(meta) { + const diff = getScalarDifference(meta.args.oldRole, meta.args.newRole); + const differenceString = differenceToString(diff); + + meta.pluginData.state.guildLogs.log(LogType.ROLE_UPDATE, { + newRole: stripObjectToScalars(meta.args.newRole), + oldRole: stripObjectToScalars(meta.args.oldRole), + differenceString, + }); + }, +}); diff --git a/backend/src/plugins/Logs/events/LogsStageInstanceModifyEvts.ts b/backend/src/plugins/Logs/events/LogsStageInstanceModifyEvts.ts index 25b68a53..6ce0b98a 100644 --- a/backend/src/plugins/Logs/events/LogsStageInstanceModifyEvts.ts +++ b/backend/src/plugins/Logs/events/LogsStageInstanceModifyEvts.ts @@ -1,5 +1,5 @@ import { LogType } from "../../../data/LogType"; -import { stripObjectToScalars } from "../../../utils"; +import { differenceToString, getScalarDifference, stripObjectToScalars } from "../../../utils"; import { logsEvt } from "../types"; export const LogsStageInstanceCreateEvt = logsEvt({ @@ -37,10 +37,15 @@ export const LogsStageInstanceUpdateEvt = logsEvt({ const stageChannel = meta.args.newStageInstance.channel ?? (await meta.pluginData.guild.channels.fetch(meta.args.newStageInstance.channelID)); + + const diff = getScalarDifference(meta.args.oldStageInstance, meta.args.newStageInstance); + const differenceString = differenceToString(diff); + meta.pluginData.state.guildLogs.log(LogType.STAGE_INSTANCE_UPDATE, { oldStageInstance: stripObjectToScalars(meta.args.oldStageInstance), newStageInstance: stripObjectToScalars(meta.args.newStageInstance), stageChannel: stripObjectToScalars(stageChannel), + differenceString, }); }, }); diff --git a/backend/src/plugins/Logs/events/LogsThreadModifyEvts.ts b/backend/src/plugins/Logs/events/LogsThreadModifyEvts.ts index bb5c4b5d..0ed7af00 100644 --- a/backend/src/plugins/Logs/events/LogsThreadModifyEvts.ts +++ b/backend/src/plugins/Logs/events/LogsThreadModifyEvts.ts @@ -1,5 +1,5 @@ import { LogType } from "../../../data/LogType"; -import { stripObjectToScalars } from "../../../utils"; +import { stripObjectToScalars, getScalarDifference, differenceToString } from "../../../utils"; import { logsEvt } from "../types"; export const LogsThreadCreateEvt = logsEvt({ @@ -26,9 +26,13 @@ export const LogsThreadUpdateEvt = logsEvt({ event: "threadUpdate", async listener(meta) { + const diff = getScalarDifference(meta.args.oldThread, meta.args.newThread, ["messageCount", "archiveTimestamp"]); + const differenceString = differenceToString(diff); + meta.pluginData.state.guildLogs.log(LogType.THREAD_UPDATE, { oldThread: stripObjectToScalars(meta.args.oldThread), newThread: stripObjectToScalars(meta.args.newThread), + differenceString, }); }, }); diff --git a/backend/src/utils.ts b/backend/src/utils.ts index 9f549db7..f9d9094a 100644 --- a/backend/src/utils.ts +++ b/backend/src/utils.ts @@ -35,6 +35,7 @@ import { SimpleCache } from "./SimpleCache"; import { sendDM } from "./utils/sendDM"; import { waitForButtonConfirm } from "./utils/waitForInteraction"; import { decodeAndValidateStrict, StrictValidationError } from "./validatorUtils"; +import { isEqual } from "lodash"; const fsp = fs.promises; @@ -165,6 +166,32 @@ function tDeepPartialProp(prop: any) { } } +export function getScalarDifference( + base: T, + object: T, + ignoreKeys: string[] = [], +): Map { + base = stripObjectToScalars(base) as T; + object = stripObjectToScalars(object) as T; + const diff = new Map(); + + for (const [key, value] of Object.entries(object)) { + if (!isEqual(value, base[key]) && !ignoreKeys.includes(key)) { + diff.set(key, { was: base[key], is: value }); + } + } + + return diff; +} + +export function differenceToString(diff: Map): string { + let toReturn = ""; + for (const [key, difference] of diff) { + toReturn += `${key[0].toUpperCase() + key.slice(1)}: \`${difference.was}\` ➜ \`${difference.is}\`\n`; + } + return toReturn; +} + // https://stackoverflow.com/a/49262929/316944 export type Not = T & Exclude;