Add ticket logging & minor fixes

This commit is contained in:
Isaac 2022-08-08 23:37:47 +01:00
parent 80399cd294
commit 25d70630a7
No known key found for this signature in database
GPG Key ID: F4EAABEB0FFCC06A
3 changed files with 90 additions and 13 deletions

View File

@ -160,7 +160,13 @@ log:
create: created create: created
delete: deleted delete: deleted
update: updated update: updated
tickets: ticket:
description: '{user} {verb} ticket'
ticket: Ticket
title: Ticket {verb}
verb:
create: created
close: closed
menus: menus:
category: category:
placeholder: Select a ticket category placeholder: Select a ticket category

View File

@ -60,8 +60,6 @@ async function getLogChannel(client, guildId) {
async function logAdminEvent(client, { async function logAdminEvent(client, {
guildId, userId, action, target, diff, guildId, userId, action, target, diff,
}) { }) {
const user = await client.users.fetch(userId);
client.log.info.settings(`${user.tag} ${action}d ${target.type} ${target.id}`);
const settings = await client.prisma.guild.findUnique({ const settings = await client.prisma.guild.findUnique({
select: { select: {
footer: true, footer: true,
@ -70,6 +68,10 @@ async function logAdminEvent(client, {
}, },
where: { id: guildId }, where: { id: guildId },
}); });
/** @type {import("discord.js").Guild} */
const guild = client.guilds.cache.get(guildId);
const member = await guild.members.fetch(userId);
client.log.info.settings(`${member.user.tag} ${action}d ${target.type} ${target.id}`);
if (!settings.logChannel) return; if (!settings.logChannel) return;
const colour = action === 'create' const colour = action === 'create'
? 'Green' : action === 'update' ? 'Green' : action === 'update'
@ -77,7 +79,7 @@ async function logAdminEvent(client, {
? 'Red' : 'Default'; ? 'Red' : 'Default';
const getMessage = client.i18n.getLocale(settings.locale); const getMessage = client.i18n.getLocale(settings.locale);
const i18nOptions = { const i18nOptions = {
user: `<@${user.id}>`, user: `<@${member.user.id}>`,
verb: getMessage(`log.admin.verb.${action}`), verb: getMessage(`log.admin.verb.${action}`),
}; };
const channel = client.channels.cache.get(settings.logChannel); const channel = client.channels.cache.get(settings.logChannel);
@ -86,8 +88,8 @@ async function logAdminEvent(client, {
new EmbedBuilder() new EmbedBuilder()
.setColor(colour) .setColor(colour)
.setAuthor({ .setAuthor({
iconURL: user.avatarURL(), iconURL: member.displayAvatarURL(),
name: user.username, name: member.displayName,
}) })
.setTitle(getMessage('log.admin.title.joined', { .setTitle(getMessage('log.admin.title.joined', {
...i18nOptions, ...i18nOptions,
@ -119,7 +121,66 @@ async function logAdminEvent(client, {
return await channel.send({ embeds }); return await channel.send({ embeds });
} }
/**
* @param {import("client")} client
* @param {object} details
* @param {string} details.guildId
* @param {string} details.userId
* @param {string} details.action
*/
async function logTicketEvent(client, {
userId, action, target,
}) {
const ticket = await client.prisma.ticket.findUnique({
include: { guild: true },
where: { id: target.id },
});
if (!ticket) return;
/** @type {import("discord.js").Guild} */
const guild = client.guilds.cache.get(ticket.guild.id);
const member = await guild.members.fetch(userId);
client.log.info.tickets(`${member.user.tag} ${action}d ticket ${target.id}`);
if (!ticket.guild.logChannel) return;
const colour = action === 'create'
? 'Aqua' : action === 'close'
? 'DarkAqua' : action === 'claim'
? 'LuminousVividPink' : action === 'unclaim'
? 'DarkVividPink' : 'Default';
const getMessage = client.i18n.getLocale(ticket.guild.locale);
const i18nOptions = {
user: `<@${member.user.id}>`,
verb: getMessage(`log.ticket.verb.${action}`),
};
const channel = client.channels.cache.get(ticket.guild.logChannel);
if (!channel) return;
const embeds = [
new EmbedBuilder()
.setColor(colour)
.setAuthor({
iconURL: member.displayAvatarURL(),
name: member.displayName,
})
.setTitle(getMessage('log.ticket.title', {
...i18nOptions,
verb: getMessage(`log.ticket.verb.${action}`),
}))
.setDescription(getMessage('log.ticket.description', {
...i18nOptions,
verb: getMessage(`log.ticket.verb.${action}`),
}))
.addFields([
{
name: getMessage('log.ticket.ticket'),
value: target.name ?? target.id,
},
]),
];
return await channel.send({ embeds });
}
module.exports = { module.exports = {
getLogChannel, getLogChannel,
logAdminEvent, logAdminEvent,
logTicketEvent,
}; };

View File

@ -1,6 +1,7 @@
/* eslint-disable max-lines */ /* eslint-disable max-lines */
const { const {
ActionRowBuilder, ActionRowBuilder,
ButtonBuilder,
ButtonStyle, ButtonStyle,
ModalBuilder, ModalBuilder,
SelectMenuBuilder, SelectMenuBuilder,
@ -11,7 +12,7 @@ const {
const emoji = require('node-emoji'); const emoji = require('node-emoji');
const ms = require('ms'); const ms = require('ms');
const ExtendedEmbedBuilder = require('../embed'); const ExtendedEmbedBuilder = require('../embed');
const { ButtonBuilder } = require('discord.js'); const { logTicketEvent } = require('../logging');
/** /**
* @typedef {import('@prisma/client').Category & {guild: import('@prisma/client').Guild} & {questions: import('@prisma/client').Question[]}} CategoryGuildQuestions * @typedef {import('@prisma/client').Category & {guild: import('@prisma/client').Guild} & {questions: import('@prisma/client').Question[]}} CategoryGuildQuestions
@ -274,7 +275,9 @@ module.exports = class TicketManager {
new ExtendedEmbedBuilder() new ExtendedEmbedBuilder()
.setColor(category.guild.primaryColour) .setColor(category.guild.primaryColour)
.setFields( .setFields(
category.questions.map(q => ({ category.questions
.sort((a, b) => a.order - b.order)
.map(q => ({
name: q.label, name: q.label,
value: interaction.fields.getTextInputValue(q.id) || getMessage('ticket.answers.no_value'), value: interaction.fields.getTextInputValue(q.id) || getMessage('ticket.answers.no_value'),
})), })),
@ -392,6 +395,13 @@ module.exports = class TicketManager {
.setDescription(getMessage('ticket.created.description', { channel: channel.toString() })), .setDescription(getMessage('ticket.created.description', { channel: channel.toString() })),
], ],
}); });
// TODO: log channel await logTicketEvent(this.client, { // FIXME: remove await
action: 'create',
target: {
id: ticket.id,
name: channel.toString(),
},
userId: interaction.user.id,
});
} }
}; };