From 411222cb150f7ff9f271042a25d44a55401a9632 Mon Sep 17 00:00:00 2001 From: Dragon Fire Date: Tue, 24 Mar 2020 21:58:11 -0400 Subject: [PATCH] Add Types to Pokedex --- assets/json/missingno.json | 12 ++++++++++++ commands/search/pokedex.js | 6 +++++- package.json | 2 +- structures/pokemon/Pokemon.js | 22 +++++++++++++++++++++- 4 files changed, 39 insertions(+), 3 deletions(-) diff --git a/assets/json/missingno.json b/assets/json/missingno.json index d66f061c..f1d83c70 100644 --- a/assets/json/missingno.json +++ b/assets/json/missingno.json @@ -30,6 +30,18 @@ "genus": "??? Pokémon" } ], + "varieties": [ + { + "is_default": true, + "pokemon": { + "name": "missingno" + } + } + ], + "types": [ + "Bird", + "Normal" + ], "missingno": true, "sprite": "https://cdn.bulbagarden.net/upload/9/98/Missingno_RB.png", "box": "https://cdn.bulbagarden.net/upload/1/1f/AniMS_Missingno_I.png", diff --git a/commands/search/pokedex.js b/commands/search/pokedex.js index 5fc95e0d..8b6fde6e 100644 --- a/commands/search/pokedex.js +++ b/commands/search/pokedex.js @@ -42,6 +42,7 @@ module.exports = class PokedexCommand extends Command { try { const data = await this.client.pokemon.fetch(pokemon); if (!data) return msg.say('Could not find any results.'); + if (!data.typesCached) await data.fetchTypes(); const embed = new MessageEmbed() .setColor(0xED1C24) .setAuthor(`#${data.displayID} - ${data.name}`, data.boxImageURL, data.serebiiURL) @@ -49,7 +50,10 @@ module.exports = class PokedexCommand extends Command { **${data.genus}** ${data.entries[Math.floor(Math.random() * data.entries.length)]} `) - .setThumbnail(data.spriteImageURL); + .setThumbnail(data.spriteImageURL) + .addField('❯ Types', data.varieties.map( + variety => `${variety.types.join('/')}${variety.name ? ` (${variety.name})` : ''}` + ).join('\n')); 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 e88d4847..c1a48f93 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "xiao", - "version": "112.14.3", + "version": "112.14.4", "description": "Your personal server companion.", "main": "Xiao.js", "scripts": { diff --git a/structures/pokemon/Pokemon.js b/structures/pokemon/Pokemon.js index 86d35eb9..81471034 100644 --- a/structures/pokemon/Pokemon.js +++ b/structures/pokemon/Pokemon.js @@ -1,4 +1,5 @@ -const { removeDuplicates } = require('../../util/Util'); +const request = require('node-superfetch'); +const { removeDuplicates, firstUpperCase } = require('../../util/Util'); const missingno = require('../../assets/json/missingno'); module.exports = class Pokemon { @@ -10,6 +11,11 @@ module.exports = class Pokemon { .map(entry => entry.flavor_text.replace(/\n|\f|\r/g, ' '))); this.names = data.names.map(entry => ({ name: entry.name, language: entry.language.name })); this.genus = `The ${data.genera.filter(entry => entry.language.name === 'en')[0].genus}`; + this.varieties = data.varieties.map(variety => { + const name = firstUpperCase(variety.pokemon.name.replace(new RegExp(`${this.slug}-?`, 'i'), '')); + return { id: variety.pokemon.name, name: name || null, default: variety.is_default, types: [] }; + }); + this.typesCached = false; this.missingno = data.missingno || false; } @@ -35,4 +41,18 @@ module.exports = class Pokemon { if (this.missingno) return missingno.url; return `https://www.serebii.net/pokedex-sm/${this.displayID}.shtml`; } + + async fetchTypes() { + if (this.typesCached) return this; + if (this.missingno) { + this.varieties[0].types.push(...missingno.types); + } else { + for (const variety of this.varieties) { + const { body } = await request.get(`https://pokeapi.co/api/v2/pokemon/${variety.id}`); + variety.types.push(...body.types.map(type => firstUpperCase(type.type.name))); + } + } + this.typesCached = true; + return this; + } };