Skip to content

Instantly share code, notes, and snippets.

@brh55
Created September 19, 2019 07:01
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save brh55/9de1bd4594d1a1a9779afe33ee5aead2 to your computer and use it in GitHub Desktop.
Save brh55/9de1bd4594d1a1a9779afe33ee5aead2 to your computer and use it in GitHub Desktop.
Pokedex Skills Code
const Pokedex = require('../services/pokedex');
const convert = require('convert-units');
// Add this helper method
const parseParameter = (command, text) => {
const start = text.indexOf(command);
const textStart = text.substring(start)
const parameters = textStart.split(" ");
if (parameters[1]) {
return parameters[1];
}
return null;
}
// Return <feet>' <inches>"
const formatHeight = (height) => {
// height is in decimeter
const meters = height / 10;
const feet = convert(meters).from('m').to('ft-us');
const parts = feet.toString().split('.');
const characteristic = parts[0];
const mantissa = `.${parts[1]}`;
const inches = convert(mantissa).from('ft').to('in').toFixed();
return `${characteristic}' ${inches}"`
}
// Return <lb> lbs
const formatWeight = (weight) => {
// weight is in hectograms
const grams = weight * 100;
// Pokedex usually shows up to the tenth
const lb = convert(grams).from('g').to('lb').toFixed(1);
return `${lb} lbs`;
}
// Return "name" -> "Name"
const formatName = (name) => name.charAt(0).toUpperCase() + name.slice(1);
module.exports = controller => {
const POKEDEX_CMD = '!pokedex';
controller.hears(
POKEDEX_CMD, // this is our command
// We define the context of when our bot should hear this command
// ambient = all of chat the bot is in
// direct_message = any messages directly sent to the bot
['ambient', 'direct_message'],
(bot, message) => {
const parameter = parseParameter(POKEDEX_CMD, message.text);
if (parameter) {
Pokedex.search(parameter)
.then(result => {
const embed = new controller.RichEmbed();
embed.setAuthor(
"Pokedex",
"https://icon-library.net/images/pokedex-icon/pokedex-icon-15.jpg" // Grabbing this icon from icon-library
);
embed.setTitle(formatName(result.name));
embed.setDescription(`**No. ${response.id}** \n **${response.types[1].type.name}**`);
embed.setThumbnail(result.sprites.front_default);
embed.addField("Weight", formatWeight(result.weight));
embed.addField("Height", formatHeight(result.height));
embed.setColor("GREEN");
embed.addField("Description", result.description);
bot.reply(message, embed);
})
.catch((e) => {
bot.reply(message, 'NO DATA')
})
} else {
bot.reply(message, 'Please try again with a name or number');
}
}
)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment