diff --git a/squeebot.repo.json b/squeebot.repo.json index 5976f7e..9192e1b 100644 --- a/squeebot.repo.json +++ b/squeebot.repo.json @@ -47,7 +47,7 @@ }, { "name": "utility", - "version": "3.1.0" + "version": "3.1.1" } ], "typescript": true diff --git a/utility/plugin.json b/utility/plugin.json index 6454369..d0cdaea 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.1.0", + "version": "3.1.1", "tags": ["commands", "tools"], "dependencies": ["simplecommands"], "npmDependencies": [ diff --git a/utility/plugin.ts b/utility/plugin.ts index 259c75a..59b368c 100644 --- a/utility/plugin.ts +++ b/utility/plugin.ts @@ -656,83 +656,86 @@ function addCommands(plugin: UtilityPlugin, commands: any): void { aliases: ['cv', 'unit'] }); - cmds.push({ - name: 'currency', - execute: async (msg: IMessage, msr: MessageResolver, spec: any, prefix: string, ...simplified: any[]): Promise => { - let ctw: CEXResponse | null = cexCache.cache as CEXResponse; - if (cexCache.expiry < Date.now()) { - let fetched; - try { - const data = await httpGET('https://api.exchangeratesapi.io/latest'); - fetched = JSON.parse(data); - logger.log('[utility] Fetched currency exchange rates successfully.'); - } catch (e) { - ctw = null; + const cexkey = plugin.config.get('currencyAPIKey'); + if (cexkey) { + cmds.push({ + name: 'currency', + execute: async (msg: IMessage, msr: MessageResolver, spec: any, prefix: string, ...simplified: any[]): Promise => { + let ctw: CEXResponse | null = cexCache.cache as CEXResponse; + if (cexCache.expiry < Date.now()) { + let fetched; + try { + const data = await httpGET('http://api.exchangeratesapi.io/latest?access_key=' + cexkey); + fetched = JSON.parse(data); + logger.log('[utility] Fetched currency exchange rates successfully.'); + } catch (e) { + ctw = null; + } + + if (!ctw || !fetched.rates) { + msg.resolve('Could not fetch currency exchange rates at this time. Please try again later.'); + return true; + } + + cexCache.cache = fetched.rates; + cexCache.date = fetched.date; + cexCache.expiry = Date.now() + 86400000; // day + ctw = cexCache.cache as CEXResponse; } - if (!ctw || !fetched.rates) { - msg.resolve('Could not fetch currency exchange rates at this time. Please try again later.'); + if (simplified[0] === 'date') { + msg.resolve('Currency exchange rates are as of %s', cexCache.date); + return true; + } else if (simplified[0] === 'list') { + msg.resolve('Currently supported currencies: EUR, %s', Object.keys(cexCache.cache).join(', ')); return true; } - cexCache.cache = fetched.rates; - cexCache.date = fetched.date; - cexCache.expiry = Date.now() + 86400000; // day - ctw = cexCache.cache as CEXResponse; - } + const n = parseFloat(simplified[0]); + let f = simplified[1]; + let t = simplified[2]; + if (isNaN(n) || !f || !t) { + msg.resolve('Invalid parameters.'); + return true; + } + f = f.toUpperCase(); + t = t.toUpperCase(); - if (simplified[0] === 'date') { - msg.resolve('Currency exchange rates are as of %s', cexCache.date); - return true; - } else if (simplified[0] === 'list') { - msg.resolve('Currently supported currencies: EUR, %s', Object.keys(cexCache.cache).join(', ')); - return true; - } + if (f !== 'EUR' && !ctw[f]) { + msg.resolve('This currency is currently not supported.'); + return true; + } - const n = parseFloat(simplified[0]); - let f = simplified[1]; - let t = simplified[2]; - if (isNaN(n) || !f || !t) { - msg.resolve('Invalid parameters.'); - return true; - } - f = f.toUpperCase(); - t = t.toUpperCase(); + if (t !== 'EUR' && !ctw[t]) { + msg.resolve('This currency is currently not supported.'); + return true; + } - if (f !== 'EUR' && !ctw[f]) { - msg.resolve('This currency is currently not supported.'); - return true; - } + if (f === t) { + msg.resolve('%f %s', n, f); + return true; + } - if (t !== 'EUR' && !ctw[t]) { - msg.resolve('This currency is currently not supported.'); - 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; + } - if (f === t) { - msg.resolve('%f %s', n, f); + const amnt = (ctw[t] * n / ctw[f]).toFixed(4); + msg.resolve('%f %s => %f %s', n, f, amnt, t); 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 amnt = (ctw[t] * n / ctw[f]).toFixed(4); - msg.resolve('%f %s => %f %s', n, f, amnt, t); - return true; - }, - description: 'Convert between currencies.', - usage: ' | [date | list] [] []', - aliases: ['cex', 'exchange'] - }); + }, + description: 'Convert between currencies.', + usage: ' | [date | list] [] []', + aliases: ['cex', 'exchange'] + }); + } cmds.push({ name: 'randomnumber', @@ -784,6 +787,7 @@ function addCommands(plugin: UtilityPlugin, commands: any): void { @Configurable({ ipfsGateway: 'https://ipfs.io', + currencyAPIKey: '', randomMax: 64 }) class UtilityPlugin extends Plugin {