DiscordTickets/src/modals/questions.js

132 lines
3.5 KiB
JavaScript
Raw Normal View History

const { Modal } = require('@eartharoid/dbf');
2022-09-05 14:43:27 +03:00
const { EmbedBuilder } = require('discord.js');
const ExtendedEmbedBuilder = require('../lib/embed');
const { logTicketEvent } = require('../lib/logging');
2022-09-11 21:46:53 +03:00
const Cryptr = require('cryptr');
2022-10-24 19:44:07 +03:00
const {
encrypt,
decrypt,
} = new Cryptr(process.env.ENCRYPTION_KEY);
module.exports = class QuestionsModal extends Modal {
constructor(client, options) {
super(client, {
...options,
id: 'questions',
});
}
/**
*
* @param {*} id
* @param {import("discord.js").ModalSubmitInteraction} interaction
*/
2022-08-06 00:21:55 +03:00
async run(id, interaction) {
2022-09-05 14:43:27 +03:00
/** @type {import("client")} */
const client = this.client;
if (id.edit) {
await interaction.deferReply({ ephemeral: true });
2022-09-05 14:43:27 +03:00
const { category } = await client.prisma.ticket.findUnique({
select: { category: { select: { customTopic: true } } },
where: { id: interaction.channel.id },
});
const select = {
createdById: true,
guild: {
select: {
footer: true,
locale: true,
successColour: true,
},
},
id: true,
openingMessageId: true,
questionAnswers: { include: { question: true } },
};
const original = await client.prisma.ticket.findUnique({
select,
where: { id: interaction.channel.id },
});
let topic;
if (category.customTopic) {
const customTopicAnswer = original.questionAnswers.find(a => a.question.id === category.customTopic);
if (!customTopicAnswer) throw new Error('Custom topic answer not found');
topic = interaction.fields.getTextInputValue(String(customTopicAnswer.id));
}
2022-09-05 14:43:27 +03:00
const ticket = await client.prisma.ticket.update({
data: {
questionAnswers: {
update: interaction.fields.fields.map(f => ({
2022-10-24 19:44:07 +03:00
data: { value: f.value ? encrypt(f.value) : '' },
2022-09-05 14:43:27 +03:00
where: { id: Number(f.customId) },
})),
},
2023-06-25 17:01:39 +03:00
topic: topic ? encrypt(topic) : null,
2022-09-05 14:43:27 +03:00
},
select,
where: { id: interaction.channel.id },
});
const getMessage = client.i18n.getLocale(ticket.guild.locale);
if (topic) await interaction.channel.setTopic(`<@${ticket.createdById}> | ${topic}`);
const opening = await interaction.channel.messages.fetch(ticket.openingMessageId);
if (opening && opening.embeds.length >= 2) {
const embeds = [...opening.embeds];
embeds[1] = new EmbedBuilder(embeds[1].data)
.setFields(
ticket.questionAnswers
.map(a => ({
name: a.question.label,
2022-10-24 19:44:07 +03:00
value: a.value ? decrypt(a.value) : getMessage('ticket.answers.no_value'),
2022-09-05 14:43:27 +03:00
})),
);
await opening.edit({ embeds });
}
await interaction.editReply({
embeds: [
new ExtendedEmbedBuilder({
iconURL: interaction.guild.iconURL(),
text: ticket.guild.footer,
})
.setColor(ticket.guild.successColour)
.setTitle(getMessage('ticket.edited.title'))
.setDescription(getMessage('ticket.edited.description')),
],
});
/** @param {ticket} ticket */
const makeDiff = ticket => {
const diff = {};
ticket.questionAnswers.forEach(a => {
2022-10-24 19:44:07 +03:00
diff[a.question.label] = a.value ? decrypt(a.value) : getMessage('ticket.answers.no_value');
2022-09-05 14:43:27 +03:00
});
return diff;
};
logTicketEvent(this.client, {
action: 'update',
diff: {
original: makeDiff(original),
updated: makeDiff(ticket),
},
target: {
id: ticket.id,
name: `<#${ticket.id}>`,
},
userId: interaction.user.id,
});
} else {
await this.client.tickets.postQuestions({
...id,
interaction,
});
}
2022-08-06 00:21:55 +03:00
}
};