Add cache to xiao

This commit is contained in:
Dragon Fire
2024-03-30 19:13:44 -04:00
parent 3487ffdd58
commit 7e18cd74f8
2 changed files with 5 additions and 52 deletions
-52
View File
@@ -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] });
}
};
+5
View File
@@ -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;
}