Uwev2/src/inc/trigger/coins.mjs
2017-12-24 00:11:51 +01:00

58 lines
2.0 KiB
JavaScript

import rp from "request-promise";
const api_url = ({ market, crypto, currency }) => `https://api.cryptowat.ch/markets/${market}/${crypto}${currency}/summary`;
const currencies = {
usd: ({ val }) => `\$${val}`,
usdt: ({ val }) => `\$${val}`,
eur: ({ val }) => `${val}`,
eth: ({ val }) => `${val}Ξ`,
btc: ({ val }) => `${val}฿`,
xmr: ({ val }) => `${val} xmr`,
xrp: ({ val }) => `${val} xrp`
};
const markets = {
btc: "coinbase",
eth: "coinbase",
xmr: "bitfinex",
xrp: "poloniex"
};
export default bot => {
bot._trigger.set("coins", new bot.trigger({
call: /^(\.|\/)(btc|eth|xmr|xrp)/i,
f: e => {
let currency = e.args[0] || "eur";
if(e.cmd === "xrp")
currency = "usdt";
cryptowat_summary(e.cmd, markets[e.cmd], currency).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 => {
console.log(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");
});
});
};