Created
July 12, 2019 00:15
-
-
Save anchetaWern/579847150203df68cbd9e2ef87eb4b8d to your computer and use it in GitHub Desktop.
Pokedex bot: Type effectiveness 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 (get_type_effectiveness) { | |
const pokemon_type = parameters.pokemon_types; | |
let type_effectiveness = parameters.type_effectiveness; | |
const type_effectiveness_formatted = type_effectiveness.replace(/_/g, ' '); | |
const type_effectiveness_word = outputContexts[0].parameters['type_effectiveness.original']; | |
let from_or_to = type_effectiveness.split('_')[2]; | |
const pokemon_type_comes_first = (queryText.indexOf(pokemon_type) < queryText.indexOf(type_effectiveness_word)) ? true : false; | |
const exempt_words = ['resistant', 'no damage', 'zero damage', 'no effect']; | |
const has_exempt_words = exempt_words.some(v => type_effectiveness_word.includes(v)); | |
if ( | |
(pokemon_type_comes_first && !has_exempt_words) || | |
(!pokemon_type_comes_first && has_exempt_words) | |
) { | |
let new_from_or_to = (from_or_to == 'from') ? 'to' : 'from'; | |
type_effectiveness = type_effectiveness.replace(from_or_to, new_from_or_to); | |
from_or_to = new_from_or_to; | |
} | |
const response = await axios_instance.get(`/type/${pokemon_type}`); | |
const damage_relations = (response.data.damage_relations[type_effectiveness].length > 0) ? response.data.damage_relations[type_effectiveness].map(item => item.name).join(', ') : 'none'; | |
const nature_of_damage = (from_or_to == 'from') ? 'receives' : 'inflicts'; | |
fulfillmentText = (nature_of_damage == 'inflicts') ? | |
`${pokemon_type} type inflicts ${type_effectiveness_formatted} ${damage_relations} type` : | |
`${pokemon_type} ${nature_of_damage} ${type_effectiveness_formatted} the following: ${damage_relations}`; | |
Object.assign(response_obj, { | |
fulfillmentText | |
}); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment