mirror of
https://github.com/arthur-pbty/xiao.git
synced 2026-06-24 14:19:56 +02:00
Cache in pokedex, cache less in whos-that-pokemon
This commit is contained in:
@@ -53,7 +53,10 @@ module.exports = class WhosThatPokemonCommand extends Command {
|
|||||||
async fetchPokemon(pokemon) {
|
async fetchPokemon(pokemon) {
|
||||||
if (this.cache.has(pokemon)) return this.cache.get(pokemon);
|
if (this.cache.has(pokemon)) return this.cache.get(pokemon);
|
||||||
const { body } = await request.get(`https://pokeapi.co/api/v2/pokemon-species/${pokemon}/`);
|
const { body } = await request.get(`https://pokeapi.co/api/v2/pokemon-species/${pokemon}/`);
|
||||||
this.cache.set(body.id, body);
|
this.cache.set(body.id, {
|
||||||
|
id: body.id,
|
||||||
|
names: body.names
|
||||||
|
});
|
||||||
return body;
|
return body;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -1,5 +1,5 @@
|
|||||||
const Command = require('../../structures/Command');
|
const Command = require('../../structures/Command');
|
||||||
const { MessageEmbed } = require('discord.js');
|
const { Collection, MessageEmbed } = require('discord.js');
|
||||||
const request = require('node-superfetch');
|
const request = require('node-superfetch');
|
||||||
const { stripIndents } = require('common-tags');
|
const { stripIndents } = require('common-tags');
|
||||||
|
|
||||||
@@ -21,24 +21,25 @@ module.exports = class PokedexCommand extends Command {
|
|||||||
}
|
}
|
||||||
]
|
]
|
||||||
});
|
});
|
||||||
|
|
||||||
|
this.cache = new Collection();
|
||||||
}
|
}
|
||||||
|
|
||||||
async run(msg, { pokemon }) {
|
async run(msg, { pokemon }) {
|
||||||
try {
|
try {
|
||||||
const { body } = await request.get(`https://pokeapi.co/api/v2/pokemon-species/${pokemon}/`);
|
const data = await this.fetchPokemon(pokemon);
|
||||||
const id = body.id.toString().padStart(3, '0');
|
|
||||||
const embed = new MessageEmbed()
|
const embed = new MessageEmbed()
|
||||||
.setColor(0xED1C24)
|
.setColor(0xED1C24)
|
||||||
.setAuthor(
|
.setAuthor(
|
||||||
`#${id} - ${this.filterPokemonData(body.names, false).name}`,
|
`#${data.id} - ${data.name}`,
|
||||||
`https://www.serebii.net/pokedex-sm/icon/${id}.png`,
|
`https://www.serebii.net/pokedex-sm/icon/${data.id}.png`,
|
||||||
`https://www.serebii.net/pokedex-sm/${id}.shtml`
|
`https://www.serebii.net/pokedex-sm/${data.id}.shtml`
|
||||||
)
|
)
|
||||||
.setDescription(stripIndents`
|
.setDescription(stripIndents`
|
||||||
**The ${this.filterPokemonData(body.genera, false).genus}**
|
**The ${data.genus}**
|
||||||
${this.filterPokemonData(body.flavor_text_entries).flavor_text.replace(/\n|\f|\r/g, ' ')}
|
${this.filterPokemonData(data.entries).flavor_text.replace(/\n|\f|\r/g, ' ')}
|
||||||
`)
|
`)
|
||||||
.setThumbnail(`https://www.serebii.net/sunmoon/pokemon/${id}.png`);
|
.setThumbnail(`https://www.serebii.net/sunmoon/pokemon/${data.id}.png`);
|
||||||
return msg.embed(embed);
|
return msg.embed(embed);
|
||||||
} catch (err) {
|
} catch (err) {
|
||||||
if (err.status === 404) return msg.say('Could not find any results.');
|
if (err.status === 404) return msg.say('Could not find any results.');
|
||||||
@@ -46,6 +47,22 @@ 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
|
||||||
|
);
|
||||||
|
if (found) return found;
|
||||||
|
const { body } = await request.get(`https://pokeapi.co/api/v2/pokemon-species/${query}/`);
|
||||||
|
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
|
||||||
|
});
|
||||||
|
return this.cache.get(body.id);
|
||||||
|
}
|
||||||
|
|
||||||
filterPokemonData(arr, random = true) {
|
filterPokemonData(arr, random = true) {
|
||||||
const filtered = arr.filter(entry => entry.language.name === 'en');
|
const filtered = arr.filter(entry => entry.language.name === 'en');
|
||||||
return filtered[random ? Math.floor(Math.random() * filtered.length) : 0];
|
return filtered[random ? Math.floor(Math.random() * filtered.length) : 0];
|
||||||
|
|||||||
+1
-1
@@ -1,6 +1,6 @@
|
|||||||
{
|
{
|
||||||
"name": "xiao",
|
"name": "xiao",
|
||||||
"version": "84.1.1",
|
"version": "84.1.2",
|
||||||
"description": "Your personal server companion.",
|
"description": "Your personal server companion.",
|
||||||
"main": "Xiao.js",
|
"main": "Xiao.js",
|
||||||
"scripts": {
|
"scripts": {
|
||||||
|
|||||||
Reference in New Issue
Block a user