Add evolution chain to Pokemon

This commit is contained in:
Dragon Fire
2020-03-25 20:46:32 -04:00
parent d2c8473dc0
commit 82dbca2b75
4 changed files with 45 additions and 5 deletions
+13 -1
View File
@@ -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!`);
+1 -1
View File
@@ -1,6 +1,6 @@
{
"name": "xiao",
"version": "112.15.3",
"version": "112.15.4",
"description": "Your personal server companion.",
"main": "Xiao.js",
"scripts": {
+29 -1
View File
@@ -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;
}
};
+2 -2
View File
@@ -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) {