[SockoBot] Adding 3 new commands and updating old code

Words
407
Reading
2 min
Listen
Play
9y

sockobotlogo.jpg

SockoBot is trying to be the only tool you'll ever need on your Discord Server or Facebook Page when it comes to steem, while also being easily expandable to anyone that knows a bit of Python.

SockoBot

Languages

  • Python 3.6

New Features

What feature(s) did you add?

In this update, I addressed some of the requests suggested to me by venalbe@venalbe over here. Namely, I've added the $wallet and $blocktrades (now known as $convert) commands. On top of that, I've decided to bring a command for showing just the Steem Power and delegations of the user - $sp. To deal with these issues, I've added 2 new functions that aided me in creating these functionalities, but will also be really useful if the bot ever expands in these directions, calculate_steem_power() and calculate_estimated_acc_value(). On top of that, I've updated the $price function to work with all the coins just like the facebook version of the bot does.

Commands:
  • The wallet command was added, which fetches and displays data that can be found in the wallet page of steemit.com and most other big apps, namely:
    wallet_discord.png

You can see the code here:

elif text.lower().startswith('wallet'):
        try:
            user_name = text.split(' ')[1]
        except IndexError:
            await client.send_message(msg.channel, str("Too few arguments provided"))
            return 0
        
        acc = Account(user_name, steemd_instance=s)
        url = requests.get('https://steemitimages.com/u/' + user_name + '/avatar/small', allow_redirects=True).url

        vests = float(acc['vesting_shares'].replace('VESTS', ''))
        sp = calculate_steem_power(vests)
        rec_vests = float(acc['received_vesting_shares'].replace('VESTS', ''))
        rec_sp = calculate_steem_power(rec_vests)
        del_vests = float(acc['delegated_vesting_shares'].replace('VESTS', ''))
        del_sp = calculate_steem_power(del_vests)
        sp_diff = round(rec_sp - del_sp, 2)
        voting_power = round(float(Account(user_name)['voting_power'] / 100), 2)
        estimated_upvote = round(calculate_estimated_upvote(user_name), 2)

        embed=discord.Embed(color=0xe3b13c)
        embed.set_author(name='@' + user_name, icon_url=url)
        embed.add_field(name="Steem", value=str(str(acc['balance'].replace('STEEM', ''))), inline=True)
        embed.add_field(name="Steem Dollars", value=str(acc['sbd_balance'].replace('SBD', '')), inline=True)
        if sp_diff >= 0:
            embed.add_field(name="Steem Power", value=str(sp) + " ( +" + str(sp_diff) + ")", inline=True)
        else:
            embed.add_field(name="Steem Power", value=str(sp) + " ( " + str(sp_diff) + ")", inline=True)
        embed.add_field(name="Estimated Account Value", value=str(calculate_estimated_acc_value(user_name)), inline=True)
        embed.add_field(name="Estimated Vote Value", value=str(estimated_upvote) + " $", inline=True)
        embed.set_footer(text="SockoBot - a Steem bot by Vctr#5566 (@jestemkioskiem)")

        await client.send_message(msg.channel, embed=embed)
  • The convert command was added, which takes an amount and 2 coins as arguments and displays the amount you can receive upon conversion.
    convert_discord.png

You can see the code here:

elif text.lower().startswith('convert'):
    try:
        value = text.split(' ')[1]
        coin1 = text.split(' ')[2].lower()
        coin2 = text.split(' ')[3].lower()
    except IndexError:
        await client.send_message(msg.channel, str("Too few arguments provided"))
        return None

    try:
        price1 = cmc.ticker(coin1, limit="3", convert="USD")[0].get("price_usd", "none")
        price2 = cmc.ticker(coin2, limit="3", convert="USD")[0].get("price_usd", "none")
    except Exception:
        await client.send_message(msg.channel, str("You need to provide the full name of the coin (as per coinmarketcap)."))
        
    conv_rate = float(price1)/float(price2)
    outcome = float(value) * conv_rate

    await client.send_message(msg.channel, str("You can receive " + str(outcome) + " **" + coin2 + "** for " + str(value) + " **" + coin1 + "**." ))
  • The sp command was added, which fetches and displays the steem power and delegations of a given user.
    sp_wallet.png

