From 7e18cd74f8287228e64cc40b5695ab6306fdd737 Mon Sep 17 00:00:00 2001 From: Dragon Fire Date: Sat, 30 Mar 2024 19:13:44 -0400 Subject: [PATCH] Add cache to xiao --- commands/analyze/saucenao.js | 52 ------------------------------------ commands/random-img/xiao.js | 5 ++++ 2 files changed, 5 insertions(+), 52 deletions(-) delete mode 100644 commands/analyze/saucenao.js diff --git a/commands/analyze/saucenao.js b/commands/analyze/saucenao.js deleted file mode 100644 index 19f4341e..00000000 --- a/commands/analyze/saucenao.js +++ /dev/null @@ -1,52 +0,0 @@ -const Command = require('../../framework/Command'); -const { MessageEmbed } = require('discord.js'); -const { isUrlNSFW } = require('../../util/Util'); -const sagiri = require('sagiri'); -const { SAUCENAO_KEY } = process.env; -const sagiriClient = sagiri(SAUCENAO_KEY); - -module.exports = class SauceNaoCommand extends Command { - constructor(client) { - super(client, { - name: 'saucenao', - aliases: ['sauce', 'source'], - group: 'analyze', - memberName: 'saucenao', - description: 'Finds the source for artwork.', - credit: [ - { - name: 'SauceNAO', - url: 'https://saucenao.com/', - reason: 'API' - } - ], - args: [ - { - key: 'image', - type: 'image' - } - ] - }); - } - - async run(msg, { image }) { - let data; - try { - data = await sagiriClient(image); - } catch { - return msg.reply('No results for this image.'); - } - if (!data.length) return msg.reply('No results for this image.'); - const sauce = data[0]; - if (!msg.channel.nsfw) { - const nsfw = await isUrlNSFW(sauce.url, this.client.adultSiteList); - if (nsfw) return msg.reply('The result was NSFW.'); - } - const embed = new MessageEmbed() - .setImage(sauce.thumbnail) - .setURL(sauce.url) - .setAuthor(sauce.authorName || 'Unknown Author', undefined, sauce.authorUrl || sauce.url) - .setFooter(`${sauce.similarity}% similarity`); - return msg.reply({ embeds: [embed] }); - } -}; diff --git a/commands/random-img/xiao.js b/commands/random-img/xiao.js index 7cd6da3c..34a5e657 100644 --- a/commands/random-img/xiao.js +++ b/commands/random-img/xiao.js @@ -30,6 +30,8 @@ module.exports = class XiaoCommand extends Command { } ] }); + + this.cache = new Map(); } async run(msg) { @@ -40,6 +42,7 @@ module.exports = class XiaoCommand extends Command { } async getSource(img) { + if (this.cache.has(img)) return this.cache.get(img); const source = img.match(sourceRegex); const site = source[1]; if (site === 'official') return 'Official Art'; @@ -58,8 +61,10 @@ module.exports = class XiaoCommand extends Command { result += `Art Source: <${sauce.url}>`; } } catch { + this.cache.set(img, result); return result; } + this.cache.set(img, result); return result; }