From a395b07632ff9602daec353b50523ce5242ac329 Mon Sep 17 00:00:00 2001 From: Daniel Odendahl Jr Date: Thu, 17 Aug 2017 22:19:40 +0000 Subject: [PATCH] Fortune Cookie and Name APIs --- commands/random-res/fortune.js | 14 +++++++++++--- commands/random-res/name.js | 25 +++++++++++-------------- 2 files changed, 22 insertions(+), 17 deletions(-) diff --git a/commands/random-res/fortune.js b/commands/random-res/fortune.js index b364d25c..75079034 100644 --- a/commands/random-res/fortune.js +++ b/commands/random-res/fortune.js @@ -1,5 +1,6 @@ const Command = require('../../structures/Command'); -const fortunes = require('../../assets/json/fortune'); +const snekfetch = require('snekfetch'); +const { stripIndents } = require('common-tags'); module.exports = class FortuneCommand extends Command { constructor(client) { @@ -12,7 +13,14 @@ module.exports = class FortuneCommand extends Command { }); } - run(msg) { - return msg.say(fortunes[Math.floor(Math.random() * fortunes.length)]); + async run(msg) { + const { body } = await snekfetch + .get('http://fortunecookieapi.herokuapp.com/v1/cookie') + .query({ limit: 1 }); + return msg.say(stripIndents` + ${body[0].fortune.message} + Lotto: ${body[0].lotto.numbers.join(', ')} + Lesson: ${body[0].lesson.chinese} (${body[0].lesson.pronunciation}): ${body[0].lesson.english} + `); } }; diff --git a/commands/random-res/name.js b/commands/random-res/name.js index 2d4a62b2..b8e23283 100644 --- a/commands/random-res/name.js +++ b/commands/random-res/name.js @@ -1,7 +1,7 @@ const Command = require('../../structures/Command'); -const { last, male, female } = require('../../assets/json/name'); +const snekfetch = require('snekfetch'); -module.exports = class RandomNameCommand extends Command { +module.exports = class NameCommand extends Command { constructor(client) { super(client, { name: 'name', @@ -24,18 +24,15 @@ module.exports = class RandomNameCommand extends Command { }); } - run(msg, args) { // eslint-disable-line consistent-return + async run(msg, args) { const { gender } = args; - const lastName = last[Math.floor(Math.random() * last.length)]; - if (gender === 'male') { - return msg.say(`${male[Math.floor(Math.random() * male.length)]} ${lastName}`); - } else if (gender === 'female') { - return msg.say(`${female[Math.floor(Math.random() * female.length)]} ${lastName}`); - } else if (gender === 'both') { - const genders = ['male', 'female']; - const rGender = genders[Math.floor(Math.random() * genders.length)]; - if (rGender === 'male') return msg.say(`${male[Math.floor(Math.random() * male.length)]} ${lastName}`); - else if (rGender === 'female') return msg.say(`${female[Math.floor(Math.random() * female.length)]} ${lastName}`); - } + const { body } = await snekfetch + .get('http://namey.muffinlabs.com/name.json') + .query({ + with_surname: true, + type: gender, + frequency: 'all' + }); + return msg.say(body[0]); } };