Cache in pokedex, cache less in whos-that-pokemon

This commit is contained in:
Daniel Odendahl Jr
2018-07-09 18:59:44 +00:00
parent c9cdb122a6
commit d6d1bf2b04
3 changed files with 31 additions and 11 deletions
+4 -1
View File
@@ -53,7 +53,10 @@ module.exports = class WhosThatPokemonCommand extends Command {
async fetchPokemon(pokemon) {
if (this.cache.has(pokemon)) return this.cache.get(pokemon);
const { body } = await request.get(`https://pokeapi.co/api/v2/pokemon-species/${pokemon}/`);
this.cache.set(body.id, body);
this.cache.set(body.id, {
id: body.id,
names: body.names
});
return body;
}
+26 -9
View File
@@ -1,5 +1,5 @@
const Command = require('../../structures/Command');
const { MessageEmbed } = require('discord.js');
const { Collection, MessageEmbed } = require('discord.js');
const request = require('node-superfetch');
const { stripIndents } = require('common-tags');
@@ -21,24 +21,25 @@ module.exports = class PokedexCommand extends Command {
}
]
});
this.cache = new Collection();
}
async run(msg, { pokemon }) {
try {
const { body } = await request.get(`https://pokeapi.co/api/v2/pokemon-species/${pokemon}/`);
const id = body.id.toString().padStart(3, '0');
const data = await this.fetchPokemon(pokemon);
const embed = new MessageEmbed()
.setColor(0xED1C24)
.setAuthor(
`#${id} - ${this.filterPokemonData(body.names, false).name}`,
`https://www.serebii.net/pokedex-sm/icon/${id}.png`,
`https://www.serebii.net/pokedex-sm/${id}.shtml`
`#${data.id} - ${data.name}`,
`https://www.serebii.net/pokedex-sm/icon/${data.id}.png`,
`https://www.serebii.net/pokedex-sm/${data.id}.shtml`
)
.setDescription(stripIndents`
**The ${this.filterPokemonData(body.genera, false).genus}**
${this.filterPokemonData(body.flavor_text_entries).flavor_text.replace(/\n|\f|\r/g, ' ')}
**The ${data.genus}**
${this.filterPokemonData(data.entries).flavor_text.replace(/\n|\f|\r/g, ' ')}
`)
.setThumbnail(`https://www.serebii.net/sunmoon/pokemon/${id}.png`);
.setThumbnail(`https://www.serebii.net/sunmoon/pokemon/${data.id}.png`);
return msg.embed(embed);
} catch (err) {
if (err.status === 404) return msg.say('Could not find any results.');
@@ -46,6 +47,22 @@ module.exports = class PokedexCommand extends Command {
}
}
async fetchPokemon(query) {
if (this.cache.has(query)) return this.cache.get(query);
const found = this.cache.find(
pokemon => pokemon.name.toLowerCase() === query.toLowerCase() || pokemon.id === query
);
if (found) return found;
const { body } = await request.get(`https://pokeapi.co/api/v2/pokemon-species/${query}/`);
this.cache.set(body.id, {
id: body.id.toString().padStart(3, '0'),
name: this.filterPokemonData(body.names, false).name,
genus: this.filterPokemonData(body.genera, false).genus,
entries: body.flavor_text_entries
});
return this.cache.get(body.id);
}
filterPokemonData(arr, random = true) {
const filtered = arr.filter(entry => entry.language.name === 'en');
return filtered[random ? Math.floor(Math.random() * filtered.length) : 0];
+1 -1
View File
@@ -1,6 +1,6 @@
{
"name": "xiao",
"version": "84.1.1",
"version": "84.1.2",
"description": "Your personal server companion.",
"main": "Xiao.js",
"scripts": {