From 045176f799185fcd17b2c85c39dce83eda289c05 Mon Sep 17 00:00:00 2001 From: Dragon Fire Date: Mon, 23 Mar 2020 14:38:38 -0400 Subject: [PATCH] Fix strange error in pokedex fetch --- commands/search/pokedex.js | 1 + structures/pokemon/PokemonStore.js | 13 +++++++++---- 2 files changed, 10 insertions(+), 4 deletions(-) diff --git a/commands/search/pokedex.js b/commands/search/pokedex.js index 1d24b206..1e795e46 100644 --- a/commands/search/pokedex.js +++ b/commands/search/pokedex.js @@ -41,6 +41,7 @@ module.exports = class PokedexCommand extends Command { async run(msg, { pokemon }) { try { const data = await this.client.pokemon.fetch(pokemon); + if (!data) return msg.say('Could not find any results.'); const embed = new MessageEmbed() .setColor(0xED1C24) .setAuthor(`#${data.displayID} - ${data.name}`, data.boxImageURL, data.serebiiURL) diff --git a/structures/pokemon/PokemonStore.js b/structures/pokemon/PokemonStore.js index 335f6f02..24378757 100644 --- a/structures/pokemon/PokemonStore.js +++ b/structures/pokemon/PokemonStore.js @@ -6,6 +6,7 @@ const missingno = require('../../assets/json/missingno'); module.exports = class PokemonStore extends Collection { async fetch(query) { query = encodeURIComponent(query.toLowerCase().replace(/ /g, '-').replace(/[^a-zA-Z0-9-]/g, '')); + if (!query) return null; const num = Number.parseInt(query, 10); if (this.has(num)) return this.get(num); const found = this.find(pokemon => pokemon.slug === query); @@ -15,9 +16,13 @@ module.exports = class PokemonStore extends Collection { this.set(pokemon.id, pokemon); return pokemon; } - const { body } = await request.get(`https://pokeapi.co/api/v2/pokemon-species/${query}/`); - const pokemon = new Pokemon(body); - this.set(pokemon.id, pokemon); - return pokemon; + try { + const { body } = await request.get(`https://pokeapi.co/api/v2/pokemon-species/${query}/`); + const pokemon = new Pokemon(body); + this.set(pokemon.id, pokemon); + return pokemon; + } catch { + return null; + } } };