Pokedex Item Command

This commit is contained in:
Dragon Fire
2021-01-31 09:57:40 -05:00
parent fbe901a249
commit 08f9a62608
8 changed files with 109 additions and 8 deletions
+19
View File
@@ -0,0 +1,19 @@
const { firstUpperCase } = require('../../util/Util');
module.exports = class Item {
constructor(store, data) {
this.store = store;
this.id = data.id;
const slugName = firstUpperCase(data.name).replaceAll('-', ' ');
this.name = data.names.length
? data.names.find(entry => entry.language.name === 'en').name
: slugName;
this.description = data.effect_entries.length
? data.effect_entries.find(entry => entry.language.name === 'en').effect
: data.flavor_text_entries.length
? data.flavor_text_entries.find(entry => entry.language.name === 'en').flavor_text
: null;
this.spriteURL = data.sprites.default || null;
this.cost = data.cost || 0;
}
};
+26
View File
@@ -0,0 +1,26 @@
const Collection = require('@discordjs/collection');
const request = require('node-superfetch');
const Item = require('./Item');
module.exports = class ItemStore extends Collection {
async fetch(query) {
if (this.has(query)) return this.get(query);
query = this.makeSlug(query.toString());
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/item/${query}/`);
const item = new Item(this, body);
this.set(item.id, item);
return item;
} catch (err) {
if (err.status === 404) return null;
throw err;
}
}
makeSlug(query) {
return encodeURIComponent(query.toLowerCase().replaceAll(' ', '-').replace(/[^a-zA-Z0-9-]/g, ''));
}
};
+4 -4
View File
@@ -269,8 +269,8 @@ module.exports = class Pokemon {
return false;
});
return {
url: item.item.url,
name: null,
data: null,
slug: item.item.name,
rarity
};
});
@@ -306,8 +306,8 @@ module.exports = class Pokemon {
async fetchHeldItemNames() {
for (const item of this.heldItems) {
const { body } = await request.get(item.url);
item.name = body.names.find(name => name.language.name === 'en').name;
const data = await this.store.items.fetch(item.slug);
item.data = data;
}
return this.heldItems;
}
+2
View File
@@ -3,6 +3,7 @@ const request = require('node-superfetch');
const Pokemon = require('./Pokemon');
const MoveStore = require('./MoveStore');
const AbilityStore = require('./AbilityStore');
const ItemStore = require('./ItemStore');
const missingno = require('../../assets/json/missingno');
module.exports = class PokemonStore extends Collection {
@@ -14,6 +15,7 @@ module.exports = class PokemonStore extends Collection {
this.smogonData = {};
this.moves = new MoveStore();
this.abilities = new AbilityStore();
this.items = new ItemStore();
}
async fetch(query) {