From db94ab770de2bb2b7a2ca18412205a8b185528c6 Mon Sep 17 00:00:00 2001 From: Isaac Date: Thu, 12 Jan 2023 17:19:03 +0000 Subject: [PATCH] perf: cache tags --- src/autocomplete/tag.js | 17 +++++++- src/listeners/client/messageCreate.js | 58 +++++++++++++++++++-------- 2 files changed, 58 insertions(+), 17 deletions(-) diff --git a/src/autocomplete/tag.js b/src/autocomplete/tag.js index b75a76d..90dcef6 100644 --- a/src/autocomplete/tag.js +++ b/src/autocomplete/tag.js @@ -1,4 +1,5 @@ const { Autocompleter } = require('@eartharoid/dbf'); +const ms = require('ms'); module.exports = class TagCompleter extends Autocompleter { constructor(client, options) { @@ -17,7 +18,21 @@ module.exports = class TagCompleter extends Autocompleter { /** @type {import("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 => tag.name.match(new RegExp(value, 'i')) || tag.content.match(new RegExp(value, 'i')) || diff --git a/src/listeners/client/messageCreate.js b/src/listeners/client/messageCreate.js index 1832b4e..a8eb04c 100644 --- a/src/listeners/client/messageCreate.js +++ b/src/listeners/client/messageCreate.js @@ -184,6 +184,7 @@ module.exports = class extends Listener { let ticket = await client.prisma.ticket.findUnique({ where: { id: message.channel.id } }); if (ticket) { + // archive messages if (settings.archive) { try { await client.tickets.archiver.saveMessage(ticket.id, message); @@ -194,6 +195,7 @@ module.exports = class extends Listener { } if (!message.author.bot) { + // update user's message count await client.prisma.user.upsert({ create: { id: message.author.id, @@ -203,6 +205,7 @@ module.exports = class extends Listener { where: { id: message.author.id }, }); + // set first and last message timestamps const data = { lastMessageAt: new Date() }; if ( ticket.firstResponseAt === null && @@ -212,30 +215,53 @@ module.exports = class extends Listener { data, 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 } - if (!message.author.bot) { - const enabled = - (settings.autoTag === 'all') || + // auto-tag + if ( + !message.author.bot && + ( + (settings.autoTag === 'all') || (settings.autoTag === 'ticket' && ticket) || (settings.autoTag === '!ticket' && !ticket) || - (settings.autoTag.includes(message.channel.id)); - if (enabled) { - const tags = await client.prisma.tag.findMany({ where: { guildId: message.guild.id } }); - const tag = tags.find(tag => message.content.match(new RegExp(tag.regex, 'mi'))); - if (tag) { - await message.reply({ - embeds: [ - new EmbedBuilder() - .setColor(settings.primaryColour) - .setDescription(tag.content), - ], - }); - } + (settings.autoTag.includes(message.channel.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'))); + if (tag) { + await message.reply({ + embeds: [ + new EmbedBuilder() + .setColor(settings.primaryColour) + .setDescription(tag.content), + ], + }); + } + } } }