diff --git a/commands/games/whos-that-pokemon.js b/commands/games/whos-that-pokemon.js index 1903fdef..2253726f 100644 --- a/commands/games/whos-that-pokemon.js +++ b/commands/games/whos-that-pokemon.js @@ -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; } diff --git a/commands/search/pokedex.js b/commands/search/pokedex.js index 65a35629..c39a719d 100644 --- a/commands/search/pokedex.js +++ b/commands/search/pokedex.js @@ -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]; diff --git a/package.json b/package.json index 08910adc..c72d2029 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "xiao", - "version": "84.1.1", + "version": "84.1.2", "description": "Your personal server companion.", "main": "Xiao.js", "scripts": {