diff --git a/.gitignore b/.gitignore index b8c3d51e..2f816d23 100644 --- a/.gitignore +++ b/.gitignore @@ -2,3 +2,6 @@ node_modules/ package-lock.json logs/ *.log + +commands/games/box-choosing.js +assets/json/box-choosing.json diff --git a/commands/search/pokedex.js b/commands/search/pokedex.js index c39a719d..c86f4272 100644 --- a/commands/search/pokedex.js +++ b/commands/search/pokedex.js @@ -17,7 +17,7 @@ module.exports = class PokedexCommand extends Command { key: 'pokemon', prompt: 'What Pokémon would you like to get information on?', type: 'string', - parse: pokemon => encodeURIComponent(pokemon.toLowerCase().replace(/ /g, '-')) + parse: pokemon => this.makeSlug(pokemon) } ] }); @@ -37,7 +37,7 @@ module.exports = class PokedexCommand extends Command { ) .setDescription(stripIndents` **The ${data.genus}** - ${this.filterPokemonData(data.entries).flavor_text.replace(/\n|\f|\r/g, ' ')} + ${data.entries[Math.floor(Math.random() * data.entries.length)]} `) .setThumbnail(`https://www.serebii.net/sunmoon/pokemon/${data.id}.png`); return msg.embed(embed); @@ -48,23 +48,32 @@ module.exports = class PokedexCommand extends Command { } async fetchPokemon(query) { - if (this.cache.has(query)) return this.cache.get(query); - const found = this.cache.find( - pokemon => pokemon.name.toLowerCase() === query.toLowerCase() || pokemon.id === query - ); + const num = Number.parseInt(query, 10); + if (this.cache.has(num)) return this.cache.get(num); + const found = this.cache.find(pokemon => pokemon.slug === query); if (found) return found; const { body } = await request.get(`https://pokeapi.co/api/v2/pokemon-species/${query}/`); + const entries = body.flavor_text_entries + .filter(entry => entry.language.name === 'en') + .map(entry => entry.flavor_text.replace(/\n|\f|\r/g, ' ')); + const { name } = this.filterPokemonData(body.names); this.cache.set(body.id, { - id: body.id.toString().padStart(3, '0'), - name: this.filterPokemonData(body.names, false).name, - genus: this.filterPokemonData(body.genera, false).genus, - entries: body.flavor_text_entries + id: body.id, + displayID: body.id.toString().padStart(3, '0'), + name, + slug: this.makeSlug(name), + genus: this.filterPokemonData(body.genera).genus, + entries }); return this.cache.get(body.id); } - filterPokemonData(arr, random = true) { + filterPokemonData(arr) { const filtered = arr.filter(entry => entry.language.name === 'en'); - return filtered[random ? Math.floor(Math.random() * filtered.length) : 0]; + return filtered[0]; + } + + makeSlug(name) { + return encodeURIComponent(name.toLowerCase().replace(/ /g, '-').replace(/[^a-zA-Z0-9-]/g, '')); } }; diff --git a/package.json b/package.json index c72d2029..c76e9c40 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "xiao", - "version": "84.1.2", + "version": "84.1.3", "description": "Your personal server companion.", "main": "Xiao.js", "scripts": {