new log diff

This commit is contained in:
Isaac 2022-07-17 14:13:44 +01:00
parent 574d101637
commit 5aac998bee
6 changed files with 33 additions and 7 deletions

View File

@ -129,6 +129,9 @@
"no-console": [ "no-console": [
"off" "off"
], ],
"no-prototype-builtins": [
"off"
],
"no-return-assign": [ "no-return-assign": [
"error" "error"
], ],

View File

@ -39,7 +39,6 @@
"@fastify/oauth2": "^5.0.0", "@fastify/oauth2": "^5.0.0",
"@prisma/client": "^4.0.0", "@prisma/client": "^4.0.0",
"cryptr": "^6.0.3", "cryptr": "^6.0.3",
"deep-object-diff": "^1.1.7",
"discord.js": "^13.8.1", "discord.js": "^13.8.1",
"dotenv": "^16.0.1", "dotenv": "^16.0.1",
"fastify": "^4.2.1", "fastify": "^4.2.1",
@ -50,6 +49,7 @@
"ms": "^2.1.3", "ms": "^2.1.3",
"node-dir": "^0.1.17", "node-dir": "^0.1.17",
"node-fetch": "^2.6.7", "node-fetch": "^2.6.7",
"object-diffy": "^1.0.4",
"semver": "^7.3.7", "semver": "^7.3.7",
"terminal-link": "^2.1.1", "terminal-link": "^2.1.1",
"yaml": "^1.10.2" "yaml": "^1.10.2"

View File

@ -126,7 +126,7 @@ model Guild {
successColour String @default("GREEN") successColour String @default("GREEN")
tags Tag[] tags Tag[]
tickets Ticket[] 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") @@map("guilds")
} }

View File

@ -1,4 +1,20 @@
const { MessageEmbed } = require('discord.js'); 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 * @param {import("client")} client
@ -57,6 +73,7 @@ async function logAdminEvent(client, {
const channel = client.channels.cache.get(settings.logChannel); const channel = client.channels.cache.get(settings.logChannel);
if (!channel) return; if (!channel) return;
const targetName = await getTargetName(client, target); const targetName = await getTargetName(client, target);
return await channel.send({ return await channel.send({
embeds: [ embeds: [
new MessageEmbed() new MessageEmbed()
@ -81,11 +98,11 @@ async function logAdminEvent(client, {
// text: settings.footer, // text: settings.footer,
// }), // }),
...[ ...[
action === 'update' && diff && diff &&
new MessageEmbed() new MessageEmbed()
.setColor('ORANGE') .setColor('ORANGE')
.setTitle(getMessage('log.admin.differences')) .setTitle(getMessage('log.admin.differences'))
.setDescription(`\`\`\`json\n${JSON.stringify(diff)}\n\`\`\``), .setFields(makeDiff(diff)),
], ],
], ],
}); });

View File

@ -11,7 +11,8 @@ module.exports = class extends Listener {
} }
run() { 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); this.client.log.success('Connected to Discord as "%s"', this.client.user.tag);
} }
}; };

View File

@ -1,5 +1,4 @@
const { logAdminEvent } = require('../../../../../lib/logging.js'); const { logAdminEvent } = require('../../../../../lib/logging.js');
const { updatedDiff } = require('deep-object-diff');
module.exports.delete = fastify => ({ module.exports.delete = fastify => ({
handler: async (req, res) => { handler: async (req, res) => {
@ -37,6 +36,8 @@ module.exports.get = fastify => ({
module.exports.patch = fastify => ({ module.exports.patch = fastify => ({
handler: async (req, res) => { 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')} */ /** @type {import('client')} */
const client = res.context.config.client; const client = res.context.config.client;
const id = req.params.guild; const id = req.params.guild;
@ -47,8 +48,12 @@ module.exports.patch = fastify => ({
}); });
logAdminEvent(client, { logAdminEvent(client, {
action: 'update', action: 'update',
diff: updatedDiff(original, settings), diff: {
original,
updated: settings,
},
guildId: id, guildId: id,
original,
target: { target: {
id, id,
type: 'settings', type: 'settings',