mirror of
https://github.com/Hessenuk/DiscordTickets.git
synced 2024-12-22 07:43:08 +02:00
perf: improve ticket
/references
autocompleters (and de-duplicate)
This commit is contained in:
parent
f27feea2f9
commit
09095f71c1
@ -142,7 +142,7 @@
|
||||
"error"
|
||||
],
|
||||
"no-underscore-dangle": [
|
||||
"error",
|
||||
"warn",
|
||||
{
|
||||
"allowAfterThis": true,
|
||||
"allowFunctionParams": true
|
||||
|
@ -1,7 +1,4 @@
|
||||
const { Autocompleter } = require('@eartharoid/dbf');
|
||||
const emoji = require('node-emoji');
|
||||
const Cryptr = require('cryptr');
|
||||
const { decrypt } = new Cryptr(process.env.ENCRYPTION_KEY);
|
||||
|
||||
module.exports = class ReferencesCompleter extends Autocompleter {
|
||||
constructor(client, options) {
|
||||
@ -11,12 +8,6 @@ module.exports = class ReferencesCompleter extends Autocompleter {
|
||||
});
|
||||
}
|
||||
|
||||
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}`;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param {string} value
|
||||
@ -24,32 +15,12 @@ module.exports = class ReferencesCompleter extends Autocompleter {
|
||||
* @param {import("discord.js").AutocompleteInteraction} interaction
|
||||
*/
|
||||
async run(value, comamnd, interaction) {
|
||||
/** @type {import("client")} */
|
||||
const client = this.client;
|
||||
const tickets = await client.prisma.ticket.findMany({
|
||||
include: {
|
||||
category: {
|
||||
select: {
|
||||
emoji: true,
|
||||
name: true,
|
||||
},
|
||||
},
|
||||
guild: true,
|
||||
},
|
||||
where: {
|
||||
createdById: interaction.user.id,
|
||||
await interaction.respond(
|
||||
await this.client.autocomplete.components.get('ticket').getOptions(value, {
|
||||
guildId: interaction.guild.id,
|
||||
open: false,
|
||||
},
|
||||
});
|
||||
const options = value ? tickets.filter(t => this.format(t).match(new RegExp(value, 'i'))) : tickets;
|
||||
await interaction.respond(
|
||||
options
|
||||
.slice(0, 25)
|
||||
.map(t => ({
|
||||
name: this.format(t),
|
||||
value: t.id,
|
||||
})),
|
||||
userId: interaction.user.id,
|
||||
}),
|
||||
);
|
||||
}
|
||||
};
|
@ -1,7 +1,10 @@
|
||||
/* eslint-disable no-underscore-dangle */
|
||||
const { Autocompleter } = require('@eartharoid/dbf');
|
||||
const emoji = require('node-emoji');
|
||||
const Cryptr = require('cryptr');
|
||||
const { decrypt } = new Cryptr(process.env.ENCRYPTION_KEY);
|
||||
const Keyv = require('keyv');
|
||||
const ms = require('ms');
|
||||
|
||||
module.exports = class TicketCompleter extends Autocompleter {
|
||||
constructor(client, options) {
|
||||
@ -9,24 +12,23 @@ module.exports = class TicketCompleter extends Autocompleter {
|
||||
...options,
|
||||
id: 'ticket',
|
||||
});
|
||||
|
||||
this.cache = new Keyv();
|
||||
}
|
||||
|
||||
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}`;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param {string} value
|
||||
* @param {*} command
|
||||
* @param {import("discord.js").AutocompleteInteraction} interaction
|
||||
*/
|
||||
async run(value, command, interaction) {
|
||||
async getOptions(value, {
|
||||
guildId,
|
||||
open,
|
||||
userId,
|
||||
}) {
|
||||
/** @type {import("client")} */
|
||||
const client = this.client;
|
||||
const tickets = await client.prisma.ticket.findMany({
|
||||
const cacheKey = [guildId, userId, open].join('/');
|
||||
|
||||
let tickets = await this.cache.get(cacheKey);
|
||||
|
||||
if (!tickets) {
|
||||
tickets = await client.prisma.ticket.findMany({
|
||||
include: {
|
||||
category: {
|
||||
select: {
|
||||
@ -37,19 +39,42 @@ module.exports = class TicketCompleter extends Autocompleter {
|
||||
guild: true,
|
||||
},
|
||||
where: {
|
||||
createdById: interaction.user.id,
|
||||
guildId: interaction.guild.id,
|
||||
open: ['add', 'close', 'force-close', 'remove'].includes(command.name), // false for `new`, `transcript` etc
|
||||
createdById: userId,
|
||||
guildId,
|
||||
open,
|
||||
},
|
||||
});
|
||||
const options = value ? tickets.filter(t => this.format(t).match(new RegExp(value, 'i'))) : tickets;
|
||||
await interaction.respond(
|
||||
options
|
||||
tickets = tickets.map(ticket => {
|
||||
const date = new Date(ticket.createdAt).toLocaleString([ticket.guild.locale, 'en-GB'], { 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;
|
||||
ticket._name = `${category} #${ticket.number} (${date}) ${topic}`;
|
||||
return ticket;
|
||||
});
|
||||
this.cache.set(cacheKey, tickets, ms('1m'));
|
||||
}
|
||||
|
||||
const options = value ? tickets.filter(t => t._name.match(new RegExp(value, 'i'))) : tickets;
|
||||
return options
|
||||
.slice(0, 25)
|
||||
.map(t => ({
|
||||
name: this.format(t),
|
||||
name: t._name,
|
||||
value: t.id,
|
||||
})),
|
||||
}));
|
||||
}
|
||||
|
||||
/**
|
||||
* @param {string} value
|
||||
* @param {*} command
|
||||
* @param {import("discord.js").AutocompleteInteraction} interaction
|
||||
*/
|
||||
async run(value, command, interaction) {
|
||||
await interaction.respond(
|
||||
await this.getOptions(value, {
|
||||
guildId: interaction.guild.id,
|
||||
open: ['add', 'close', 'force-close', 'remove'].includes(command.name), // false for `new`, `transcript` etc
|
||||
userId: interaction.user.id,
|
||||
}),
|
||||
);
|
||||
}
|
||||
};
|
Loading…
Reference in New Issue
Block a user