2022-08-02 23:13:32 +03:00
|
|
|
const { Autocompleter } = require('@eartharoid/dbf');
|
2022-10-11 23:28:53 +03:00
|
|
|
const emoji = require('node-emoji');
|
|
|
|
const Cryptr = require('cryptr');
|
2022-10-24 19:44:07 +03:00
|
|
|
const { decrypt } = new Cryptr(process.env.ENCRYPTION_KEY);
|
2022-08-02 23:13:32 +03:00
|
|
|
|
|
|
|
module.exports = class ReferencesCompleter extends Autocompleter {
|
|
|
|
constructor(client, options) {
|
|
|
|
super(client, {
|
|
|
|
...options,
|
|
|
|
id: 'references',
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2022-08-14 01:01:44 +03:00
|
|
|
/**
|
|
|
|
* @param {string} value
|
|
|
|
* @param {*} comamnd
|
|
|
|
* @param {import("discord.js").AutocompleteInteraction} interaction
|
|
|
|
*/
|
|
|
|
async run(value, comamnd, interaction) {
|
|
|
|
/** @type {import("client")} */
|
|
|
|
const client = this.client;
|
|
|
|
const settings = await client.prisma.guild.findUnique({ where: { id: interaction.guild.id } });
|
|
|
|
const tickets = await client.prisma.ticket.findMany({
|
2022-10-11 23:28:53 +03:00
|
|
|
include: {
|
|
|
|
category: {
|
|
|
|
select: {
|
|
|
|
emoji: true,
|
|
|
|
name: true,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
2022-08-14 01:01:44 +03:00
|
|
|
where: {
|
|
|
|
createdById: interaction.user.id,
|
|
|
|
guildId: interaction.guild.id,
|
|
|
|
open: false,
|
|
|
|
},
|
|
|
|
});
|
|
|
|
const options = value ? tickets.filter(t =>
|
|
|
|
String(t.number).match(new RegExp(value, 'i')) ||
|
|
|
|
t.topic?.match(new RegExp(value, 'i')) ||
|
|
|
|
new Date(t.createdAt).toLocaleString(settings.locale, { dateStyle: 'short' })?.match(new RegExp(value, 'i')),
|
|
|
|
) : tickets;
|
|
|
|
await interaction.respond(
|
|
|
|
options
|
|
|
|
.slice(0, 25)
|
|
|
|
.map(t => {
|
|
|
|
const date = new Date(t.createdAt).toLocaleString(settings.locale, { dateStyle: 'short' });
|
2022-10-24 19:44:07 +03:00
|
|
|
const topic = t.topic ? '| ' + decrypt(t.topic).substring(0, 50) : '';
|
2022-10-11 23:28:53 +03:00
|
|
|
const category = emoji.hasEmoji(t.category.emoji) ? emoji.get(t.category.emoji) + ' ' + t.category.name : t.category.name;
|
2022-08-14 01:01:44 +03:00
|
|
|
return {
|
2022-10-11 23:28:53 +03:00
|
|
|
name: `${category} #${t.number} - ${date} ${topic}`,
|
2022-08-14 01:01:44 +03:00
|
|
|
value: t.id,
|
|
|
|
};
|
|
|
|
}),
|
|
|
|
);
|
|
|
|
}
|
2022-08-02 23:13:32 +03:00
|
|
|
};
|