From 5aac998bee3faaaad0e2b2fa01af04b957a8730e Mon Sep 17 00:00:00 2001 From: Isaac Date: Sun, 17 Jul 2022 14:13:44 +0100 Subject: [PATCH] new log diff --- .eslintrc.json | 3 +++ package.json | 2 +- prisma/schema.prisma | 2 +- src/lib/logging.js | 21 +++++++++++++++++-- src/listeners/client/ready.js | 3 ++- .../api/admin/guilds/[guild]/settings.js | 9 ++++++-- 6 files changed, 33 insertions(+), 7 deletions(-) diff --git a/.eslintrc.json b/.eslintrc.json index c0a3d52..dc66be5 100644 --- a/.eslintrc.json +++ b/.eslintrc.json @@ -129,6 +129,9 @@ "no-console": [ "off" ], + "no-prototype-builtins": [ + "off" + ], "no-return-assign": [ "error" ], diff --git a/package.json b/package.json index d7d6a6c..f2bbf8c 100644 --- a/package.json +++ b/package.json @@ -39,7 +39,6 @@ "@fastify/oauth2": "^5.0.0", "@prisma/client": "^4.0.0", "cryptr": "^6.0.3", - "deep-object-diff": "^1.1.7", "discord.js": "^13.8.1", "dotenv": "^16.0.1", "fastify": "^4.2.1", @@ -50,6 +49,7 @@ "ms": "^2.1.3", "node-dir": "^0.1.17", "node-fetch": "^2.6.7", + "object-diffy": "^1.0.4", "semver": "^7.3.7", "terminal-link": "^2.1.1", "yaml": "^1.10.2" diff --git a/prisma/schema.prisma b/prisma/schema.prisma index 01fccb5..eb31fd9 100644 --- a/prisma/schema.prisma +++ b/prisma/schema.prisma @@ -126,7 +126,7 @@ model Guild { successColour String @default("GREEN") tags Tag[] tickets Ticket[] - workingHours Json @default("[\"UTC\", null, null, null, null, null, null, null]") + workingHours Json @default("[\"UTC\", [\"00:00\",\"23:59\"], [\"00:00\",\"23:59\"], [\"00:00\",\"23:59\"], [\"00:00\",\"23:59\"], [\"00:00\",\"23:59\"], [\"00:00\",\"23:59\"], [\"00:00\",\"23:59\"]]") @@map("guilds") } diff --git a/src/lib/logging.js b/src/lib/logging.js index a586614..535f041 100644 --- a/src/lib/logging.js +++ b/src/lib/logging.js @@ -1,4 +1,20 @@ const { MessageEmbed } = require('discord.js'); +const { diff: getDiff } = require('object-diffy'); + +function makeDiff({ + original, updated, +}) { + const diff = getDiff(original, updated); + const fields = []; + for (const key in diff) { + if (key === 'createdAt') continue; // object-diffy doesn't like dates + fields.push({ + name: key, + value: `\`\`\`diff\n${diff[key].from && `- ${diff[key].from}\n`}\n${diff[key].to && `+ ${diff[key].to}\n`}\n\`\`\``, + }); + } + return fields; +} /** * @param {import("client")} client @@ -57,6 +73,7 @@ async function logAdminEvent(client, { const channel = client.channels.cache.get(settings.logChannel); if (!channel) return; const targetName = await getTargetName(client, target); + return await channel.send({ embeds: [ new MessageEmbed() @@ -81,11 +98,11 @@ async function logAdminEvent(client, { // text: settings.footer, // }), ...[ - action === 'update' && diff && + diff && new MessageEmbed() .setColor('ORANGE') .setTitle(getMessage('log.admin.differences')) - .setDescription(`\`\`\`json\n${JSON.stringify(diff)}\n\`\`\``), + .setFields(makeDiff(diff)), ], ], }); diff --git a/src/listeners/client/ready.js b/src/listeners/client/ready.js index 5ec8ed9..4315f0a 100644 --- a/src/listeners/client/ready.js +++ b/src/listeners/client/ready.js @@ -11,7 +11,8 @@ module.exports = class extends Listener { } run() { - process.title = this.client.user.tag + ' [Discord Tickets]'; + // process.title = `"[Discord Tickets] ${this.client.user.tag}"`; // too long and gets cut off + process.title = 'tickets'; this.client.log.success('Connected to Discord as "%s"', this.client.user.tag); } }; diff --git a/src/routes/api/admin/guilds/[guild]/settings.js b/src/routes/api/admin/guilds/[guild]/settings.js index 32887a7..ee3757a 100644 --- a/src/routes/api/admin/guilds/[guild]/settings.js +++ b/src/routes/api/admin/guilds/[guild]/settings.js @@ -1,5 +1,4 @@ const { logAdminEvent } = require('../../../../../lib/logging.js'); -const { updatedDiff } = require('deep-object-diff'); module.exports.delete = fastify => ({ handler: async (req, res) => { @@ -37,6 +36,8 @@ module.exports.get = fastify => ({ module.exports.patch = fastify => ({ handler: async (req, res) => { + if (req.body.hasOwnProperty('id')) delete req.body.id; + if (req.body.hasOwnProperty('createdAt')) delete req.body.createdAt; /** @type {import('client')} */ const client = res.context.config.client; const id = req.params.guild; @@ -47,8 +48,12 @@ module.exports.patch = fastify => ({ }); logAdminEvent(client, { action: 'update', - diff: updatedDiff(original, settings), + diff: { + original, + updated: settings, + }, guildId: id, + original, target: { id, type: 'settings',