perf: cache tags

This commit is contained in:
Isaac 2023-01-12 17:19:03 +00:00
parent cf6b34785c
commit db94ab770d
No known key found for this signature in database
GPG Key ID: 0DE40AE37BBA5C33
2 changed files with 58 additions and 17 deletions

View File

@ -1,4 +1,5 @@
const { Autocompleter } = require('@eartharoid/dbf'); const { Autocompleter } = require('@eartharoid/dbf');
const ms = require('ms');
module.exports = class TagCompleter extends Autocompleter { module.exports = class TagCompleter extends Autocompleter {
constructor(client, options) { constructor(client, options) {
@ -17,7 +18,21 @@ module.exports = class TagCompleter extends Autocompleter {
/** @type {import("client")} */ /** @type {import("client")} */
const client = this.client; const client = this.client;
const tags = await client.prisma.tag.findMany({ where: { guildId: interaction.guild.id } }); const cacheKey = `cache/guild-tags:${interaction.guild.id}`;
let tags = await client.keyv.get(cacheKey);
if (!tags) {
tags = await client.prisma.tag.findMany({
select: {
content: true,
id: true,
name: true,
regex: true,
},
where: { guildId: interaction.guild.id },
});
client.keyv.set(cacheKey, tags, ms('1h'));
}
const options = value ? tags.filter(tag => const options = value ? tags.filter(tag =>
tag.name.match(new RegExp(value, 'i')) || tag.name.match(new RegExp(value, 'i')) ||
tag.content.match(new RegExp(value, 'i')) || tag.content.match(new RegExp(value, 'i')) ||

View File

@ -184,6 +184,7 @@ module.exports = class extends Listener {
let ticket = await client.prisma.ticket.findUnique({ where: { id: message.channel.id } }); let ticket = await client.prisma.ticket.findUnique({ where: { id: message.channel.id } });
if (ticket) { if (ticket) {
// archive messages
if (settings.archive) { if (settings.archive) {
try { try {
await client.tickets.archiver.saveMessage(ticket.id, message); await client.tickets.archiver.saveMessage(ticket.id, message);
@ -194,6 +195,7 @@ module.exports = class extends Listener {
} }
if (!message.author.bot) { if (!message.author.bot) {
// update user's message count
await client.prisma.user.upsert({ await client.prisma.user.upsert({
create: { create: {
id: message.author.id, id: message.author.id,
@ -203,6 +205,7 @@ module.exports = class extends Listener {
where: { id: message.author.id }, where: { id: message.author.id },
}); });
// set first and last message timestamps
const data = { lastMessageAt: new Date() }; const data = { lastMessageAt: new Date() };
if ( if (
ticket.firstResponseAt === null && ticket.firstResponseAt === null &&
@ -212,19 +215,42 @@ module.exports = class extends Listener {
data, data,
where: { id: ticket.id }, where: { id: ticket.id },
}); });
// if the ticket was set as stale, unset it
if (client.tickets.$stale.has(ticket.id)) {
await message.channel.messages.delete(client.tickets.$stale.get(ticket.id).message.id);
client.tickets.$stale.delete(ticket.id);
}
} }
// TODO: if (!message.author.bot) staff status alert, working hours alerts // TODO: if (!message.author.bot) staff status alert, working hours alerts
} }
if (!message.author.bot) { // auto-tag
const enabled = if (
!message.author.bot &&
(
(settings.autoTag === 'all') || (settings.autoTag === 'all') ||
(settings.autoTag === 'ticket' && ticket) || (settings.autoTag === 'ticket' && ticket) ||
(settings.autoTag === '!ticket' && !ticket) || (settings.autoTag === '!ticket' && !ticket) ||
(settings.autoTag.includes(message.channel.id)); (settings.autoTag.includes(message.channel.id))
if (enabled) { )
const tags = await client.prisma.tag.findMany({ where: { guildId: message.guild.id } }); ) {
const cacheKey = `cache/guild-tags:${message.guild.id}`;
let tags = await client.keyv.get(cacheKey);
if (!tags) {
tags = await client.prisma.tag.findMany({
select: {
content: true,
id: true,
name: true,
regex: true,
},
where: { guildId: message.guild.id },
});
client.keyv.set(cacheKey, tags, ms('1h'));
}
const tag = tags.find(tag => message.content.match(new RegExp(tag.regex, 'mi'))); const tag = tags.find(tag => message.content.match(new RegExp(tag.regex, 'mi')));
if (tag) { if (tag) {
await message.reply({ await message.reply({
@ -235,7 +261,7 @@ module.exports = class extends Listener {
], ],
}); });
} }
}
} }
} }
} }