DiscordTickets/src/modals/topic.js

101 lines
2.6 KiB
JavaScript
Raw Normal View History

2022-08-06 00:21: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-06 00:21:55 +03:00
module.exports = class TopicModal extends Modal {
constructor(client, options) {
super(client, {
...options,
id: 'topic',
});
}
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 });
const topic = interaction.fields.getTextInputValue('topic');
const select = {
createdById: true,
guild: {
select: {
footer: true,
locale: true,
successColour: true,
},
},
id: true,
openingMessageId: true,
2022-09-06 02:16:07 +03:00
topic: true,
2022-09-05 14:43:27 +03:00
};
const original = await client.prisma.ticket.findUnique({
select,
where: { id: interaction.channel.id },
});
const ticket = await client.prisma.ticket.update({
2022-10-24 19:44:07 +03:00
data: { 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);
2022-09-06 02:16:07 +03:00
if (topic) interaction.channel.setTopic(`<@${ticket.createdById}> | ${topic}`);
2022-09-05 14:43:27 +03:00
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({
name: getMessage('ticket.opening_message.fields.topic'),
value: topic,
});
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 = {};
2022-10-24 19:44:07 +03:00
diff[getMessage('ticket.opening_message.fields.topic')] = ticket.topic ? decrypt(ticket.topic) : ' ';
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
}
};