perf: improve ticket/references autocompleters (and de-duplicate)

This commit is contained in:
Isaac 2022-10-26 00:41:40 +01:00
parent f27feea2f9
commit 09095f71c1
No known key found for this signature in database
GPG Key ID: 0DE40AE37BBA5C33
3 changed files with 60 additions and 64 deletions

View File

@ -142,7 +142,7 @@
"error" "error"
], ],
"no-underscore-dangle": [ "no-underscore-dangle": [
"error", "warn",
{ {
"allowAfterThis": true, "allowAfterThis": true,
"allowFunctionParams": true "allowFunctionParams": true

View File

@ -1,7 +1,4 @@
const { Autocompleter } = require('@eartharoid/dbf'); 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 { module.exports = class ReferencesCompleter extends Autocompleter {
constructor(client, options) { 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 * @param {string} value
@ -24,32 +15,12 @@ module.exports = class ReferencesCompleter extends Autocompleter {
* @param {import("discord.js").AutocompleteInteraction} interaction * @param {import("discord.js").AutocompleteInteraction} interaction
*/ */
async run(value, comamnd, interaction) { async run(value, comamnd, interaction) {
/** @type {import("client")} */ await interaction.respond(
const client = this.client; await this.client.autocomplete.components.get('ticket').getOptions(value, {
const tickets = await client.prisma.ticket.findMany({
include: {
category: {
select: {
emoji: true,
name: true,
},
},
guild: true,
},
where: {
createdById: interaction.user.id,
guildId: interaction.guild.id, guildId: interaction.guild.id,
open: false, open: false,
}, userId: interaction.user.id,
}); }),
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,
})),
); );
} }
}; };

View File

@ -1,7 +1,10 @@
/* eslint-disable no-underscore-dangle */
const { Autocompleter } = require('@eartharoid/dbf'); const { Autocompleter } = require('@eartharoid/dbf');
const emoji = require('node-emoji'); const emoji = require('node-emoji');
const Cryptr = require('cryptr'); const Cryptr = require('cryptr');
const { decrypt } = new Cryptr(process.env.ENCRYPTION_KEY); const { decrypt } = new Cryptr(process.env.ENCRYPTION_KEY);
const Keyv = require('keyv');
const ms = require('ms');
module.exports = class TicketCompleter extends Autocompleter { module.exports = class TicketCompleter extends Autocompleter {
constructor(client, options) { constructor(client, options) {
@ -9,13 +12,55 @@ module.exports = class TicketCompleter extends Autocompleter {
...options, ...options,
id: 'ticket', id: 'ticket',
}); });
this.cache = new Keyv();
} }
format(ticket) { async getOptions(value, {
const date = new Date(ticket.createdAt).toLocaleString(ticket.guild.locale, { dateStyle: 'short' }); guildId,
const topic = ticket.topic ? '| ' + decrypt(ticket.topic).substring(0, 50) : ''; open,
const category = emoji.hasEmoji(ticket.category.emoji) ? emoji.get(ticket.category.emoji) + ' ' + ticket.category.name : ticket.category.name; userId,
return `${category} #${ticket.number} - ${date} ${topic}`; }) {
/** @type {import("client")} */
const client = this.client;
const cacheKey = [guildId, userId, open].join('/');
let tickets = await this.cache.get(cacheKey);
if (!tickets) {
tickets = await client.prisma.ticket.findMany({
include: {
category: {
select: {
emoji: true,
name: true,
},
},
guild: true,
},
where: {
createdById: userId,
guildId,
open,
},
});
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: t._name,
value: t.id,
}));
} }
/** /**
@ -24,32 +69,12 @@ module.exports = class TicketCompleter extends Autocompleter {
* @param {import("discord.js").AutocompleteInteraction} interaction * @param {import("discord.js").AutocompleteInteraction} interaction
*/ */
async run(value, command, interaction) { async run(value, command, 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,
guildId: interaction.guild.id,
open: ['add', 'close', 'force-close', 'remove'].includes(command.name), // false for `new`, `transcript` etc
},
});
const options = value ? tickets.filter(t => this.format(t).match(new RegExp(value, 'i'))) : tickets;
await interaction.respond( await interaction.respond(
options await this.getOptions(value, {
.slice(0, 25) guildId: interaction.guild.id,
.map(t => ({ open: ['add', 'close', 'force-close', 'remove'].includes(command.name), // false for `new`, `transcript` etc
name: this.format(t), userId: interaction.user.id,
value: t.id, }),
})),
); );
} }
}; };