feat: inline log when editing question answers

This commit is contained in:
Isaac
2025-03-05 01:27:21 +00:00
parent 96775dfabc
commit cb30171ee7

View File

@@ -3,6 +3,7 @@ const { EmbedBuilder } = require('discord.js');
const ExtendedEmbedBuilder = require('../lib/embed'); const ExtendedEmbedBuilder = require('../lib/embed');
const { logTicketEvent } = require('../lib/logging'); const { logTicketEvent } = require('../lib/logging');
const { reusable } = require('../lib/threads'); const { reusable } = require('../lib/threads');
const { cleanCodeBlockContent } = require('discord.js');
module.exports = class QuestionsModal extends Modal { module.exports = class QuestionsModal extends Modal {
@@ -37,6 +38,7 @@ module.exports = class QuestionsModal extends Modal {
select: { select: {
footer: true, footer: true,
locale: true, locale: true,
primaryColour: true,
successColour: true, successColour: true,
}, },
}, },
@@ -49,6 +51,16 @@ module.exports = class QuestionsModal extends Modal {
where: { id: interaction.channel.id }, where: { id: interaction.channel.id },
}); });
const plainTextAnswers = await Promise.all(
original.questionAnswers
.map(async answer => ({
after: interaction.fields.getTextInputValue(String(answer.id)),
before: answer.value ? await worker.decrypt(answer.value) : '',
id: answer.id,
question: answer.question,
})),
);
let topic; let topic;
if (category.customTopic) { if (category.customTopic) {
const customTopicAnswer = original.questionAnswers.find(a => a.question.id === category.customTopic); const customTopicAnswer = original.questionAnswers.find(a => a.question.id === category.customTopic);
@@ -81,13 +93,11 @@ module.exports = class QuestionsModal extends Modal {
const embeds = [...opening.embeds]; const embeds = [...opening.embeds];
embeds[1] = new EmbedBuilder(embeds[1].data) embeds[1] = new EmbedBuilder(embeds[1].data)
.setFields( .setFields(
await Promise.all( plainTextAnswers
ticket.questionAnswers .map(a => ({
.map(async a => ({ name: a.question.label,
name: a.question.label, value: a.after || getMessage('ticket.answers.no_value'),
value: a.value ? await worker.decrypt(a.value) : getMessage('ticket.answers.no_value'), })),
})),
),
); );
await opening.edit({ embeds }); await opening.edit({ embeds });
} }
@@ -104,21 +114,38 @@ module.exports = class QuestionsModal extends Modal {
], ],
}); });
/** @param {ticket} ticket */ const diff = {
const makeDiff = async ticket => { original: {},
const diff = {}; updated: {},
for (const a of ticket.questionAnswers) {
diff[a.question.label] = a.value ? await worker.decrypt(a.value) : getMessage('ticket.answers.no_value');
}
return diff;
}; };
const inlineDiffEmbeds = [];
for (const answer of plainTextAnswers) {
diff.original[answer.question.label] = answer.before || getMessage('ticket.answers.no_value');
diff.updated[answer.question.label] = answer.after || getMessage('ticket.answers.no_value');
if (answer.before !== answer.after) {
const from = answer.before ? answer.before.replace(/^/gm, '- ') + '\n' : '';
const to = answer.after ? answer.after.replace(/^/gm, '+ ') + '\n' : '';
inlineDiffEmbeds.push(
new EmbedBuilder()
.setColor(ticket.guild.primaryColour)
.setAuthor({
iconURL: interaction.member.displayAvatarURL(),
name: interaction.user.username,
})
.setTitle(answer.question.label)
.setDescription(`\`\`\`diff\n${cleanCodeBlockContent(from + to)}\n\`\`\``),
);
}
}
if (inlineDiffEmbeds.length) {
await interaction.followUp({ embeds: inlineDiffEmbeds });
}
logTicketEvent(this.client, { logTicketEvent(this.client, {
action: 'update', action: 'update',
diff: { diff,
original: await makeDiff(original),
updated: await makeDiff(ticket),
},
target: { target: {
id: ticket.id, id: ticket.id,
name: `<#${ticket.id}>`, name: `<#${ticket.id}>`,