From 9e0feffc3c9461f56bb3663a01bf33d342bc6967 Mon Sep 17 00:00:00 2001 From: Evert Prants Date: Wed, 18 May 2022 13:52:27 +0300 Subject: [PATCH] hopefully fix currency exchange --- squeebot.repo.json | 2 +- utility/plugin.json | 2 +- utility/plugin.ts | 61 +++++++++++++++++++++------------------------ 3 files changed, 31 insertions(+), 34 deletions(-) diff --git a/squeebot.repo.json b/squeebot.repo.json index 7305833..4cd01d0 100644 --- a/squeebot.repo.json +++ b/squeebot.repo.json @@ -43,7 +43,7 @@ }, { "name": "utility", - "version": "3.2.1" + "version": "3.2.2" } ], "typescript": true diff --git a/utility/plugin.json b/utility/plugin.json index f574b73..406aaf6 100644 --- a/utility/plugin.json +++ b/utility/plugin.json @@ -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" diff --git a/utility/plugin.ts b/utility/plugin.ts index 136de5c..af0b5fd 100644 --- a/utility/plugin.ts +++ b/utility/plugin.ts @@ -611,28 +611,32 @@ function addCommands(plugin: UtilityPlugin, commands: any): void { prefix: string, ...simplified: any[] ): Promise => { - 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.',