mirror of
https://github.com/arthur-pbty/xiao.git
synced 2026-06-24 22:34:46 +02:00
PokemonStore for a shared pokemon cache
This commit is contained in:
@@ -25,49 +25,36 @@ module.exports = class WhosThatPokemonCommand extends Command {
|
||||
}
|
||||
]
|
||||
});
|
||||
|
||||
this.cache = new Map();
|
||||
}
|
||||
|
||||
async run(msg, { hide }) {
|
||||
const pokemon = Math.floor(Math.random() * 802) + 1;
|
||||
try {
|
||||
const data = await this.fetchPokemon(pokemon);
|
||||
const data = await this.client.pokemon.fetch(pokemon.toString());
|
||||
const names = data.names.map(name => name.name.toLowerCase());
|
||||
const displayName = data.names.filter(name => name.language.name === 'en')[0].name;
|
||||
const id = data.id.toString().padStart(3, '0');
|
||||
const attachment = await this.fetchImage(id, hide);
|
||||
await msg.reply('**You have 15 seconds, who\'s that Pokémon?**', { files: [{ attachment, name: `${id}.png` }] });
|
||||
const attachment = await this.fetchImage(data, hide);
|
||||
await msg.reply('**You have 15 seconds, who\'s that Pokémon?**', { files: [attachment] });
|
||||
const msgs = await msg.channel.awaitMessages(res => res.author.id === msg.author.id, {
|
||||
max: 1,
|
||||
time: 15000
|
||||
});
|
||||
if (!msgs.size) return msg.reply(`Sorry, time is up! It was ${displayName}.`);
|
||||
if (!names.includes(msgs.first().content.toLowerCase())) return msg.reply(`Nope, sorry, it's ${displayName}.`);
|
||||
if (!msgs.size) return msg.reply(`Sorry, time is up! It was ${data.name}.`);
|
||||
if (!names.includes(msgs.first().content.toLowerCase())) return msg.reply(`Nope, sorry, it's ${data.name}.`);
|
||||
return msg.reply('Nice job! 10/10! You deserve some cake!');
|
||||
} catch (err) {
|
||||
return msg.reply(`Oh no, an error occurred: \`${err.message}\`. Try again later!`);
|
||||
}
|
||||
}
|
||||
|
||||
async fetchPokemon(pokemon) {
|
||||
if (this.cache.has(pokemon)) return this.cache.get(pokemon);
|
||||
const { body } = await request.get(`https://pokeapi.co/api/v2/pokemon-species/${pokemon}/`);
|
||||
this.cache.set(body.id, {
|
||||
id: body.id,
|
||||
names: body.names
|
||||
});
|
||||
return body;
|
||||
}
|
||||
|
||||
async fetchImage(id, hide = false) {
|
||||
const image = await request.get(`https://www.serebii.net/sunmoon/pokemon/${id}.png`);
|
||||
if (!hide) return image.body;
|
||||
async fetchImage(pokemon, hide = false) {
|
||||
const name = `${pokemon.id}.png`;
|
||||
const image = await request.get(pokemon.spriteImageURL);
|
||||
if (!hide) return { attachment: image.body, name };
|
||||
const base = await loadImage(image.body);
|
||||
const canvas = createCanvas(base.width, base.height);
|
||||
const ctx = canvas.getContext('2d');
|
||||
ctx.drawImage(base, 0, 0);
|
||||
silhouette(ctx, 0, 0, base.width, base.height);
|
||||
return canvas.toBuffer();
|
||||
return { attachment: canvas.toBuffer(), name };
|
||||
}
|
||||
};
|
||||
|
||||
Reference in New Issue
Block a user