mirror of
https://github.com/arthur-pbty/xiao.git
synced 2026-06-04 15:56:52 +02:00
56 lines
2.1 KiB
JavaScript
56 lines
2.1 KiB
JavaScript
const { Command } = require('discord.js-commando');
|
|
const request = require('superagent');
|
|
const codes = require('../../assets/json/currency');
|
|
|
|
module.exports = class CurrencyCommand extends Command {
|
|
constructor(client) {
|
|
super(client, {
|
|
name: 'currency',
|
|
group: 'numedit',
|
|
memberName: 'currency',
|
|
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.includes(base.toUpperCase())) return true;
|
|
return 'Invalid Currency Code. Use `help currency` to view a list of currency codes.';
|
|
},
|
|
parse: base => base.toUpperCase()
|
|
},
|
|
{
|
|
key: 'to',
|
|
prompt: 'What currency code do you want to convert to?',
|
|
type: 'string',
|
|
validate: to => {
|
|
if (codes.includes(to.toUpperCase())) return true;
|
|
return 'Invalid Currency Code. Use `help currency` to view a list of currency codes.';
|
|
},
|
|
parse: to => to.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;
|
|
if (base === to) return msg.say(`Converting ${base} to ${to} is the same value, dummy.`);
|
|
try {
|
|
const { body } = await request
|
|
.get(`http://api.fixer.io/latest?base=${base}&symbols=${to}`);
|
|
const rate = body.rates[to];
|
|
return msg.say(`${amount} ${base} is ${amount * rate} ${to}.`);
|
|
} catch (err) {
|
|
return msg.say(err);
|
|
}
|
|
}
|
|
};
|