mirror of
https://github.com/arthur-pbty/xiao.git
synced 2026-06-03 23:36:43 +02:00
30 lines
806 B
JavaScript
30 lines
806 B
JavaScript
const Collection = require('@discordjs/collection');
|
|
const request = require('node-superfetch');
|
|
const Ability = require('./Ability');
|
|
|
|
module.exports = class AbilityStore extends Collection {
|
|
constructor(options) {
|
|
super(options);
|
|
}
|
|
|
|
async fetch(query) {
|
|
query = this.makeSlug(query);
|
|
if (!query) return null;
|
|
const found = this.find(pokemon => pokemon.slug === query);
|
|
if (found) return found;
|
|
try {
|
|
const { body } = await request.get(`https://pokeapi.co/api/v2/ability/${query}/`);
|
|
const ability = new Ability(this, body);
|
|
this.set(ability.id, ability);
|
|
return ability;
|
|
} catch (err) {
|
|
if (err.status === 404) return null;
|
|
throw err;
|
|
}
|
|
}
|
|
|
|
makeSlug(query) {
|
|
return encodeURIComponent(query.toLowerCase().replaceAll(' ', '-').replace(/[^a-zA-Z0-9-]/g, ''));
|
|
}
|
|
};
|