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

@@ -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)),
],
],
});

View File

@@ -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);
}
};

View File

@@ -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',