custom PokeAPI middleware

This commit is contained in:
Emily 2020-11-02 13:13:34 +11:00
parent 678aed92e4
commit c81ca81d2e
4 changed files with 96 additions and 113 deletions

View file

@ -1,6 +1,5 @@
const Embed = require('../../util/embed');
const Pokedex = require('pokedex-promise-v2');
const Dex = new Pokedex();
const API = new (require('../../util/pokeapi'));
module.exports = class {
constructor (name, category) {
@ -21,98 +20,58 @@ module.exports = class {
}
async run (client, message, args, data) { //eslint-disable-line no-unused-vars
const forms = [
'mega',
'alola',
'alolan',
'galar',
'galarian'
];
let query = args.join(' ');
let form = '';
let spritePrefix = '';
for (let i = 0; i < forms.length; i++) {
if (query.indexOf(forms[i]) > -1) {
query = query.replace(forms[i], '');
if (forms[i] === '-galarian') {
form += 'galar';
} else if (forms[i] === '-alolan') {
form += '-alola';
} else {
form += `-${forms[i]}`;
}
if (forms[i] === 'alola') {
spritePrefix = 'alolan-';
} else if (forms[i] === 'galar') {
spritePrefix = 'galarian-';
} else {
spritePrefix = `${forms[i]}-`;
}
break;
}
}
query = query.trim();
const pokemon = await Dex.resource('/api/v2/pokemon/' + query + form);
const pokeSpecies = await Dex.resource('/api/v2/pokemon-species/' + pokemon.species.name);
let evoData = await Dex.resource(pokeSpecies.evolution_chain.url);
evoData = evoData.chain;
const pokemon = await API.fetchPokemon(args.join(' '));
const typeArray = [];
for (let i = 0; i < pokemon.types.length; i++) {
typeArray.push(pokemon.types[i].type.name.toProperCase());
for (let i = 0; i < pokemon.info.types.length; i++) {
typeArray.push(pokemon.info.types[i].type.name.toProperCase());
}
const abilityArray = [];
for (let i = 0; i < pokemon.abilities.length; i++) {
let ability = pokemon.abilities[i].ability.name.toProperCase();
if (pokemon.abilities[i].is_hidden === true) ability = `*${ability}*`;
for (let i = 0; i < pokemon.info.abilities.length; i++) {
let ability = pokemon.info.abilities[i].ability.name.toProperCase();
if (pokemon.info.abilities[i].is_hidden === true) ability = `*${ability}*`;
abilityArray.push(ability);
}
const evoArray = [];
do {
const numberOfEvolutions = evoData.evolves_to.length;
const numberOfEvolutions = pokemon.evolutions.chain.evolves_to.length;
evoArray.push(evoData.species.name.toProperCase());
evoArray.push(pokemon.evolutions.chain.species.name.toProperCase());
if (numberOfEvolutions > 1) {
for (let i = 1;i < numberOfEvolutions; i++) {
evoArray.push(evoData.species.name.toProperCase());
evoArray.push(pokemon.evolutions.chain.species.name.toProperCase());
}
}
evoData = evoData.evolves_to[0];
pokemon.evolutions.chain = pokemon.evolutions.chain.evolves_to[0];
} while (evoData != undefined && evoData.hasOwnProperty('evolves_to')); //eslint-disable-line no-prototype-builtins
} while (pokemon.evolutions.chain != undefined && pokemon.evolutions.chain.hasOwnProperty('evolves_to')); //eslint-disable-line no-prototype-builtins
console.log(evoArray)
const filtered = pokeSpecies.flavor_text_entries.filter(text => text.language.name === 'en');
const filtered = pokemon.species.flavor_text_entries.filter(text => text.language.name === 'en');
const description = filtered[0].flavor_text.replace(/(\r\n|\n|\r)/gm, ' ').replace(/\s+/g,' ');
const sprite = API.fetchSprite(query);
let spritePath = 'normal';
const random = client.functions.intBetween(0, 100);
if (random === 69) spritePath = 'shiny';
console.log(`https://raw.githubusercontent.com/woomyware/rotom-b-data/master/sprites/pokemon/${spritePath}/${spritePrefix}${query}.gif`);
const embed = new Embed()
.setTitle(pokemon.name.toProperCase())
.setThumbnail(`https://raw.githubusercontent.com/woomyware/rotom-b-data/master/sprites/pokemon/${spritePath}/${spritePrefix}${query}.gif`)
.setTitle(pokemon.info.name.toProperCase())
.setThumbnail(sprite)
.setDescription(description.replace('\n', ''))
.addField('**Types:**', typeArray.join(', '), true)
.addField('**Abilities:**', abilityArray.join(', '), true);
if (evoArray.length > 1) embed.addField('**Evolution Chain:**', evoArray.join(' → ').replace(pokeSpecies.name.toProperCase(), `**${pokeSpecies.name.toProperCase()}**`));
embed.addField('**Base Stats:**', `**HP:** ${pokemon.stats[0].base_stat} **Atk:** ${pokemon.stats[1].base_stat} **Def:** ${pokemon.stats[2].base_stat} **SpA:** ${pokemon.stats[3].base_stat} **SpD:** ${pokemon.stats[4].base_stat} **Spe:** ${pokemon.stats[5].base_stat}`);
if (evoArray.length > 1) embed.addField('**Evolution Chain:**', evoArray.join(' → ').replace(pokemon.species.name.toProperCase(), `**${pokemon.species.name.toProperCase()}**`));
embed.addField('**Base Stats:**', `**HP:** ${pokemon.info.stats[0].base_stat} **Atk:** ${pokemon.info.stats[1].base_stat} **Def:** ${pokemon.info.stats[2].base_stat} **SpA:** ${pokemon.info.stats[3].base_stat} **SpD:** ${pokemon.info.stats[4].base_stat} **Spe:** ${pokemon.info.stats[5].base_stat}`);
message.channel.createMessage({ embed: embed });
}
};

76
bot/util/pokeapi.js Normal file
View file

@ -0,0 +1,76 @@
const fetch = require('node-fetch');
const { Collection } = require('eris');
class PokeAPI {
constructor (client) {
this.client = client;
this.cache = new Collection();
this.url = 'https://pokeapi.co/api/v2';
this.forms = [
'mega',
'alola',
'alolan',
'galar',
'galarian'
];
}
async fetchPokemon (query) {
let form = '';
for (let i = 0; i < this.forms.length; i++) {
if (query.indexOf( this.forms[i]) > -1) {
query = query.replace( this.forms[i], '');
if ( this.forms[i] === '-galarian') {
form += 'galar';
} else if ( this.forms[i] === '-alolan') {
form += '-alola';
} else {
form += `-${ this.forms[i]}`;
}
break;
}
}
query = query.trim();
if (this.cache.has(query + form)) return this.cache.get(query + form);
const pokeData = {};
const info = await fetch(this.url + '/pokemon/' + query + form);
pokeData.info = await info.json();
const species = await fetch(this.url + '/pokemon-species/' + pokeData.info.species.name);
pokeData.species = await species.json();
const evolutions = await fetch(pokeData.species.evolution_chain.url);
pokeData.evolutions = await evolutions.json();
this.cache.set(query + form, pokeData);
return pokeData;
}
fetchSprite (query) {
let spritePrefix = '';
for (let i = 0; i < this.forms.length; i++) {
if (query.indexOf( this.forms[i]) > -1) {
query = query.replace( this.forms[i], '');
if ( this.forms[i] === 'alola') {
spritePrefix = 'alolan-';
} else if ( this.forms[i] === 'galar') {
spritePrefix = 'galarian-';
} else {
spritePrefix = `${ this.forms[i]}-`;
}
break;
}
}
let spritePath = 'normal';
if (Math.floor((Math.random() * 100) + 1) === 69) spritePath = 'shiny';
return `https://raw.githubusercontent.com/woomyware/rotom-b-data/master/sprites/pokemon/${spritePath}/${spritePrefix}${query}.gif`;
}
}
module.exports = PokeAPI;