diff --git a/commands/search/pokedex.js b/commands/search/pokedex.js index 6668c5ee..b296089a 100644 --- a/commands/search/pokedex.js +++ b/commands/search/pokedex.js @@ -55,7 +55,19 @@ module.exports = class PokedexCommand extends Command { .addField('❯ Types', typesShown.map(variety => { const showParens = variety.name && typesShown.length > 1; return `${variety.types.join('/')}${showParens ? ` (${variety.name})` : ''}`; - }).join('\n')); + }).join('\n')) + .addField('❯ Evolution Chain', data.chain.map(pkmn => { + if (Array.isArray(pkmn)) { + return pkmn.map(pkmn2 => { + const found = this.client.pokemon.get(pkmn2); + if (found.id === data.id) return `**${found.name}**`; + return found.name + }).join('/'); + } + const found = this.client.pokemon.get(pkmn); + if (found.id === data.id) return `**${found.name}**`; + return found.name; + }).join(' -> ')); return msg.embed(embed); } catch (err) { return msg.reply(`Oh no, an error occurred: \`${err.message}\`. Try again later!`); diff --git a/package.json b/package.json index a7a5ecd0..0ca6fbb4 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "xiao", - "version": "112.15.3", + "version": "112.15.4", "description": "Your personal server companion.", "main": "Xiao.js", "scripts": { diff --git a/structures/pokemon/Pokemon.js b/structures/pokemon/Pokemon.js index e4c79881..b70bac12 100644 --- a/structures/pokemon/Pokemon.js +++ b/structures/pokemon/Pokemon.js @@ -3,7 +3,8 @@ const { removeDuplicates, firstUpperCase } = require('../../util/Util'); const missingno = require('../../assets/json/missingno'); module.exports = class Pokemon { - constructor(data) { + constructor(store, data) { + this.store = store; this.id = data.id; this.name = data.names.find(entry => entry.language.name === 'en').name; this.entries = removeDuplicates(data.flavor_text_entries @@ -23,6 +24,7 @@ module.exports = class Pokemon { types: [] }; }); + this.chain = { url: data.evolution_chain.url, data: [] }; this.typesCached = false; this.missingno = data.missingno || false; } @@ -73,4 +75,30 @@ module.exports = class Pokemon { this.typesCached = true; return this; } + + async fetchChain() { + if (this.chain.data.length) return this.chain.data; + const { body } = await request.get(this.chain.url); + const basePkmn = await this.store.fetch(body.chain.species.name); + this.chain.data.push(basePkmn.id); + if (body.chain.evolves_to.length) { + const evolution1 = body.chain.evolves_to; + if (!evolution1) return this.chain.data; + const evos1 = []; + const evos2 = []; + for (const evolution of evolution1) { + const pkmn = await this.store.fetch(evolution.species.name); + evos1.push(pkmn.id); + if (evolution.evolves_to) { + for (const evolution2 of evolution.evolves_to) { + const pkmn2 = await this.store.fetch(evolution2.species.name); + evos2.push(pkmn2.id); + } + } + } + this.chain.data.push(evos1.length === 1 ? evos1[0] : evos1); + if (evos2.length) this.chain.data.push(evos2.length === 1 ? evos2[0] : evos2); + } + return this.chain.data; + } }; diff --git a/structures/pokemon/PokemonStore.js b/structures/pokemon/PokemonStore.js index 5ed61c3d..699d7b09 100644 --- a/structures/pokemon/PokemonStore.js +++ b/structures/pokemon/PokemonStore.js @@ -12,13 +12,13 @@ module.exports = class PokemonStore extends Collection { const found = this.find(pokemon => pokemon.slug === query); if (found) return found; if (query === 'missingno' || num === 0) { - const pokemon = new Pokemon(missingno); + const pokemon = new Pokemon(this, missingno); 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); + const pokemon = new Pokemon(this, body); this.set(pokemon.id, pokemon); return pokemon; } catch (err) {