Fix up pokedex cache

This commit is contained in:
Daniel Odendahl Jr
2018-07-09 23:44:20 +00:00
parent d6d1bf2b04
commit efeaeac134
3 changed files with 25 additions and 13 deletions
+3
View File
@@ -2,3 +2,6 @@ node_modules/
package-lock.json package-lock.json
logs/ logs/
*.log *.log
commands/games/box-choosing.js
assets/json/box-choosing.json
+21 -12
View File
@@ -17,7 +17,7 @@ module.exports = class PokedexCommand extends Command {
key: 'pokemon', key: 'pokemon',
prompt: 'What Pokémon would you like to get information on?', prompt: 'What Pokémon would you like to get information on?',
type: 'string', type: 'string',
parse: pokemon => encodeURIComponent(pokemon.toLowerCase().replace(/ /g, '-')) parse: pokemon => this.makeSlug(pokemon)
} }
] ]
}); });
@@ -37,7 +37,7 @@ module.exports = class PokedexCommand extends Command {
) )
.setDescription(stripIndents` .setDescription(stripIndents`
**The ${data.genus}** **The ${data.genus}**
${this.filterPokemonData(data.entries).flavor_text.replace(/\n|\f|\r/g, ' ')} ${data.entries[Math.floor(Math.random() * data.entries.length)]}
`) `)
.setThumbnail(`https://www.serebii.net/sunmoon/pokemon/${data.id}.png`); .setThumbnail(`https://www.serebii.net/sunmoon/pokemon/${data.id}.png`);
return msg.embed(embed); return msg.embed(embed);
@@ -48,23 +48,32 @@ module.exports = class PokedexCommand extends Command {
} }
async fetchPokemon(query) { async fetchPokemon(query) {
if (this.cache.has(query)) return this.cache.get(query); const num = Number.parseInt(query, 10);
const found = this.cache.find( if (this.cache.has(num)) return this.cache.get(num);
pokemon => pokemon.name.toLowerCase() === query.toLowerCase() || pokemon.id === query const found = this.cache.find(pokemon => pokemon.slug === query);
);
if (found) return found; if (found) return found;
const { body } = await request.get(`https://pokeapi.co/api/v2/pokemon-species/${query}/`); const { body } = await request.get(`https://pokeapi.co/api/v2/pokemon-species/${query}/`);
const entries = body.flavor_text_entries
.filter(entry => entry.language.name === 'en')
.map(entry => entry.flavor_text.replace(/\n|\f|\r/g, ' '));
const { name } = this.filterPokemonData(body.names);
this.cache.set(body.id, { this.cache.set(body.id, {
id: body.id.toString().padStart(3, '0'), id: body.id,
name: this.filterPokemonData(body.names, false).name, displayID: body.id.toString().padStart(3, '0'),
genus: this.filterPokemonData(body.genera, false).genus, name,
entries: body.flavor_text_entries slug: this.makeSlug(name),
genus: this.filterPokemonData(body.genera).genus,
entries
}); });
return this.cache.get(body.id); return this.cache.get(body.id);
} }
filterPokemonData(arr, random = true) { filterPokemonData(arr) {
const filtered = arr.filter(entry => entry.language.name === 'en'); const filtered = arr.filter(entry => entry.language.name === 'en');
return filtered[random ? Math.floor(Math.random() * filtered.length) : 0]; return filtered[0];
}
makeSlug(name) {
return encodeURIComponent(name.toLowerCase().replace(/ /g, '-').replace(/[^a-zA-Z0-9-]/g, ''));
} }
}; };
+1 -1
View File
@@ -1,6 +1,6 @@
{ {
"name": "xiao", "name": "xiao",
"version": "84.1.2", "version": "84.1.3",
"description": "Your personal server companion.", "description": "Your personal server companion.",
"main": "Xiao.js", "main": "Xiao.js",
"scripts": { "scripts": {