2022-08-02 20:03:55 +03:00
|
|
|
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);
|
2022-08-02 20:03:55 +03:00
|
|
|
|
|
|
|
module.exports = class QuestionsModal extends Modal {
|
|
|
|
constructor(client, options) {
|
|
|
|
super(client, {
|
|
|
|
...options,
|
|
|
|
id: 'questions',
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2022-08-08 03:36:14 +03:00
|
|
|
/**
|
|
|
|
*
|
|
|
|
* @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 });
|
2023-06-25 15:39:42 +03:00
|
|
|
|
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 },
|
|
|
|
});
|
2023-06-25 15:39:42 +03:00
|
|
|
|
|
|
|
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
|
|
|
}
|
2023-06-25 15:39:42 +03:00
|
|
|
};
|