exchangeratesapi can go suck it

This commit is contained in:
Evert Prants 2021-04-15 19:56:45 +03:00
parent dd39ffc54f
commit 909b747bf8
Signed by: evert
GPG Key ID: 1688DA83D222D0B5
3 changed files with 72 additions and 76 deletions

View File

@ -47,7 +47,7 @@
}, },
{ {
"name": "utility", "name": "utility",
"version": "3.1.1" "version": "3.1.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.1.1", "version": "3.1.2",
"tags": ["commands", "tools"], "tags": ["commands", "tools"],
"dependencies": ["simplecommands"], "dependencies": ["simplecommands"],
"npmDependencies": [ "npmDependencies": [

View File

@ -656,86 +656,83 @@ function addCommands(plugin: UtilityPlugin, commands: any): void {
aliases: ['cv', 'unit'] aliases: ['cv', 'unit']
}); });
const cexkey = plugin.config.get('currencyAPIKey'); cmds.push({
if (cexkey) { name: 'currency',
cmds.push({ execute: async (msg: IMessage, msr: MessageResolver, spec: any, prefix: string, ...simplified: any[]): Promise<boolean> => {
name: 'currency', let ctw: CEXResponse | null = cexCache.cache as CEXResponse;
execute: async (msg: IMessage, msr: MessageResolver, spec: any, prefix: string, ...simplified: any[]): Promise<boolean> => { if (cexCache.expiry < Date.now()) {
let ctw: CEXResponse | null = cexCache.cache as CEXResponse; let fetched;
if (cexCache.expiry < Date.now()) { try {
let fetched; const data = await httpGET('https://api.exchangerate.host/latest');
try { fetched = JSON.parse(data);
const data = await httpGET('http://api.exchangeratesapi.io/latest?access_key=' + cexkey); logger.log('[utility] Fetched currency exchange rates successfully.');
fetched = JSON.parse(data); } catch (e) {
logger.log('[utility] Fetched currency exchange rates successfully.'); ctw = null;
} 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 (simplified[0] === 'date') { if (!ctw || !fetched.rates) {
msg.resolve('Currency exchange rates are as of %s', cexCache.date); msg.resolve('Could not fetch currency exchange rates at this time. Please try again later.');
return true;
} else if (simplified[0] === 'list') {
msg.resolve('Currently supported currencies: EUR, %s', Object.keys(cexCache.cache).join(', '));
return true; return true;
} }
const n = parseFloat(simplified[0]); cexCache.cache = fetched.rates;
let f = simplified[1]; cexCache.date = fetched.date;
let t = simplified[2]; cexCache.expiry = Date.now() + 86400000; // day
if (isNaN(n) || !f || !t) { ctw = cexCache.cache as CEXResponse;
msg.resolve('Invalid parameters.'); }
return true;
}
f = f.toUpperCase();
t = t.toUpperCase();
if (f !== 'EUR' && !ctw[f]) { if (simplified[0] === 'date') {
msg.resolve('This currency is currently not supported.'); msg.resolve('Currency exchange rates are as of %s', cexCache.date);
return true;
}
if (t !== 'EUR' && !ctw[t]) {
msg.resolve('This currency is currently not supported.');
return true;
}
if (f === t) {
msg.resolve('%f %s', n, f);
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; return true;
}, } else if (simplified[0] === 'list') {
description: 'Convert between currencies.', msg.resolve('Currently supported currencies: EUR, %s', Object.keys(cexCache.cache).join(', '));
usage: '<number> | [date | list] [<from currency>] [<to currency>]', return true;
aliases: ['cex', 'exchange'] }
});
} 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 (f !== 'EUR' && !ctw[f]) {
msg.resolve('This currency is currently not supported.');
return true;
}
if (t !== 'EUR' && !ctw[t]) {
msg.resolve('This currency is currently not supported.');
return true;
}
if (f === t) {
msg.resolve('%f %s', n, f);
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: '<number> | [date | list] [<from currency>] [<to currency>]',
aliases: ['cex', 'exchange']
});
cmds.push({ cmds.push({
name: 'randomnumber', name: 'randomnumber',
@ -787,7 +784,6 @@ function addCommands(plugin: UtilityPlugin, commands: any): void {
@Configurable({ @Configurable({
ipfsGateway: 'https://ipfs.io', ipfsGateway: 'https://ipfs.io',
currencyAPIKey: '',
randomMax: 64 randomMax: 64
}) })
class UtilityPlugin extends Plugin { class UtilityPlugin extends Plugin {