From 1dca3ae20b9ac06a7b77b9d8e584192146b47205 Mon Sep 17 00:00:00 2001 From: Dragon Fire Date: Wed, 20 Jan 2021 17:37:31 -0500 Subject: [PATCH] Add Smogon Tiers to pokedex-stats --- commands/pokedex/pokedex-stats.js | 3 +++ package.json | 2 +- structures/pokemon/Pokemon.js | 13 +++++++++++++ structures/pokemon/PokemonStore.js | 10 ++++++++++ 4 files changed, 27 insertions(+), 1 deletion(-) diff --git a/commands/pokedex/pokedex-stats.js b/commands/pokedex/pokedex-stats.js index 287d4b59..caafe20c 100644 --- a/commands/pokedex/pokedex-stats.js +++ b/commands/pokedex/pokedex-stats.js @@ -54,6 +54,8 @@ module.exports = class PokedexCommand extends Command { const data = await this.client.pokemon.fetch(pokemon); if (!data) return msg.say('Could not find any results.'); if (!data.gameDataCached) await data.fetchGameData(); + const game = data.id > 807 ? 'ss' : 'sm'; + if (!data.smogonTiers[game]) await data.fetchSmogonTiers(game); const displayForms = data.varieties.filter(vrity => vrity.statsDiffer); const variety = displayForms.find(vrity => { if (!form || form === 'normal') return vrity.default; @@ -89,6 +91,7 @@ module.exports = class PokedexCommand extends Command { \`Total: [${'█'.repeat(repeat.total)}${' '.repeat(20 - repeat.total)}]\` **${statTotal}** `) .addField('❯ Abilities', variety.abilities.join('/')) + .addField('❯ Smogon Tiers', `[${data.smogonTiers[game].join('/')}](${data.smogonURL})`) .addField('❯ Other Forms', stripIndents` _Use ${this.usage(`${data.id}
`)} to get stats for another form._ diff --git a/package.json b/package.json index a2f4f63f..1f0c5c08 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "xiao", - "version": "126.8.1", + "version": "126.8.2", "description": "Your personal server companion.", "main": "Xiao.js", "scripts": { diff --git a/structures/pokemon/Pokemon.js b/structures/pokemon/Pokemon.js index de81a60c..9d2b5e4d 100644 --- a/structures/pokemon/Pokemon.js +++ b/structures/pokemon/Pokemon.js @@ -60,6 +60,7 @@ module.exports = class Pokemon { this.cry = data.id > store.pokemonCountWithCry ? null : path.join(__dirname, '..', '..', 'assets', 'sounds', 'pokedex', `${data.id}.wav`); + this.smogonTiers = data.missingno ? data.smogonTiers : {}; } baseStatTotal(variety) { @@ -137,6 +138,18 @@ module.exports = class Pokemon { return `https://www.serebii.net/pokedex-swsh/${this.displayID}.shtml`; } + get smogonURL() { + if (this.missingno) return null; + return `https://www.smogon.com/dex/${this.id > 807 ? 'ss' : 'sm'}/pokemon/${this.slug}/`; + } + + async fetchSmogonTiers(gen) { + if (!this.store.smogonData[gen.toLowerCase()]) await this.store.fetchSmogonData(gen.toLowerCase()); + const pkmn = this.store.smogonData[gen.toLowerCase()].find(pkmn => pkmn.id === this.id); + this.smogonTiers[gen.toLowerCase()] = pkmn.formats; + return this.smogonTiers[gen.toLowerCase()]; + } + async fetchGameData() { if (this.gameDataCached) return this; await this.fetchDefaultVariety(); diff --git a/structures/pokemon/PokemonStore.js b/structures/pokemon/PokemonStore.js index 04c13475..42334a25 100644 --- a/structures/pokemon/PokemonStore.js +++ b/structures/pokemon/PokemonStore.js @@ -9,6 +9,7 @@ module.exports = class PokemonStore extends Collection { this.pokemonCount = 898; this.pokemonCountWithCry = 893; + this.smogonData = {}; } async fetch(query) { @@ -34,6 +35,15 @@ module.exports = class PokemonStore extends Collection { } } + async fetchSmogonData(gen) { + if (this.smogonData[gen.toLowerCase()]) return this.smogonData[gen.toLowerCase()]; + const { text } = await request.get(`https://www.smogon.com/dex/${gen}/pokemon/`); + this.smogonData[gen.toLowerCase()] = JSON.parse(text.match(/dexSettings = ({.+})/i)[1]) + .injectRpcs[1][1] + .pokemon + .map(pkmn => ({ id: pkmn.oob.dex_number, formats: pkmn.formats })); + } + makeSlug(query) { return encodeURIComponent(query.toLowerCase().replaceAll(' ', '-').replace(/[^a-zA-Z0-9-]/g, '')); }