From d76835eaec420e56f5610d95af012af9f82338e1 Mon Sep 17 00:00:00 2001 From: Daniel Odendahl Jr Date: Fri, 5 May 2017 13:59:41 +0000 Subject: [PATCH] Currency Converter --- commands/random/currency.js | 57 ++++++++++++++++++++++++++++++ commands/random/currencycodes.json | 33 +++++++++++++++++ package.json | 2 +- 3 files changed, 91 insertions(+), 1 deletion(-) create mode 100644 commands/random/currency.js create mode 100644 commands/random/currencycodes.json diff --git a/commands/random/currency.js b/commands/random/currency.js new file mode 100644 index 00000000..b352e344 --- /dev/null +++ b/commands/random/currency.js @@ -0,0 +1,57 @@ +const { Command } = require('discord.js-commando'); +const request = require('superagent'); +const codes = require('./codes'); + +module.exports = class CurrencyCommand extends Command { + constructor(client) { + super(client, { + name: 'currency', + group: 'random', + memberName: 'currentUser', + description: 'Converts from one currency to another.', + details: `**Codes:** ${codes.join(', ')}`, + args: [ + { + key: 'base', + prompt: 'What currency code do you want to use as the base?', + type: 'string', + validate: base => { + if(codes[base.toUpperCase()]) + return true; + return `${base} is not a valid currency code. Use \`help currency\` to view a list of codes.`; + }, + parse: base => base.toUpperCase() + }, + { + key: 'to', + prompt: 'What currency code do you want to convert to?', + type: 'string', + validate: to => { + if(codes[to.toUpperCase()]) + return true; + return `${to} is not a valid currency code. Use \`help currency\` to view a list of codes.`; + }, + parse: code => code.toUpperCase() + }, + { + key: 'amount', + prompt: 'How much money should be converted? Do not use symbols.', + type: 'integer' + } + ] + }); + } + + async run(msg, args) { + const { base, to, amount } = args; + try { + const { body } = await request + .get(`http://api.fixer.io/latest?base=${base}`); + const rate = body[to]; + const converted = rate * amount; + return msg.say(`${amount} ${base} is ${converted} ${to}.`); + } catch(err) { + return msg.say('An Unknown Error Occurred.'); + } + } +}; diff --git a/commands/random/currencycodes.json b/commands/random/currencycodes.json new file mode 100644 index 00000000..4b3f37a5 --- /dev/null +++ b/commands/random/currencycodes.json @@ -0,0 +1,33 @@ +[ + "AUD", + "BGN", + "BRL", + "CAD", + "CHF", + "CNY", + "CZK", + "DKK", + "GBP", + "HKD", + "HRK", + "HUF", + "IDR", + "ILS", + "INR", + "JPY", + "KRW", + "MXN", + "MYR", + "NOK", + "NZD", + "PHP", + "PLN", + "RON", + "RUB", + "SEK", + "SGD", + "THB", + "TRY", + "ZAR", + "EUR" +] diff --git a/package.json b/package.json index bfa5566c..00cdbbdc 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "xiaobot", - "version": "18.1.1", + "version": "18.2.0", "description": "A Discord Bot", "main": "shardingmanager.js", "scripts": {