Created
July 12, 2019 00:14
-
-
Save anchetaWern/4dfc90e5d2ffec775c7fc98204bdfdb7 to your computer and use it in GitHub Desktop.
Pokedex Bot: Pokemon description and evolution endpoint in server.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
if (pokemon_species_endpoint.indexOf(specs) !== -1 || intent.displayName == 'evolution') { | |
const { data } = await axios_instance.get(`/pokemon-species/${pokemon}`); | |
const evolution_chain_id = data.evolution_chain.url.split('/')[6]; | |
const text = data.flavor_text_entries.find(item => { | |
return item.language.name == 'en'; | |
}); | |
let fulfillmentText; | |
if (specs == 'description') { | |
fulfillmentText = `${pokemon}:\n\n ${text.flavor_text}`; | |
Object.assign(response_obj, { | |
fulfillmentText | |
}); | |
} | |
if (intent.displayName == 'evolution') { | |
const evolution_response = await axios_instance.get(`/evolution-chain/${evolution_chain_id}`); | |
const evolution_requirement = parameters.evolutions; | |
let pokemon_evolutions = [evolution_response.data.chain.species.name]; | |
fulfillmentText = `${pokemon} has no evolution`; | |
if (evolution_response.data.chain.evolves_to.length) { | |
pokemon_evolutions.push(evolution_response.data.chain.evolves_to[0].species.name); | |
} | |
if (evolution_response.data.chain.evolves_to[0].evolves_to.length) { | |
pokemon_evolutions.push(evolution_response.data.chain.evolves_to[0].evolves_to[0].species.name); | |
} | |
let evolution_chain = pokemon_evolutions.join(' -> '); | |
const order_in_evolution_chain = pokemon_evolutions.indexOf(pokemon); | |
const next_form = pokemon_evolutions[order_in_evolution_chain + 1]; | |
const previous_form = pokemon_evolutions[order_in_evolution_chain - 1]; | |
const evolution_text = { | |
'evolution_chain': `${pokemon}'s evolution chain is: ${evolution_chain}`, | |
'first_evolution': (pokemon == pokemon_evolutions[0]) ? `This is already the first form` : `${pokemon_evolutions[0]} is the first evolution`, | |
'last_evolution': (pokemon == pokemon_evolutions[pokemon_evolutions.length - 1]) ? `This is already the final form` : pokemon_evolutions[pokemon_evolutions.length - 1], | |
'next_form': `${pokemon} evolves to ${next_form}`, | |
'previous_form': `${pokemon} evolves from ${previous_form}` | |
}; | |
if (evolution_text[evolution_requirement]) { | |
fulfillmentText = evolution_text[evolution_requirement]; | |
} | |
Object.assign(response_obj, { | |
fulfillmentText | |
}); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment