mirror of
https://github.com/Hessenuk/DiscordTickets.git
synced 2025-09-04 09:21:25 +03:00
Settings, encryption, logging
This commit is contained in:
97
src/lib/logging.js
Normal file
97
src/lib/logging.js
Normal file
@@ -0,0 +1,97 @@
|
||||
const { MessageEmbed } = require('discord.js');
|
||||
|
||||
/**
|
||||
* @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 {*} target
|
||||
* @returns {string} target.type
|
||||
* @returns {string} target.id
|
||||
*/
|
||||
async function getTargetName(client, target) {
|
||||
if (target.type === 'settings') {
|
||||
return client.guilds.cache.get(target.id).name;
|
||||
} else {
|
||||
const row = await client.prisma[target.type].findUnique({ where: { id: target.id } });
|
||||
return row.name ?? target.id;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @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);
|
||||
client.log.info(`${user.tag} ${action}d ${target.type} ${target.id}`);
|
||||
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;
|
||||
const targetName = await getTargetName(client, target);
|
||||
return await channel.send({
|
||||
embeds: [
|
||||
new MessageEmbed()
|
||||
.setColor('ORANGE')
|
||||
.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}`),
|
||||
}))
|
||||
.addField(getMessage(`log.admin.title.target.${target.type}`), targetName),
|
||||
// .setFooter({
|
||||
// iconURL: client.guilds.cache.get(guildId).iconURL(),
|
||||
// text: settings.footer,
|
||||
// }),
|
||||
...[
|
||||
action === 'update' && diff &&
|
||||
new MessageEmbed()
|
||||
.setColor('ORANGE')
|
||||
.setTitle(getMessage('log.admin.differences'))
|
||||
.setDescription(`\`\`\`json\n${JSON.stringify(diff)}\n\`\`\``),
|
||||
],
|
||||
],
|
||||
});
|
||||
}
|
||||
|
||||
module.exports = {
|
||||
getLogChannel,
|
||||
logAdminEvent,
|
||||
};
|
41
src/lib/prisma.js
Normal file
41
src/lib/prisma.js
Normal file
@@ -0,0 +1,41 @@
|
||||
const Cryptr = require('cryptr');
|
||||
const cryptr = new Cryptr(process.env.ENCRYPTION_KEY);
|
||||
const fields = [
|
||||
'name',
|
||||
'content',
|
||||
'username',
|
||||
'displayName',
|
||||
'channelName',
|
||||
'openingMessage',
|
||||
'description',
|
||||
'value',
|
||||
'placeholder',
|
||||
'closedReason',
|
||||
'topic',
|
||||
'comment',
|
||||
'label',
|
||||
'regex',
|
||||
];
|
||||
const shouldEncrypt = ['create', 'createMany', 'update', 'updateMany', 'upsert'];
|
||||
const shouldDecrypt = ['findUnique', 'findFirst', 'findMany'];
|
||||
|
||||
module.exports = async (params, next) => {
|
||||
if (params.args.data && shouldEncrypt.includes(params.action)) {
|
||||
for (const field of fields) {
|
||||
if (field in params.args.data) {
|
||||
params.args.data[field] = cryptr.encrypt(params.args.data[field]);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
const result = await next(params);
|
||||
|
||||
if (result && shouldDecrypt.includes(params.action)) {
|
||||
for (const field of fields) {
|
||||
if (field in result) {
|
||||
result[field] = cryptr.decrypt(params.result[field]);
|
||||
}
|
||||
}
|
||||
}
|
||||
return result;
|
||||
};
|
1
src/lib/strings.js
Normal file
1
src/lib/strings.js
Normal file
@@ -0,0 +1 @@
|
||||
module.exports.capitalise = string => string.charAt(0).toUpperCase() + string.slice(1);
|
Reference in New Issue
Block a user