mirror of
https://github.com/arthur-pbty/xiao.git
synced 2026-06-07 14:55:40 +02:00
PokemonStore for a shared pokemon cache
This commit is contained in:
@@ -0,0 +1,10 @@
|
||||
const { CommandoClient } = require('discord.js-commando');
|
||||
const PokemonStore = require('./PokemonStore');
|
||||
|
||||
module.exports = class XiaoClient extends CommandoClient {
|
||||
constructor(options) {
|
||||
super(options);
|
||||
|
||||
this.pokemon = new PokemonStore();
|
||||
}
|
||||
};
|
||||
@@ -0,0 +1,31 @@
|
||||
module.exports = class Pokemon {
|
||||
constructor(data) {
|
||||
this.id = data.id;
|
||||
this.name = data.names.filter(entry => entry.language.name === 'en')[0].name;
|
||||
this.entries = data.flavor_text_entries
|
||||
.filter(entry => entry.language.name === 'en')
|
||||
.map(entry => entry.flavor_text.replace(/\n|\f|\r/g, ' '));
|
||||
this.names = data.names;
|
||||
this.genus = `The ${data.genera.filter(entry => entry.language.name === 'en')[0].genus}`;
|
||||
}
|
||||
|
||||
get displayID() {
|
||||
return this.id.toString().padStart(3, '0');
|
||||
}
|
||||
|
||||
get slug() {
|
||||
return encodeURIComponent(this.name.toLowerCase().replace(/ /g, '-').replace(/[^a-zA-Z0-9-]/g, ''));
|
||||
}
|
||||
|
||||
get spriteImageURL() {
|
||||
return `https://www.serebii.net/sunmoon/pokemon/${this.displayID}.png`;
|
||||
}
|
||||
|
||||
get boxImageURL() {
|
||||
return `https://www.serebii.net/pokedex-sm/icon/${this.displayID}.png`;
|
||||
}
|
||||
|
||||
get serebiiURL() {
|
||||
return `https://www.serebii.net/pokedex-sm/${this.displayID}.shtml`;
|
||||
}
|
||||
};
|
||||
@@ -0,0 +1,21 @@
|
||||
const { Collection } = require('discord.js');
|
||||
const request = require('node-superfetch');
|
||||
const Pokemon = require('./Pokemon');
|
||||
|
||||
module.exports = class PokemonStore extends Collection {
|
||||
constructor(iterable) {
|
||||
super(iterable);
|
||||
}
|
||||
|
||||
async fetch(query) {
|
||||
query = encodeURIComponent(query.toLowerCase().replace(/ /g, '-').replace(/[^a-zA-Z0-9-]/g, ''));
|
||||
const num = Number.parseInt(query, 10);
|
||||
if (this.has(num)) return this.get(num);
|
||||
const found = this.find(pokemon => pokemon.slug === query);
|
||||
if (found) return found;
|
||||
const { body } = await request.get(`https://pokeapi.co/api/v2/pokemon-species/${query}/`);
|
||||
const pokemon = new Pokemon(body);
|
||||
this.set(pokemon.id, pokemon);
|
||||
return pokemon;
|
||||
}
|
||||
};
|
||||
Reference in New Issue
Block a user