From 91ea2e708122b2ccf1c0886734a51b1818607257 Mon Sep 17 00:00:00 2001 From: Dragon Fire Date: Thu, 10 Dec 2020 09:35:56 -0500 Subject: [PATCH] Pokedex Location Command --- README.md | 6 ++- assets/json/pokedex-location.json | 32 +++++++++++++ commands/search/pokedex-location.js | 72 +++++++++++++++++++++++++++++ package.json | 2 +- structures/pokemon/Pokemon.js | 42 +++++++++++++---- 5 files changed, 144 insertions(+), 10 deletions(-) create mode 100644 assets/json/pokedex-location.json create mode 100644 commands/search/pokedex-location.js diff --git a/README.md b/README.md index 98b9c351..b7c48b86 100644 --- a/README.md +++ b/README.md @@ -503,6 +503,7 @@ Total: 569 * **paladins:** Responds with information on a Paladins player. * **periodic-table:** Finds an element on the periodic table. * **poem:** Searches for poems by a specific author. +* **pokedex-location:** Responds with the location data for a Pokémon. * **pokedex-moveset:** Responds with the moveset for a Pokémon. * **pokedex:** Searches the Pokédex for a Pokémon. * **pornhub:** Searches Pornhub for your query. (NSFW) @@ -1545,7 +1546,8 @@ here. - [Pokemon Fusion](https://pokemon.alexonsager.net/) * pokemon-fusion (Images) - [PokéAPI](https://pokeapi.co/) - * pokemon-moveset (API) + * pokedex-location (API) + * pokedex-moveset (API) * pokedex (API) * pokemon-cry (API) * whos-that-pokemon (API) @@ -1554,6 +1556,7 @@ here. * 3000-years (Image, Original Game) * dexter (Image, Original Anime) * hat (Ash Hat Original Anime) + * pokedex-location (Images, Original Game) * pokedex-moveset (Images, Original Game) * pokedex (Images, Original Game) * pokemon-cry (Original Game) @@ -1631,6 +1634,7 @@ here. - [SEGA](https://www.sega.com/) * sonic-says ([Image, Original "Sonic the Hedgehog" Game](https://www.sonicthehedgehog.com/)) - [Serebii.net](https://www.serebii.net/index2.shtml) + * pokedex-location (Images) * pokedex-moveset (Images) * pokedex (Images) * whos-that-pokemon (Images) diff --git a/assets/json/pokedex-location.json b/assets/json/pokedex-location.json new file mode 100644 index 00000000..175b61af --- /dev/null +++ b/assets/json/pokedex-location.json @@ -0,0 +1,32 @@ +{ + "red": "Red", + "blue": "Blue", + "yellow": "Yellow", + "gold": "Gold", + "silver": "Silver", + "crystal": "Crystal", + "ruby": "Ruby", + "sapphire": "Sapphire", + "emerald": "Emerald", + "firered": "FireRed", + "leafgreen": "LeafGreen", + "diamond": "Diamond", + "pearl": "Pearl", + "platinum": "Platinum", + "heartgold": "HeartGold", + "soulsilver": "SoulSilver", + "black": "Black", + "white": "White", + "black-2": "Black 2", + "white-2": "White 2", + "x": "X", + "y": "Y", + "alpha-sapphire": "Alpha Sapphire", + "omega-ruby": "Omega Ruby", + "sun": "Sun", + "moon": "Moon", + "ultra-sun": "Ultra Sun", + "ultra-moon": "Ultra Moon", + "sword": "Sword", + "shield": "Shield" +} diff --git a/commands/search/pokedex-location.js b/commands/search/pokedex-location.js new file mode 100644 index 00000000..a6357b95 --- /dev/null +++ b/commands/search/pokedex-location.js @@ -0,0 +1,72 @@ +const Command = require('../../structures/Command'); +const { MessageEmbed } = require('discord.js'); +const versions = require('../../assets/json/pokedex-location'); + +module.exports = class PokedexLocationCommand extends Command { + constructor(client) { + super(client, { + name: 'pokedex-location', + aliases: [ + 'pokemon-location', + 'pokémon-location', + 'pokédex-location', + 'pkmn-location', + 'pokedex-locate', + 'pokémon-locate', + 'pokemon-locate', + 'pokédex-locate', + 'pkmn-locate' + ], + group: 'search', + memberName: 'pokedex-location', + description: 'Responds with the location data for a Pokémon.', + clientPermissions: ['EMBED_LINKS'], + credit: [ + { + name: 'Pokémon', + url: 'https://www.pokemon.com/us/', + reason: 'Images, Original Game' + }, + { + name: 'PokéAPI', + url: 'https://pokeapi.co/', + reason: 'API' + }, + { + name: 'Serebii.net', + url: 'https://www.serebii.net/index2.shtml', + reason: 'Images' + } + ], + args: [ + { + key: 'pokemon', + prompt: 'What Pokémon would you like to get information on?', + type: 'string' + } + ] + }); + } + + async run(msg, { pokemon }) { + try { + const data = await this.client.pokemon.fetch(pokemon); + if (!data) return msg.say('Could not find any results.'); + if (!data.gameDataCached) await data.fetchGameData(); + if (!data.encounters) await data.fetchEncounters(); + const desc = data.encounters.length + ? data.encounters + .map(location => `${location.name} (${location.versions.map(v => versions[v]).join('/')})`) + .join('\n') + : 'Location Unknown' + const embed = new MessageEmbed() + .setColor(0xED1C24) + .setAuthor(`#${data.displayID} - ${data.name}`, data.boxImageURL, data.serebiiURL) + .setDescription(desc) + .setThumbnail(data.spriteImageURL); + 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 fc247805..b6fd34ae 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "xiao", - "version": "123.4.2", + "version": "123.5.0", "description": "Your personal server companion.", "main": "Xiao.js", "scripts": { diff --git a/structures/pokemon/Pokemon.js b/structures/pokemon/Pokemon.js index 6e4cbd5b..1423964b 100644 --- a/structures/pokemon/Pokemon.js +++ b/structures/pokemon/Pokemon.js @@ -2,6 +2,7 @@ const request = require('node-superfetch'); const path = require('path'); const { removeDuplicates, firstUpperCase } = require('../../util/Util'); const missingno = require('../../assets/json/missingno'); +const versions = require('../../assets/json/pokedex-location'); module.exports = class Pokemon { constructor(store, data) { @@ -45,6 +46,8 @@ module.exports = class Pokemon { url: data.evolution_chain ? data.evolution_chain.url : null, data: data.missingno ? missingno.chain : data.evolution_chain ? [] : [data.id] }; + this.encountersURL = null; + this.encounters = data.missingno ? data.encounters : null; this.stats = data.missingno ? data.stats : null; this.height = data.missingno ? data.height : null; this.weight = data.missingno ? data.weight : null; @@ -138,29 +141,30 @@ module.exports = class Pokemon { this.weight = defaultBody.weight * 0.2205; this.heldItems = defaultBody.held_items .filter(item => item.version_details.some(version => { - const inSwordShield = version.version.name === 'sword' || version.version.name === 'shield'; - if (inSwordShield) return true; - if (!inSwordShield && (version.version.name === 'ultra-sun' || version.version.name === 'ultra-moon')) { + const inSwordShield2 = version.version.name === 'sword' || version.version.name === 'shield'; + if (inSwordShield2) return true; + if (!inSwordShield2 && (version.version.name === 'ultra-sun' || version.version.name === 'ultra-moon')) { return true; } return false; })) .map(item => { - const inSwordShield = item.version_details + const inSwordShield2 = item.version_details .some(version => version.version.name === 'sword' || version.version.name === 'shield'); - const rarity = item.version_details + const { rarity } = item.version_details .find(version => { - if (inSwordShield) return true; + if (inSwordShield2) return true; const sunMoon = version.version.name === 'ultra-sun' || version.version.name === 'ultra-moon'; - if (!inSwordShield && sunMoon) return true; + if (!inSwordShield2 && sunMoon) return true; return false; - }).rarity; + }); return { url: item.item.url, name: null, rarity }; }); + this.encountersURL = defaultBody.location_area_encounters; await this.fetchHeldItemNames(); await this.fetchChain(); this.gameDataCached = true; @@ -200,4 +204,26 @@ module.exports = class Pokemon { } return this.heldItems; } + + async fetchEncounters() { + if (!this.encountersURL) return null; + if (this.encounters.length) return this.encounters; + const { body } = await request.get(this.encountersURL); + if (!body.length) { + this.encounters = body; + return body; + } + for (const encounter of body) { + if (!encounter.version_details.some(version => versions[version.version.name])) continue; + const { body: encounterBody } = await request.get(encounter.location_area.url); + const { body: locationBody } = await request.get(encounterBody.location.url); + this.encounters.push({ + name: locationBody.names.find(name => name.language.name === 'en').name, + versions: encounter.version_details + .filter(version => versions[version.version.name]) + .map(version => version.version.name) + }); + } + return this.encounters; + } };