From 2471a7c4b28a19b78b0c7e593059e563615a08d8 Mon Sep 17 00:00:00 2001 From: Flummi Date: Wed, 29 Nov 2017 21:00:00 +0100 Subject: [PATCH] promisified --- src/inc/trigger/coins.js | 30 ++++++++++++++---------------- 1 file changed, 14 insertions(+), 16 deletions(-) diff --git a/src/inc/trigger/coins.js b/src/inc/trigger/coins.js index a1038f1..cdd6fc9 100644 --- a/src/inc/trigger/coins.js +++ b/src/inc/trigger/coins.js @@ -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"); }); - }); }); }; \ No newline at end of file