From 8cc4296183b31fe0437a622223fc1ea56623892b Mon Sep 17 00:00:00 2001 From: Dragon Fire Date: Mon, 1 Feb 2021 17:40:10 -0500 Subject: [PATCH] Only fetch needed variety --- commands/games-sp/pokemon-advantage.js | 4 ++-- structures/pokemon/Pokemon.js | 7 ++++++- 2 files changed, 8 insertions(+), 3 deletions(-) diff --git a/commands/games-sp/pokemon-advantage.js b/commands/games-sp/pokemon-advantage.js index edb9a6f8..894806ee 100644 --- a/commands/games-sp/pokemon-advantage.js +++ b/commands/games-sp/pokemon-advantage.js @@ -73,8 +73,8 @@ module.exports = class PokemonAdvantageCommand extends Command { const pkmn1 = await this.client.pokemon.fetch(num1); const num2 = Math.floor(Math.random() * (this.client.pokemon.pokemonCount + 1)); const pkmn2 = await this.client.pokemon.fetch(num2); - if (!pkmn1.gameDataCached) await pkmn1.fetchGameData(); - if (!pkmn2.gameDataCached) await pkmn2.fetchGameData(); + await pkmn1.fetchDefaultVariety(); + await pkmn2.fetchDefaultVariety(); const attachment = await this.createImage(pkmn1, pkmn2, null); const answer = this.calculateAdvantage(pkmn1, pkmn2); const answerAttachment = await this.createImage(pkmn1, pkmn2, answer); diff --git a/structures/pokemon/Pokemon.js b/structures/pokemon/Pokemon.js index f65f5f97..b9d90fa5 100644 --- a/structures/pokemon/Pokemon.js +++ b/structures/pokemon/Pokemon.js @@ -42,7 +42,8 @@ module.exports = class Pokemon { statsDiffer: data.missingno ? true : null, default: variety.is_default, types: data.missingno ? variety.types : [], - abilities: data.missingno ? variety.abilities : [] + abilities: data.missingno ? variety.abilities : [], + gameDataCached: data.missingno || false }; }); this.chain = { @@ -176,6 +177,7 @@ module.exports = class Pokemon { async fetchDefaultVariety() { const defaultVariety = this.varieties.find(variety => variety.default); + if (defaultVariety.gameDataCached) return this; const { body: defaultBody } = await request.get(`https://pokeapi.co/api/v2/pokemon/${defaultVariety.id}`); defaultVariety.types.push(...defaultBody.types.map(type => firstUpperCase(type.type.name))); for (const ability of defaultBody.abilities) { @@ -191,6 +193,7 @@ module.exports = class Pokemon { spd: defaultBody.stats.find(stat => stat.stat.name === 'speed').base_stat }; defaultVariety.statsDiffer = true; + defaultVariety.gameDataCached = true; const inSwordShield = defaultBody.moves .some(move => move.version_group_details.some(mve => mve.version_group.name === 'sword-shield')); this.moveSetVersion = inSwordShield ? 'sword-shield' : 'ultra-sun-ultra-moon'; @@ -206,6 +209,7 @@ module.exports = class Pokemon { const defaultVariety = this.varieties.find(variety => variety.default); for (const variety of this.varieties) { if (variety.id === defaultVariety.id) continue; + if (variety.gameDataCached) continue; const { body } = await request.get(`https://pokeapi.co/api/v2/pokemon/${variety.id}`); const { body: formBody } = await request.get(`https://pokeapi.co/api/v2/pokemon-form/${variety.id}`); variety.types.push(...body.types.map(type => firstUpperCase(type.type.name))); @@ -229,6 +233,7 @@ module.exports = class Pokemon { const abilityData = await this.store.abilities.fetch(ability.ability.name); variety.abilities.push(abilityData); } + variety.gameDataCached = true; } return this.varieties; }