hopefully fix currency exchange

This commit is contained in:
Evert Prants 2022-05-18 13:52:27 +03:00
parent 60b55f582b
commit 9e0feffc3c
Signed by: evert
GPG Key ID: 1688DA83D222D0B5
3 changed files with 31 additions and 34 deletions

View File

@ -43,7 +43,7 @@
},
{
"name": "utility",
"version": "3.2.1"
"version": "3.2.2"
}
],
"typescript": true

View File

@ -2,7 +2,7 @@
"main": "plugin.js",
"name": "utility",
"description": "Utility commands and math operations",
"version": "3.2.1",
"version": "3.2.2",
"tags": [
"commands",
"tools"

View File

@ -611,28 +611,32 @@ function addCommands(plugin: UtilityPlugin, commands: any): void {
prefix: string,
...simplified: any[]
): Promise<boolean> => {
let ctw: CEXResponse | null = plugin.cexCache.cache as CEXResponse;
let cexData: CEXResponse | null = plugin.cexCache.cache as CEXResponse;
const currentStamp = new Date().toISOString().replace(/T(.*)$/, '');
if (plugin.cexCache.expiry < Date.now()) {
let fetched;
try {
const data = await httpGET('https://api.exchangerate.host/latest');
const data = await httpGET(`https://api.exchangerate.host/latest?v=${currentStamp}`);
fetched = JSON.parse(data);
logger.log('[utility] Fetched currency exchange rates successfully.');
} catch (e) {
ctw = null;
fetched = null;
}
if (!ctw || !fetched.rates) {
if (!fetched?.rates) {
msg.resolve(
'Could not fetch currency exchange rates at this time. Please try again later.'
);
return true;
}
plugin.cexCache.cache = fetched.rates;
plugin.cexCache.date = fetched.date;
plugin.cexCache.expiry = Date.now() + 86400000; // day
ctw = plugin.cexCache.cache as CEXResponse;
Object.assign(plugin.cexCache, {
cache: fetched.rates,
date: fetched.date,
expiry: Date.now() + 86400000 / 2 // half-day
});
cexData = fetched.rates as CEXResponse;
}
if (simplified[0] === 'date') {
@ -643,50 +647,43 @@ function addCommands(plugin: UtilityPlugin, commands: any): void {
return true;
} else if (simplified[0] === 'list') {
msg.resolve(
'Currently supported currencies: EUR, %s',
Object.keys(plugin.cexCache.cache).join(', ')
'Currently supported currencies: %s',
Object.keys(cexData).join(', ')
);
return true;
}
const n = parseFloat(simplified[0]);
let f = simplified[1];
let t = simplified[2];
if (isNaN(n) || !f || !t) {
const inputValue = parseFloat(simplified[0]);
let fromCurrency = simplified[1];
let toCurrency = simplified[2];
if (isNaN(inputValue) || !fromCurrency || !toCurrency) {
msg.resolve('Invalid parameters.');
return true;
}
f = f.toUpperCase();
t = t.toUpperCase();
if (f !== 'EUR' && !ctw[f]) {
fromCurrency = fromCurrency.toUpperCase();
toCurrency = toCurrency.toUpperCase();
if (fromCurrency !== 'EUR' && !cexData[fromCurrency]) {
msg.resolve('This currency is currently not supported.');
return true;
}
if (t !== 'EUR' && !ctw[t]) {
if (toCurrency !== 'EUR' && !cexData[toCurrency]) {
msg.resolve('This currency is currently not supported.');
return true;
}
if (f === t) {
msg.resolve('%f %s', n, f);
if (fromCurrency === toCurrency) {
msg.resolve('%f %s', inputValue, fromCurrency);
return true;
}
let ramnt: string;
if (f === 'EUR') {
ramnt = (n * ctw[t]).toFixed(4);
msg.resolve('%f EUR => %f %s', n, ramnt, t);
return true;
} else if (t === 'EUR') {
ramnt = (n / ctw[f]).toFixed(4);
msg.resolve('%f %s => %f EUR', n, f, ramnt);
return true;
}
const resultValue = ((cexData[toCurrency] * inputValue) / cexData[fromCurrency]).toFixed(4);
const conversionRate = (cexData[toCurrency] / cexData[fromCurrency]).toFixed(4);
const conversionRateString = inputValue !== 1 ? `(1 = ${conversionRate})` : '';
const amnt = ((ctw[t] * n) / ctw[f]).toFixed(4);
msg.resolve('%f %s => %f %s', n, f, amnt, t);
msg.resolve(`${inputValue} ${fromCurrency} => ${resultValue} ${toCurrency} ${conversionRateString}`);
return true;
},
description: 'Convert between currencies.',