create account

[SockoBot] Adding 3 new commands and updating old code by jestemkioskiem

View this thread on: hive.blogpeakd.comecency.com
· @jestemkioskiem · (edited)
$186.92
[SockoBot] Adding 3 new commands and updating old code
![sockobotlogo.jpg](https://res.cloudinary.com/hpiynhbhq/image/upload/v1517860639/wxkxg6pnxnqrraqygxhc.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
* [github repository](https://github.com/Jestemkioskiem/steem-sockobot)
* [author](https://github.com/Jestemkioskiem/)

## 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 over [here](https://steemit.com/utopian-io/@venalbe/sockobot-ideas-for-a-monkey-faced-bot-round-01). 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](https://res.cloudinary.com/hpiynhbhq/image/upload/v1517861012/tnpiqefstq72nyb2d9gs.png)

You can see the code here:

```python
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](https://res.cloudinary.com/hpiynhbhq/image/upload/v1517861430/drslxcanwheez5gqhink.png)

You can see the code here:

```python
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](https://res.cloudinary.com/hpiynhbhq/image/upload/v1517861577/isxgmxfzaij5el5kyaog.png)

You can see the code here:

```python
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:

```python
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:


```python
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.

```python
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](https://github.com/Jestemkioskiem/steem-sockobot/commit/87e2a436857eaa3b26ea11cc7579c6b790f92f7c).

### Bonus

All of the code above has been ported and altered to work with the [sockobot-fb](https://github.com/Jestemkioskiem/steem-sockobot-fb) version of the bot that works in Facebook's Messenger!

* ```wallet```
![wallet.png](https://res.cloudinary.com/hpiynhbhq/image/upload/v1517862380/ly0yol7shndp7zdof4ya.png)

* ```convert```
![convert.png](https://res.cloudinary.com/hpiynhbhq/image/upload/v1517862386/nokhuczxdqkhiljhvekd.png)

* ```sp```
![sp.png](https://res.cloudinary.com/hpiynhbhq/image/upload/v1517862383/rk9g443xikwwl3snpxmy.png)




<br /><hr/><em>Posted on <a href="https://utopian.io/utopian-io/@jestemkioskiem/sockobot-adding-3-new-commands-and-updating-old-code">Utopian.io -  Rewarding Open Source Contributors</a></em><hr/>
👍  , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , ,
properties (23)
authorjestemkioskiem
permlinksockobot-adding-3-new-commands-and-updating-old-code
categoryutopian-io
json_metadata{"community":"utopian","app":"steemit/0.1","format":"markdown","repository":{"id":115289990,"name":"steem-sockobot","full_name":"Jestemkioskiem/steem-sockobot","html_url":"https://github.com/Jestemkioskiem/steem-sockobot","fork":false,"owner":{"login":"Jestemkioskiem"}},"pullRequests":[],"platform":"github","type":"development","tags":["utopian-io","sockobot","python","discord"],"users":["venalbe"],"links":["https://github.com/Jestemkioskiem/steem-sockobot","https://github.com/Jestemkioskiem/","https://steemit.com/utopian-io/@venalbe/sockobot-ideas-for-a-monkey-faced-bot-round-01","https://github.com/Jestemkioskiem/steem-sockobot/commit/87e2a436857eaa3b26ea11cc7579c6b790f92f7c","https://github.com/Jestemkioskiem/steem-sockobot-fb","https://utopian.io/utopian-io/@jestemkioskiem/sockobot-adding-3-new-commands-and-updating-old-code"],"image":["https://res.cloudinary.com/hpiynhbhq/image/upload/v1517860639/wxkxg6pnxnqrraqygxhc.jpg","https://res.cloudinary.com/hpiynhbhq/image/upload/v1517861012/tnpiqefstq72nyb2d9gs.png","https://res.cloudinary.com/hpiynhbhq/image/upload/v1517861430/drslxcanwheez5gqhink.png","https://res.cloudinary.com/hpiynhbhq/image/upload/v1517861577/isxgmxfzaij5el5kyaog.png","https://res.cloudinary.com/hpiynhbhq/image/upload/v1517862380/ly0yol7shndp7zdof4ya.png","https://res.cloudinary.com/hpiynhbhq/image/upload/v1517862386/nokhuczxdqkhiljhvekd.png","https://res.cloudinary.com/hpiynhbhq/image/upload/v1517862383/rk9g443xikwwl3snpxmy.png"],"moderator":{"account":"vladimir-simovic","time":"2018-02-05T22:01:08.553Z","reviewed":true,"pending":false,"flagged":false}}
created2018-02-05 20:29:06
last_update2018-02-05 22:25:39
depth0
children7
last_payout2018-02-12 20:29:06
cashout_time1969-12-31 23:59:59
total_payout_value130.820 HBD
curator_payout_value56.099 HBD
pending_payout_value0.000 HBD
promoted0.000 HBD
body_length9,070
author_reputation41,292,066,961,817
root_title"[SockoBot] Adding 3 new commands and updating old code"
beneficiaries
0.
accountutopian.pay
weight2,500
max_accepted_payout1,000,000.000 HBD
percent_hbd10,000
post_id35,227,814
net_rshares33,499,449,937,617
author_curate_reward""
vote details (40)
@mcfarhat ·
$0.06
Nice work bro!
I've been meaning to install a bot on one of my discord channels and make it available for users .. will look into using your bot then !!
👍  
properties (23)
authormcfarhat
permlinkre-jestemkioskiem-sockobot-adding-3-new-commands-and-updating-old-code-20180206t153013871z
categoryutopian-io
json_metadata{"tags":["utopian-io"],"community":"utopian","app":"utopian/1.0.0"}
created2018-02-06 15:30:21
last_update2018-02-06 15:30:21
depth1
children1
last_payout2018-02-13 15:30:21
cashout_time1969-12-31 23:59:59
total_payout_value0.046 HBD
curator_payout_value0.015 HBD
pending_payout_value0.000 HBD
promoted0.000 HBD
body_length152
author_reputation150,651,671,367,256
root_title"[SockoBot] Adding 3 new commands and updating old code"
beneficiaries[]
max_accepted_payout1,000,000.000 HBD
percent_hbd10,000
post_id35,431,228
net_rshares9,326,675,848
author_curate_reward""
vote details (1)
@jestemkioskiem ·
Hey! The README file is slightly outdated but I'll make sure it's up to date today, thank you for considering my bot <3. If you have any questions or suggestions, you know where to find me.
properties (22)
authorjestemkioskiem
permlinkre-mcfarhat-re-jestemkioskiem-sockobot-adding-3-new-commands-and-updating-old-code-20180207t120007279z
categoryutopian-io
json_metadata{"tags":["utopian-io"],"app":"steemit/0.1"}
created2018-02-07 13:00:09
last_update2018-02-07 13:00:09
depth2
children0
last_payout2018-02-14 13:00:09
cashout_time1969-12-31 23:59:59
total_payout_value0.000 HBD
curator_payout_value0.000 HBD
pending_payout_value0.000 HBD
promoted0.000 HBD
body_length189
author_reputation41,292,066,961,817
root_title"[SockoBot] Adding 3 new commands and updating old code"
beneficiaries[]
max_accepted_payout1,000,000.000 HBD
percent_hbd10,000
post_id35,657,776
net_rshares0
@steemitstats ·
@jestemkioskiem, Approve is not my ability, but I can upvote you.
properties (22)
authorsteemitstats
permlink20180205t203213844z-post
categoryutopian-io
json_metadata{"tags":["utopian-io"]}
created2018-02-05 20:32:03
last_update2018-02-05 20:32:03
depth1
children0
last_payout2018-02-12 20:32:03
cashout_time1969-12-31 23:59:59
total_payout_value0.000 HBD
curator_payout_value0.000 HBD
pending_payout_value0.000 HBD
promoted0.000 HBD
body_length65
author_reputation351,882,871,185
root_title"[SockoBot] Adding 3 new commands and updating old code"
beneficiaries[]
max_accepted_payout1,000,000.000 HBD
percent_hbd10,000
post_id35,228,371
net_rshares0
@utopian-io ·
### Hey @jestemkioskiem I am @utopian-io. I have just upvoted you!
#### Achievements
- You have less than 500 followers. Just gave you a gift to help you succeed!
- Seems like you contribute quite often. AMAZING!
#### Community-Driven Witness!
I am the first and only Steem Community-Driven Witness. <a href="https://discord.gg/zTrEMqB">Participate on Discord</a>. Lets GROW TOGETHER!
- <a href="https://v2.steemconnect.com/sign/account-witness-vote?witness=utopian-io&approve=1">Vote for my Witness With SteemConnect</a>
- <a href="https://v2.steemconnect.com/sign/account-witness-proxy?proxy=utopian-io&approve=1">Proxy vote to Utopian Witness with SteemConnect</a>
- Or vote/proxy on <a href="https://steemit.com/~witnesses">Steemit Witnesses</a>

[![mooncryption-utopian-witness-gif](https://steemitimages.com/DQmYPUuQRptAqNBCQRwQjKWAqWU3zJkL3RXVUtEKVury8up/mooncryption-s-utopian-io-witness-gif.gif)](https://steemit.com/~witnesses)

**Up-vote this comment to grow my power and help Open Source contributions like this one. Want to chat? Join me on Discord https://discord.gg/Pc8HG9x**
properties (22)
authorutopian-io
permlinkre-jestemkioskiem-sockobot-adding-3-new-commands-and-updating-old-code-20180207t232345549z
categoryutopian-io
json_metadata{"tags":["utopian-io"],"community":"utopian","app":"utopian/1.0.0"}
created2018-02-07 23:23:45
last_update2018-02-07 23:23:45
depth1
children0
last_payout2018-02-14 23:23:45
cashout_time1969-12-31 23:59:59
total_payout_value0.000 HBD
curator_payout_value0.000 HBD
pending_payout_value0.000 HBD
promoted0.000 HBD
body_length1,090
author_reputation152,955,367,999,756
root_title"[SockoBot] Adding 3 new commands and updating old code"
beneficiaries[]
max_accepted_payout1,000,000.000 HBD
percent_hbd10,000
post_id35,773,034
net_rshares0
@venalbe ·
$0.05
ahah you've been so fast implementing them! glad you liked the suggestion! Looking forward to use Sockobot, ciao!
👍  
properties (23)
authorvenalbe
permlinkre-jestemkioskiem-sockobot-adding-3-new-commands-and-updating-old-code-20180205t205112800z
categoryutopian-io
json_metadata{"tags":["utopian-io"],"app":"steemit/0.1"}
created2018-02-05 20:51:12
last_update2018-02-05 20:51:12
depth1
children0
last_payout2018-02-12 20:51:12
cashout_time1969-12-31 23:59:59
total_payout_value0.050 HBD
curator_payout_value0.003 HBD
pending_payout_value0.000 HBD
promoted0.000 HBD
body_length113
author_reputation2,357,857,338,750
root_title"[SockoBot] Adding 3 new commands and updating old code"
beneficiaries[]
max_accepted_payout1,000,000.000 HBD
percent_hbd10,000
post_id35,232,066
net_rshares8,020,835,680
author_curate_reward""
vote details (1)
@vladimir-simovic ·
$1.78
Thank you for the contribution. It has been approved.

You can contact us on [Discord](https://discord.gg/uTyJkNm).
**[[utopian-moderator]](https://utopian.io/moderators)**
👍  
properties (23)
authorvladimir-simovic
permlinkre-jestemkioskiem-sockobot-adding-3-new-commands-and-updating-old-code-20180205t220132366z
categoryutopian-io
json_metadata{"tags":["utopian-io"],"links":["https://discord.gg/uTyJkNm","https://utopian.io/moderators"],"app":"steemit/0.1"}
created2018-02-05 22:01:33
last_update2018-02-05 22:01:33
depth1
children1
last_payout2018-02-12 22:01:33
cashout_time1969-12-31 23:59:59
total_payout_value1.334 HBD
curator_payout_value0.444 HBD
pending_payout_value0.000 HBD
promoted0.000 HBD
body_length172
author_reputation56,930,790,558,862
root_title"[SockoBot] Adding 3 new commands and updating old code"
beneficiaries[]
max_accepted_payout1,000,000.000 HBD
percent_hbd10,000
post_id35,245,071
net_rshares259,145,118,095
author_curate_reward""
vote details (1)
@utopian.tip ·
Hey @vladimir-simovic, I just gave you a tip for your hard work on moderation. Upvote this comment to support the utopian moderators and increase your future rewards!
properties (22)
authorutopian.tip
permlinkre-re-jestemkioskiem-sockobot-adding-3-new-commands-and-updating-old-code-20180205t220132366z-20180206t133840
categoryutopian-io
json_metadata""
created2018-02-06 13:38:42
last_update2018-02-06 13:38:42
depth2
children0
last_payout2018-02-13 13:38:42
cashout_time1969-12-31 23:59:59
total_payout_value0.000 HBD
curator_payout_value0.000 HBD
pending_payout_value0.000 HBD
promoted0.000 HBD
body_length166
author_reputation238,310,597,885
root_title"[SockoBot] Adding 3 new commands and updating old code"
beneficiaries[]
max_accepted_payout1,000,000.000 HBD
percent_hbd10,000
post_id35,407,570
net_rshares0