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 { logTicketEvent } = require('../lib/logging');
const { reusable } = require('../lib/threads');
const { cleanCodeBlockContent } = require('discord.js');
module.exports = class QuestionsModal extends Modal {
@@ -37,6 +38,7 @@ module.exports = class QuestionsModal extends Modal {
select: {
footer: true,
locale: true,
primaryColour: true,
successColour: true,
},
},
@@ -49,6 +51,16 @@ module.exports = class QuestionsModal extends Modal {
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;
if (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];
embeds[1] = new EmbedBuilder(embeds[1].data)
.setFields(
await Promise.all(
ticket.questionAnswers
.map(async a => ({
name: a.question.label,
value: a.value ? await worker.decrypt(a.value) : getMessage('ticket.answers.no_value'),
})),
),
plainTextAnswers
.map(a => ({
name: a.question.label,
value: a.after || getMessage('ticket.answers.no_value'),
})),
);
await opening.edit({ embeds });
}
@@ -104,21 +114,38 @@ module.exports = class QuestionsModal extends Modal {
],
});
/** @param {ticket} ticket */
const makeDiff = async ticket => {
const diff = {};
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 diff = {
original: {},
updated: {},
};
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, {
action: 'update',
diff: {
original: await makeDiff(original),
updated: await makeDiff(ticket),
},
diff,
target: {
id: ticket.id,
name: `<#${ticket.id}>`,