const https = require("https"); 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", { call: /^(\.|\/)(btc|eth|xmr)/i, level: 0, active: true, clients: ["irc", "tg"], f: e => { let args = e.message.trim().substr(1).toLowerCase().split(" "); cryptowat_summary(args[0], markets[args[0]], args[0] !== "xmr" ? args[1] : "usd").then( resolve => e.reply(resolve), reject => e.reply(reject) ); } }); }; 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) 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 }; 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]`); }); }); }); };