From 08f9a626080994d2477144322fb408d31d482e6a Mon Sep 17 00:00:00 2001 From: Dragon Fire Date: Sun, 31 Jan 2021 09:57:40 -0500 Subject: [PATCH] Pokedex Item Command --- README.md | 5 ++- commands/pokedex/pokedex-item.js | 50 ++++++++++++++++++++++++++++++ commands/pokedex/pokedex.js | 5 +-- package.json | 2 +- structures/pokemon/Item.js | 19 ++++++++++++ structures/pokemon/ItemStore.js | 26 ++++++++++++++++ structures/pokemon/Pokemon.js | 8 ++--- structures/pokemon/PokemonStore.js | 2 ++ 8 files changed, 109 insertions(+), 8 deletions(-) create mode 100644 commands/pokedex/pokedex-item.js create mode 100644 structures/pokemon/Item.js create mode 100644 structures/pokemon/ItemStore.js diff --git a/README.md b/README.md index 24cd874c..e17301ca 100644 --- a/README.md +++ b/README.md @@ -261,7 +261,7 @@ in the appropriate channel's topic to use it. ## Commands -Total: 587 +Total: 588 ### Utility: @@ -535,6 +535,7 @@ Total: 587 * **pokedex-ability:** Searches the Pokédex for a Pokémon ability. * **pokedex-cry:** Plays a Pokémon's cry. * **pokedex-image:** Responds with the image of a Pokémon. +* **pokedex-item:** Searches the Pokédex for a Pokémon item. * **pokedex-location:** Responds with the location data for a Pokémon. * **pokedex-move:** Searches the Pokédex for a Pokémon move. * **pokedex-moveset:** Responds with the moveset for a Pokémon. @@ -1621,6 +1622,7 @@ here. * pokedex-ability (API) * pokedex-cry (API) * pokedex-image (API) + * pokedex-item (API) * pokedex-location (API) * pokedex-move (API) * pokedex-moveset (API) @@ -1637,6 +1639,7 @@ here. * pokedex-ability (Original Game) * pokedex-cry (Original Game) * pokedex-image (Images, Original Game) + * pokedex-item (Images, Original Game) * pokedex-location (Images, Original Game) * pokedex-move (Original Game) * pokedex-moveset (Images, Original Game) diff --git a/commands/pokedex/pokedex-item.js b/commands/pokedex/pokedex-item.js new file mode 100644 index 00000000..8aee4a18 --- /dev/null +++ b/commands/pokedex/pokedex-item.js @@ -0,0 +1,50 @@ +const Command = require('../../structures/Command'); +const { MessageEmbed } = require('discord.js'); + +module.exports = class PokedexItemCommand extends Command { + constructor(client) { + super(client, { + name: 'pokedex-item', + aliases: ['pokemon-item', 'pokémon-item', 'pokédex-item', 'pkmn-item'], + group: 'pokedex', + memberName: 'pokedex-item', + description: 'Searches the Pokédex for a Pokémon item.', + 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' + } + ], + args: [ + { + key: 'item', + prompt: 'What item would you like to get information on?', + type: 'string' + } + ] + }); + } + + async run(msg, { item }) { + try { + const data = await this.client.pokemon.items.fetch(item); + if (!data) return msg.say('Could not find any results.'); + const embed = new MessageEmbed() + .setColor(0xED1C24) + .setTitle(data.name) + .setDescription(data.description || 'No description available.') + .setThumbnail(data.spriteURL) + .addField('❯ Cost', `${data.cost} ¥`, true); + return msg.embed(embed); + } catch (err) { + return msg.reply(`Oh no, an error occurred: \`${err.message}\`. Try again later!`); + } + } +}; diff --git a/commands/pokedex/pokedex.js b/commands/pokedex/pokedex.js index e9b25996..e17c0ac4 100644 --- a/commands/pokedex/pokedex.js +++ b/commands/pokedex/pokedex.js @@ -108,8 +108,9 @@ module.exports = class PokedexCommand extends Command { return `${variety.types.join('/')}${showParens ? ` (${variety.name})` : ''}`; }).join('\n')) .addField('❯ Evolution Chain', `${evoChain}${data.mega ? ` -> ${this.megaEvolveEmoji}` : ''}`) - .addField('❯ Held Items', - data.heldItems.length ? data.heldItems.map(item => `${item.name} (${item.rarity}%)`).join('\n') : 'None') + .addField('❯ Held Items', data.heldItems.length + ? data.heldItems.map(item => `${item.data.name} (${item.rarity}%)`).join('\n') + : 'None') .addField('❯ Gender Rate', data.genderRate.genderless ? 'Genderless' : `♂️ ${data.genderRate.male}% ♀️ ${data.genderRate.female}%`); if (data.cry) { diff --git a/package.json b/package.json index 3b727d76..b0e870a5 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "xiao", - "version": "128.2.1", + "version": "128.3.0", "description": "Your personal server companion.", "main": "Xiao.js", "scripts": { diff --git a/structures/pokemon/Item.js b/structures/pokemon/Item.js new file mode 100644 index 00000000..69c9220e --- /dev/null +++ b/structures/pokemon/Item.js @@ -0,0 +1,19 @@ +const { firstUpperCase } = require('../../util/Util'); + +module.exports = class Item { + constructor(store, data) { + this.store = store; + this.id = data.id; + const slugName = firstUpperCase(data.name).replaceAll('-', ' '); + this.name = data.names.length + ? data.names.find(entry => entry.language.name === 'en').name + : slugName; + this.description = data.effect_entries.length + ? data.effect_entries.find(entry => entry.language.name === 'en').effect + : data.flavor_text_entries.length + ? data.flavor_text_entries.find(entry => entry.language.name === 'en').flavor_text + : null; + this.spriteURL = data.sprites.default || null; + this.cost = data.cost || 0; + } +}; diff --git a/structures/pokemon/ItemStore.js b/structures/pokemon/ItemStore.js new file mode 100644 index 00000000..8875508b --- /dev/null +++ b/structures/pokemon/ItemStore.js @@ -0,0 +1,26 @@ +const Collection = require('@discordjs/collection'); +const request = require('node-superfetch'); +const Item = require('./Item'); + +module.exports = class ItemStore extends Collection { + async fetch(query) { + if (this.has(query)) return this.get(query); + query = this.makeSlug(query.toString()); + if (!query) return null; + const found = this.find(pokemon => pokemon.slug === query); + if (found) return found; + try { + const { body } = await request.get(`https://pokeapi.co/api/v2/item/${query}/`); + const item = new Item(this, body); + this.set(item.id, item); + return item; + } catch (err) { + if (err.status === 404) return null; + throw err; + } + } + + makeSlug(query) { + return encodeURIComponent(query.toLowerCase().replaceAll(' ', '-').replace(/[^a-zA-Z0-9-]/g, '')); + } +}; diff --git a/structures/pokemon/Pokemon.js b/structures/pokemon/Pokemon.js index d1461adb..f65f5f97 100644 --- a/structures/pokemon/Pokemon.js +++ b/structures/pokemon/Pokemon.js @@ -269,8 +269,8 @@ module.exports = class Pokemon { return false; }); return { - url: item.item.url, - name: null, + data: null, + slug: item.item.name, rarity }; }); @@ -306,8 +306,8 @@ module.exports = class Pokemon { async fetchHeldItemNames() { for (const item of this.heldItems) { - const { body } = await request.get(item.url); - item.name = body.names.find(name => name.language.name === 'en').name; + const data = await this.store.items.fetch(item.slug); + item.data = data; } return this.heldItems; } diff --git a/structures/pokemon/PokemonStore.js b/structures/pokemon/PokemonStore.js index a3399921..59d11e8c 100644 --- a/structures/pokemon/PokemonStore.js +++ b/structures/pokemon/PokemonStore.js @@ -3,6 +3,7 @@ const request = require('node-superfetch'); const Pokemon = require('./Pokemon'); const MoveStore = require('./MoveStore'); const AbilityStore = require('./AbilityStore'); +const ItemStore = require('./ItemStore'); const missingno = require('../../assets/json/missingno'); module.exports = class PokemonStore extends Collection { @@ -14,6 +15,7 @@ module.exports = class PokemonStore extends Collection { this.smogonData = {}; this.moves = new MoveStore(); this.abilities = new AbilityStore(); + this.items = new ItemStore(); } async fetch(query) {