You can see the code here:

elif text.lower().startswith('sp'):
        try:
            user_name = text.split(' ')[1]
        except IndexError:
            await client.send_message(msg.channel, str("Too few arguments provided"))
            return 0

        acc = Account(user_name, steemd_instance=s)
        url = requests.get('https://steemitimages.com/u/' + user_name + '/avatar/small', allow_redirects=True).url
        
        vests = float(acc['vesting_shares'].replace('VESTS', ''))
        sp = calculate_steem_power(vests)
        rec_vests = float(acc['received_vesting_shares'].replace('VESTS', ''))
        rec_sp = calculate_steem_power(rec_vests)
        del_vests = float(acc['delegated_vesting_shares'].replace('VESTS', ''))
        del_sp = calculate_steem_power(del_vests)
        sp_diff = round(rec_sp - del_sp, 2)

        embed=discord.Embed(color=0xe3b13c)
        embed.set_author(name='@' + user_name, icon_url=url)
        embed.add_field(name="Steem Power", value=str(sp), inline=True)
        if sp_diff >= 0:
            embed.add_field(name="Delegations", value="+" + str(sp_diff), inline=True)
        else:
            embed.add_field(name="Delegations", value=str(sp_diff), inline=True)

        await client.send_message(msg.channel, embed=embed) 
Functions:
  • The calculate_estimated_acc_value() function was added, which takes user_name as an input, and returns the estimated account value based on current prices of STEEM and SBD (fetched from coinmarketcap)

You can see the code here:

steem_price = float(cmc.ticker('steem', limit="3", convert="USD")[0].get("price_usd", "none"))
sbd_price = float(cmc.ticker('steem-dollars', limit="3", convert="USD")[0].get("price_usd", "none"))

acc = Account(user_name, steemd_instance=s)
vests = float(acc['vesting_shares'].replace('VESTS', ''))
sp = calculate_steem_power(vests)
steem_balance = float(acc['balance'].replace('STEEM', ''))
sbd_balance = float(acc['sbd_balance'].replace('SBD', ''))
outcome = round(((sp + steem_balance) * steem_price ) + (sbd_balance * sbd_price), 2)

return str(outcome) + " USD"
  • The calculate_steem_power() function was added, which takes VESTS as an input, and returns a value of STEEM POWER based on information from the blockchain.

You can see the code here:

post = '{"id":1,"jsonrpc":"2.0","method":"get_dynamic_global_properties", "params": []}'
response = session_post('https://api.steemit.com', post)
data = json.loads(response.text)
data = data['result']

total_vesting_fund_steem = float(data['total_vesting_fund_steem'].replace('STEEM', ''))
total_vesting_shares = float(data['total_vesting_shares'].replace('VESTS', ''))

return round(total_vesting_fund_steem * (float(vests)/total_vesting_shares), 2)
Other

On top of that, some other small changes were made:

  • The bot will now respond to the $price command the same way the facebook version does - it works with all the coins & tokens listed on coinmarketcap instead of just SBD, STEEM and BTC.
elif text.lower().startswith('price'):
    try:
        coin = text.split(' ')[1].lower()
    except IndexError:
        return str("Too few arguments provided")
        
    try: 
        value = cmc.ticker(coin, limit="3", convert="USD")[0].get("price_usd", "none")
        await client.send_message(msg.channel, str("The current price of **"+ coin +"** is: *" + str(value) + "* USD."))
    except Exception:
        await client.send_message(msg.channel, str("You need to provide the full name of the coin (as per coinmarketcap)."))         

How did you implement it/them?

All of the code is visible above, it can also be accessed in the commit 87e2a43.

Bonus

All of the code above has been ported and altered to work with the sockobot-fb version of the bot that works in Facebook's Messenger!

  • wallet
    wallet.png

  • convert
    convert.png

  • sp
    sp.png



Posted on Utopian.io - Rewarding Open Source Contributors

[SockoBot] Adding 3 new commands and updating old code | Ecency