mirror of
https://github.com/arthur-pbty/xiao.git
synced 2026-06-11 03:14:35 +02:00
Fix currency command
This commit is contained in:
@@ -12,9 +12,10 @@ module.exports = class CurrencyCommand extends Command {
|
|||||||
description: 'Converts currency from one currency to another.',
|
description: 'Converts currency from one currency to another.',
|
||||||
credit: [
|
credit: [
|
||||||
{
|
{
|
||||||
name: 'Foreign exchange rates API',
|
name: 'Fawaz Ahmed',
|
||||||
url: 'https://exchangeratesapi.io/',
|
url: 'https://github.com/fawazahmed0',
|
||||||
reason: 'API'
|
reason: 'API',
|
||||||
|
reasonURL: 'https://github.com/fawazahmed0/currency-api'
|
||||||
}
|
}
|
||||||
],
|
],
|
||||||
args: [
|
args: [
|
||||||
@@ -28,45 +29,47 @@ module.exports = class CurrencyCommand extends Command {
|
|||||||
prompt: 'What currency code (e.g. USD) do you want to use as the base?',
|
prompt: 'What currency code (e.g. USD) do you want to use as the base?',
|
||||||
type: 'string',
|
type: 'string',
|
||||||
min: 3,
|
min: 3,
|
||||||
max: 3,
|
max: 3
|
||||||
parse: base => base.toUpperCase()
|
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
key: 'target',
|
key: 'target',
|
||||||
prompt: 'What currency code (e.g. USD) do you want to convert to?',
|
prompt: 'What currency code (e.g. USD) do you want to convert to?',
|
||||||
type: 'string',
|
type: 'string',
|
||||||
min: 3,
|
min: 3,
|
||||||
max: 3,
|
max: 3
|
||||||
parse: target => target.toUpperCase()
|
|
||||||
}
|
}
|
||||||
]
|
]
|
||||||
});
|
});
|
||||||
|
|
||||||
this.rates = new Map();
|
this.currencies = null;
|
||||||
}
|
}
|
||||||
|
|
||||||
async run(msg, { base, target, amount }) {
|
async run(msg, { base, target, amount }) {
|
||||||
|
if (!this.currencies) await this.fetchCurrencies();
|
||||||
|
if (!this.currencies[base]) return msg.say('You provided an invalid base currency code.');
|
||||||
|
if (!this.currencies[target]) return msg.say('You provided an invalid target currency code.');
|
||||||
if (base === target) return msg.say(`Converting ${base} to ${target} is the same value, dummy.`);
|
if (base === target) return msg.say(`Converting ${base} to ${target} is the same value, dummy.`);
|
||||||
try {
|
try {
|
||||||
const rate = await this.fetchRate(base, target);
|
const rate = await this.fetchRate(base, target);
|
||||||
return msg.say(`${formatNumber(amount)} ${base} is ${formatNumber(amount * rate)} ${target}.`);
|
const baseName = this.currencies[base];
|
||||||
|
const targetName = this.currencies[target]
|
||||||
|
return msg.say(`${formatNumber(amount)} ${baseName} is ${formatNumber(amount * rate)} ${targetName}.`);
|
||||||
} catch (err) {
|
} catch (err) {
|
||||||
if (err.status === 400) return msg.say('Invalid base/target.');
|
|
||||||
return msg.reply(`Oh no, an error occurred: \`${err.message}\`. Try again later!`);
|
return msg.reply(`Oh no, an error occurred: \`${err.message}\`. Try again later!`);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
async fetchRate(base, target) {
|
async fetchCurrencies() {
|
||||||
const query = `${base}-${target}`;
|
|
||||||
if (this.rates.has(query)) return this.rates.get(query);
|
|
||||||
const { body } = await request
|
const { body } = await request
|
||||||
.get('https://api.exchangeratesapi.io/latest')
|
.get('https://cdn.jsdelivr.net/gh/fawazahmed0/currency-api@1/latest/currencies.json');
|
||||||
.query({
|
this.currencies = body;
|
||||||
base,
|
setTimeout(() => { this.currencies = null; }, 1.8e+6);
|
||||||
symbols: target
|
return this.currencies;
|
||||||
});
|
}
|
||||||
this.rates.set(query, body.rates[target]);
|
|
||||||
setTimeout(() => this.rates.delete(query), 1.8e+6);
|
async fetchRate(base, target) {
|
||||||
return body.rates[target];
|
const { body } = await request
|
||||||
|
.get(`https://cdn.jsdelivr.net/gh/fawazahmed0/currency-api@1/latest/currencies/${base}/${target}.json`);
|
||||||
|
return body[target];
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|||||||
Reference in New Issue
Block a user