From 06c82665ef694dec759cc398428d8b954860fa2f Mon Sep 17 00:00:00 2001 From: Andrew Tookay Date: Fri, 2 Feb 2018 04:06:14 +0200 Subject: [PATCH] Updated to the lastest version from Discord This version had no crashes since it went online weeks ago. --- Creedy.py | 61 +++++++++++++++++++++++++++++++------------------------ 1 file changed, 34 insertions(+), 27 deletions(-) diff --git a/Creedy.py b/Creedy.py index c1ca534..09a506e 100644 --- a/Creedy.py +++ b/Creedy.py @@ -2,6 +2,7 @@ import requests import asyncio import json +import re with open('botParams.json') as json_input_file: json_bot_input = json.load(json_input_file) @@ -31,25 +32,37 @@ async def on_message(message): coin_ticker = coin_ticker.upper() goCMC = 0 - goCoinsM = 0 cmc_api = 'https://api.coinmarketcap.com/v1/ticker/' json_cmc = requests.get(cmc_api).json() - for ticker in json_cmc: + for coin in json_cmc: if ticker['symbol'] == coin_ticker: goCMC = 1 - coinCMC = ticker + coinCMC = coin + + if coin_ticker.upper() == 'CRDS': + try: + crds_usd = requests.get('https://crds.co/calc/crds_usd.txt') + crds_usd = float(crds_usd.text) - if goCMC == 0: - coinsm_api = 'https://coinsmarkets.com/apicoin.php' - trade_ticker = 'BTC_' + coin_ticker - json_coinsm = requests.get(coinsm_api).json() - for ticker in json_coinsm: - if ticker == trade_ticker: - goCoinsM = 1 + crds_btc = requests.get('https://crds.co/calc/crds_btc.txt') + crds_btc = float(crds_btc.text) - if goCMC == 0 and goCoinsM == 0: - final_message = 'Invalid ticker.' + supply = requests.get('https://crds.co/calc/crds_supply.txt') + supply = supply.text + + volume = requests.get('https://crds.co/calc/crds_vol.txt') + volume = float(volume.text) + + supply = re.sub(',', '', supply) + supply = float(supply) + + marketcap = round(crds_usd * supply, 2) + + final_message = 'Coin Name: Credits' + '\nTicker: ' + coin_ticker + '\n' + 'Last price: ' + str('{0:.8f}'.format(round(crds_btc, 8))) + ' BTC\n' + 'Volume(24h): ' + str(volume) + ' BTC\n' + 'Marketcap: ' + str(marketcap) + ' USD' + + except: + final_message = 'Invalid command due to factors outside our control.' elif goCMC == 1: json_coin_name = coinCMC['name'] @@ -59,23 +72,17 @@ async def on_message(message): json_percent_change = coinCMC['percent_change_24h'] final_message = 'Coin Name: ' + str(json_coin_name) + '\nTicker: ' + coin_ticker + '\n' + 'Price (BTC): ' + str(json_price_btc) + '\n' + 'Price (USD): ' + str(json_price_usd) + '\n' + 'Volume(24h): ' + str(json_24h_volume) + ' USD\n' + 'Change (24h): ' + str(json_percent_change) + '%' - - elif goCoinsM == 1: - json_last_price = json_coinsm[trade_ticker]['last'] - json_24h_volume = json_coinsm[trade_ticker]['24htrade'] - - final_message = 'Ticker: ' + coin_ticker + '\n' + 'Last price: ' + str(json_last_price) + ' BTC\n' + 'Volume(24h): ' + str(json_24h_volume) + ' BTC' - + else: final_message = 'Something went wrong. Contact the team.' elif message.content.startswith('!mnroi'): no_mn = message.content[6:] - ok = 0 try: no_mn = int(no_mn.strip()) final_message = getMnRoi(no_mn) + except ValueError: final_message = 'Invalid number of Masternodes.' @@ -88,7 +95,7 @@ async def on_message(message): link = 'http://explorer.crds.co/' elif len(message.content) > 1 and message.content[1] != ' ' and ((message.content[0] == '!' and message.content[1] != '!' and message.content[1] != '?') or (message.content[0] == '$' and message.content[1] != '$')): - final_message = 'Invalid command. Use ?help to see the available commands.' + final_message = 'Invalid command. Use !help to see the available commands.' if final_message != '': await client.send_message(message.channel, '`' + final_message + '`' + link) @@ -100,17 +107,17 @@ def getMnRoi(no_mn): json_cmc = requests.get('https://api.coinmarketcap.com/v1/ticker/').json() btc_usd = float(json_cmc[0]['price_usd']) - json_coinsm = requests.get('https://coinsmarkets.com/apicoin.php').json() - crds_btc = float(json_coinsm['BTC_CRDS']['last']) + crds_btc = requests.get('https://crds.co/calc/crds_btc.txt') + crds_btc = float(crds_btc.text) mncount = requests.get('http://explorer.crds.co/mncount.txt') mncount = float(mncount.text) blockcount = requests.get('http://explorer.crds.co/blockcount.txt') blockcount = int(blockcount.text) + except: - final_message = 'Command unavailable due to downtime of explorer or APIs.' - return final_message + return 'Command unavailable due to downtime of explorer or APIs.' crds_usd = crds_btc * btc_usd @@ -139,14 +146,14 @@ def getMnRoi(no_mn): final_message = 'No. of Masternodes: ' + str(no_mn) final_message += '\nCollateral (CRDS): ' + str(5000 * no_mn) - final_message += '\nCRDS Value (BTC): ' + str('{0:.8f}'.format(round(crds_btc, 8))) + ' BTC / $' + str(round(crds_usd, 4)) + final_message += '\nCRDS Value (BTC): ' + str('{0:.8f}'.format(round(crds_btc, 8))) + ' BTC / ' + str(round(crds_usd, 4)) + ' USD' final_message += '\n' total_crds_col = float(no_mn * 5000) total_btc_col = round(total_crds_col * crds_btc, 8) total_usd_col = round(total_crds_col * crds_usd, 2) - final_message += '\nTotal collateral cost for ' + str(no_mn) + ' MN: ' + str(total_btc_col) + ' BTC / $' + str(total_usd_col) + final_message += '\nTotal collateral cost for ' + str(no_mn) + ' MN: ' + str(total_btc_col) + ' BTC / ' + str(total_usd_col) + ' USD' final_message += '\n' mncount = float(mncount)