import * as Sphinx from 'sphinx-bot' import { finalAction } from '../controllers/actions' import * as fetch from 'node-fetch' const msg_types = Sphinx.MSG_TYPE let initted = false /* curl -H "X-CMC_PRO_API_KEY: 2fddf900-9114-471c-849d-cad3e3923e5b" -H "Accept: application/json" -d "symbol=BTC&convert=USD" -G https://pro-api.coinmarketcap.com/v1/cryptocurrency/quotes/latest */ const token = '2fddf900-9114-471c-849d-cad3e3923e5b' const url = 'https://pro-api.coinmarketcap.com/v1/cryptocurrency/quotes/latest' export function init() { if(initted) return initted = true const client = new Sphinx.Client() client.login('_', finalAction) client.on(msg_types.MESSAGE, async (message: Sphinx.Message) => { const arr = message.content.split(' ') if (arr.length < 2) return if (arr[0]!=='/btc') return const cmd = arr[1] switch (cmd) { case 'price': console.log("price", arr[2]) try { const r = await fetch(url+'?symbol=BTC&convert=USD',{ headers:{'X-CMC_PRO_API_KEY': token, 'Accept': 'application/json'} }) if (!r.ok) return const j = await r.json() const price = j.data.BTC.quote.USD.price.toFixed(2) const percentChange24 = j.data.BTC.quote.USD.percent_change_24h.toFixed(2) const embed = new Sphinx.MessageEmbed() .setAuthor('BitcoinBot') .setTitle('Bitcoin Price:') .addFields([ { name: 'Price:', value: price }, { name: '24 Hour Change', value: percentChange24+'%' } ]) .setThumbnail(botSVG) message.channel.send({ embed }) } catch(e){ console.log('BTC bot error',e) } return default: const embed = new Sphinx.MessageEmbed() .setAuthor('BitcoinBot') .setTitle('BitcoinBot Commands:') .addFields([ { name: 'Print BTC price', value: '/btc price' }, { name: 'Help', value: '/btc help' } ]) .setThumbnail(botSVG) message.channel.send({ embed }) return } }) } const botSVG = ` `