2022-07-19 17:57:19 +03:00
|
|
|
const {
|
|
|
|
cleanCodeBlockContent,
|
|
|
|
EmbedBuilder,
|
|
|
|
} = require('discord.js');
|
2022-07-17 16:13:44 +03:00
|
|
|
const { diff: getDiff } = require('object-diffy');
|
|
|
|
|
2022-07-20 00:34:45 +03:00
|
|
|
|
|
|
|
const exists = thing => (typeof thing === 'string' && thing.length > 0) && thing !== null && thing !== undefined;
|
|
|
|
|
|
|
|
const arrToObj = obj => {
|
|
|
|
for (const key in obj) {
|
|
|
|
if (obj[key] instanceof Array && obj[key][0]?.id) {
|
|
|
|
const temp = {};
|
|
|
|
obj[key].forEach(v => (temp[v.id] = v));
|
|
|
|
obj[key] = temp;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return obj;
|
|
|
|
};
|
|
|
|
|
2022-07-17 16:13:44 +03:00
|
|
|
function makeDiff({
|
|
|
|
original, updated,
|
|
|
|
}) {
|
2022-07-20 00:34:45 +03:00
|
|
|
const diff = getDiff(arrToObj(original), arrToObj(updated));
|
2022-07-17 16:13:44 +03:00
|
|
|
const fields = [];
|
|
|
|
for (const key in diff) {
|
|
|
|
if (key === 'createdAt') continue; // object-diffy doesn't like dates
|
2022-07-20 00:34:45 +03:00
|
|
|
const from = exists(diff[key].from) ? `- ${String(diff[key].from).replace(/\n/g, '\\n')}\n` : '';
|
|
|
|
const to = exists(diff[key].to) ? `+ ${String(diff[key].to).replace(/\n/g, '\\n')}\n` : '';
|
2022-07-17 16:13:44 +03:00
|
|
|
fields.push({
|
2022-07-17 20:59:58 +03:00
|
|
|
inline: true,
|
2022-07-17 16:13:44 +03:00
|
|
|
name: key,
|
2022-07-19 17:57:19 +03:00
|
|
|
value: `\`\`\`diff\n${cleanCodeBlockContent(from + to)}\n\`\`\``,
|
2022-07-17 16:13:44 +03:00
|
|
|
});
|
|
|
|
}
|
|
|
|
return fields;
|
|
|
|
}
|
2022-07-17 00:18:50 +03:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @param {import("client")} client
|
|
|
|
* @param {string} guildId
|
|
|
|
* @returns {import("discord.js").TextChannel?}
|
|
|
|
*/
|
|
|
|
async function getLogChannel(client, guildId) {
|
|
|
|
const { logChannel: channelId } = await client.prisma.guild.findUnique({
|
|
|
|
select: { logChannel: true },
|
|
|
|
where: { id: guildId },
|
|
|
|
});
|
|
|
|
return channelId && client.channels.cache.get(channelId);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param {import("client")} client
|
|
|
|
* @param {object} details
|
|
|
|
* @param {string} details.guildId
|
|
|
|
* @param {string} details.userId
|
|
|
|
* @param {string} details.action
|
|
|
|
*/
|
|
|
|
async function logAdminEvent(client, {
|
|
|
|
guildId, userId, action, target, diff,
|
|
|
|
}) {
|
|
|
|
const user = await client.users.fetch(userId);
|
2022-07-18 00:33:42 +03:00
|
|
|
client.log.info.settings(`${user.tag} ${action}d ${target.type} ${target.id}`);
|
2022-07-17 00:18:50 +03:00
|
|
|
const settings = await client.prisma.guild.findUnique({
|
|
|
|
select: {
|
|
|
|
footer: true,
|
|
|
|
locale: true,
|
|
|
|
logChannel: true,
|
|
|
|
},
|
|
|
|
where: { id: guildId },
|
|
|
|
});
|
|
|
|
if (!settings.logChannel) return;
|
|
|
|
const getMessage = client.i18n.getLocale(settings.locale);
|
|
|
|
const i18nOptions = {
|
|
|
|
user: `<@${user.id}>`,
|
|
|
|
verb: getMessage(`log.admin.verb.${action}`),
|
|
|
|
};
|
|
|
|
const channel = client.channels.cache.get(settings.logChannel);
|
|
|
|
if (!channel) return;
|
2022-07-18 01:01:00 +03:00
|
|
|
const embeds = [
|
2022-07-18 23:53:17 +03:00
|
|
|
new EmbedBuilder()
|
|
|
|
.setColor('Orange')
|
2022-07-18 01:01:00 +03:00
|
|
|
.setAuthor({
|
|
|
|
iconURL: user.avatarURL(),
|
|
|
|
name: user.username,
|
|
|
|
})
|
|
|
|
.setTitle(getMessage('log.admin.title.joined', {
|
|
|
|
...i18nOptions,
|
|
|
|
targetType: getMessage(`log.admin.title.target.${target.type}`),
|
|
|
|
verb: getMessage(`log.admin.verb.${action}`),
|
|
|
|
}))
|
|
|
|
.setDescription(getMessage('log.admin.description.joined', {
|
|
|
|
...i18nOptions,
|
|
|
|
targetType: getMessage(`log.admin.description.target.${target.type}`),
|
|
|
|
verb: getMessage(`log.admin.verb.${action}`),
|
|
|
|
}))
|
2022-07-18 23:53:17 +03:00
|
|
|
.addFields([
|
|
|
|
{
|
|
|
|
name: getMessage(`log.admin.title.target.${target.type}`),
|
|
|
|
value: target.name ?? target.id,
|
|
|
|
},
|
|
|
|
]),
|
2022-07-18 01:01:00 +03:00
|
|
|
];
|
2022-07-17 16:13:44 +03:00
|
|
|
|
2022-07-18 01:01:00 +03:00
|
|
|
if (diff && diff.original) {
|
|
|
|
embeds.push(
|
2022-07-18 23:53:17 +03:00
|
|
|
new EmbedBuilder()
|
|
|
|
.setColor('Orange')
|
2022-07-18 01:01:00 +03:00
|
|
|
.setTitle(getMessage('log.admin.changes'))
|
|
|
|
.setFields(makeDiff(diff)),
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
return await channel.send({ embeds });
|
2022-07-17 00:18:50 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
module.exports = {
|
|
|
|
getLogChannel,
|
|
|
|
logAdminEvent,
|
|
|
|
};
|