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", "name": "utility",
"version": "3.2.1" "version": "3.2.2"
} }
], ],
"typescript": true "typescript": true

View File

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

View File

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