promisified

This commit is contained in:
Flummi 2017-11-29 21:00:00 +01:00
parent daf6090b9f
commit 2471a7c4b2

View File

@ -1,4 +1,4 @@
const https = require("https");
const rp = require("request-promise");
const api_url = ({ market, crypto, currency }) => `https://api.cryptowat.ch/markets/${market}/${crypto}${currency}/summary`;
const currencies = {
@ -17,8 +17,9 @@ const markets = {
module.exports = bot => {
bot._trigger.set("coins", new bot.trigger({
call: /^(\.|\/)(btc|eth|xmr)/i,
clients: ["irc"],
f: e => {
cryptowat_summary(e.cmd, markets[e.cmd], e.cmd !== "xmr" ? e.args[0] || "usd" : "usd").then(
cryptowat_summary(e.cmd, markets[e.cmd], e.cmd !== "xmr" ? e.args[0] || "eur" : "usd").then(
resolve => e.reply(resolve),
reject => e.reply(reject)
);
@ -28,26 +29,23 @@ module.exports = bot => {
const cryptowat_summary = (crypto, market, currency) => {
return new Promise((resolve, reject) => {
if (!currency)
currency = "eur";
if (!Object.keys(currencies).includes(currency) || crypto === currency)
reject(`Can't convert or invalid currency: ${currency}`);
https.get([{ market: market, crypto: crypto, currency: currency }].map(api_url).join``, res => {
let content = "";
res.on('data', chunk => content += chunk.toString());
res.on('end', () => {
if(content.length === 0)
rp([{ market: market, crypto: crypto, currency: currency }].map(api_url).join``, { json: true })
.then(res => {
if (res.length === 0)
reject("No data received");
const result = JSON.parse(content).result;
const data = {
last: [{ val: result.price.last }].map(currencies[currency]).join``,
high: [{ val: result.price.high }].map(currencies[currency]).join``,
low: [{ val: result.price.low }].map(currencies[currency]).join``,
change: (result.price.change.percentage * 100).toFixed(2),
volume: result.volume
last: [{ val: res.result.price.last }].map(currencies[currency]).join``,
high: [{ val: res.result.price.high }].map(currencies[currency]).join``,
low: [{ val: res.result.price.low }].map(currencies[currency]).join``,
change: (res.result.price.change.percentage * 100).toFixed(2),
volume: res.result.volume
};
resolve(`Current: [b]${data.last}[/b] - High: [b]${data.high}[/b] - Low: [b]${data.low}[/b] - Change: [b]${data.change}[/b]% - Volume: [b]${data.volume}[/b]`);
})
.catch(err => {
reject("lol cryptowatch ist down");
});
});
});
};