const rp = require("request-promise"); const api_url = ({ market, crypto, currency }) => `https://api.cryptowat.ch/markets/${market}/${crypto}${currency}/summary`; const currencies = { usd: ({ val }) => `\$${val}`, eur: ({ val }) => `${val}€`, eth: ({ val }) => `${val}Ξ`, btc: ({ val }) => `${val}฿`, xmr: ({ val }) => `${val} xmr` }; const markets = { btc: "coinbase", eth: "coinbase", xmr: "bitfinex" }; 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] || "eur" : "usd").then( resolve => e.reply(resolve), reject => e.reply(reject) ); } })); }; const cryptowat_summary = (crypto, market, currency) => { return new Promise((resolve, reject) => { if (!Object.keys(currencies).includes(currency) || crypto === currency) reject(`Can't convert or invalid currency: ${currency}`); rp([{ market: market, crypto: crypto, currency: currency }].map(api_url).join``, { json: true }) .then(res => { if (res.length === 0) reject("No data received"); const data = { 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"); }); }); };