DiscordTickets/src/autocomplete/ticket.js

55 lines
1.6 KiB
JavaScript
Raw Normal View History

2022-08-02 23:13:32 +03:00
const { Autocompleter } = require('@eartharoid/dbf');
2022-08-17 22:41:51 +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 TicketCompleter extends Autocompleter {
constructor(client, options) {
super(client, {
...options,
id: 'ticket',
});
}
2022-10-24 22:17:40 +03:00
format(ticket) {
const date = new Date(ticket.createdAt).toLocaleString(ticket.guild.locale, { dateStyle: 'short' });
const topic = ticket.topic ? '| ' + decrypt(ticket.topic).substring(0, 50) : '';
const category = emoji.hasEmoji(ticket.category.emoji) ? emoji.get(ticket.category.emoji) + ' ' + ticket.category.name : ticket.category.name;
return `${category} #${ticket.number} - ${date} ${topic}`;
}
2022-08-14 01:58:41 +03:00
/**
* @param {string} value
2022-09-04 23:00:18 +03:00
* @param {*} command
2022-08-14 01:58:41 +03:00
* @param {import("discord.js").AutocompleteInteraction} interaction
*/
2022-09-04 23:00:18 +03:00
async run(value, command, interaction) {
2022-08-14 01:58:41 +03:00
/** @type {import("client")} */
const client = this.client;
const tickets = await client.prisma.ticket.findMany({
2022-08-17 22:41:51 +03:00
include: {
category: {
select: {
emoji: true,
name: true,
},
},
2022-10-24 22:17:40 +03:00
guild: true,
2022-08-17 22:41:51 +03:00
},
2022-08-14 01:58:41 +03:00
where: {
createdById: interaction.user.id,
guildId: interaction.guild.id,
2022-09-04 23:00:18 +03:00
open: ['add', 'close', 'force-close', 'remove'].includes(command.name), // false for `new`, `transcript` etc
2022-08-14 01:58:41 +03:00
},
});
2022-10-24 22:17:40 +03:00
const options = value ? tickets.filter(t => this.format(t).match(new RegExp(value, 'i'))) : tickets;
2022-08-14 01:58:41 +03:00
await interaction.respond(
options
.slice(0, 25)
2022-10-24 22:17:40 +03:00
.map(t => ({
name: this.format(t),
value: t.id,
})),
2022-08-14 01:58:41 +03:00
);
}
2022-08-02 23:13:32 +03:00
};