Skip to content

Commit

Permalink
feat: inline log when editing question answers
Browse files Browse the repository at this point in the history
  • Loading branch information
eartharoid committed Mar 5, 2025
1 parent 96775df commit cb30171
Showing 1 changed file with 45 additions and 18 deletions.
63 changes: 45 additions & 18 deletions src/modals/questions.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -37,6 +38,7 @@ module.exports = class QuestionsModal extends Modal {
select: {
footer: true,
locale: true,
primaryColour: true,
successColour: true,
},
},
Expand All @@ -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);
Expand Down Expand Up @@ -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 });
}
Expand All @@ -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}>`,
Expand Down

0 comments on commit cb30171

Please sign in to comment.