Parsbot | Discord Bot Tutorial Node.js | Part 7

in #utopian-io7 years ago (edited)

Node.js is a JavaScript runtime built on Chrome's V8 JavaScript engine. Node.js uses an event-driven, non-blocking I/O model that makes it lightweight and efficient. Node.js' package ecosystem, npm, is the largest ecosystem of open source libraries in the world.
Source


1.png

Image Source


Useful Links

Node.js Website

Node.js Github

Parsbot Github


What Will I Learn?

  • New Command for All Account Datas with Account Image (accdata)

  • New Command Help (help)

  • Run and test Parsbot


Requirements


Difficulty

  • Intermediate

Curriculum

Parsbot | Discord Bot Tutorial Node.js | Part 4

Parsbot | Discord Bot Tutorial Node.js | Part 5

Parsbot | Discord Bot Tutorial Node.js | Part 6

For other tutorials you can visit my profile.


Tutorial Contents

In this tutorial we will add two new command for our bot. Our first command will show the all account datas with account image in the message embed (accdatas). To do this, we will use the commands we have prepared in our previous lessons in a single command (reputation, vote power, last vote, follower,follows etc.). Our second command will show the all bot commands for to help the user (help).

1.png

New Command for All Account Datas with Account Image (accdata)

Now we will add a new command to see the all account datas with account image in the message embed.

Now, let's write the "else if" section of our command "app.js" file.

Code:

else if (msgorg.startsWith(prefix + 'accdata')) {

}

1.png

Now we will create a variable called "accountname". We will receive our message in this variable, and we will delete the "$accdata" section.

Code:

let accountname = msgorg.replace(prefix + 'accfow ','');

2.png

In this way we have successfully acquired steem account name.

Now we can create a request for steem account datas with steem.js. And I get steem account profile name and profile image to our variables.

Code:


steemjs.api.getAccounts([accountname],

function(err,result)

{

if(result["0"] === undefined)

{

console.log("Invalid Acccount ID");

message.channel.send('Invalid Acccount ID');

}

else

{

let profile = JSON.parse(result[0].json_metadata).profile;

let profile_image = profile.profile_image;

let name = profile.name;

if (name === undefined)

{

name = result[0].name;

}


In this way we have successfully acquired steem account datas. And we gave "name" and "profile_image" variables account values.

3.png

Now, we will take the account reputation score of to our "reputation" variable. And calculate the power of the vote to account and we will take our "vtpower" variable.

Code:


let reputation = steemjs.formatter.reputation(result["0"].reputation);

var secondsago = (new Date - new Date(result[0].last_vote_time + "Z")) / 1000;

var vpow = result[0].voting_power + (10000 * secondsago / 432000);

vtpower = Math.min(vpow / 100, 100).toFixed(2);


4.png

Now we will calculate our steem power,delegated_steem_power and take it to our variables.

Code:


var vesting_shares, delegated_vesting_shares, received_vesting_shares, total_vesting_shares , total_vesting_fund_steem=null;

vesting_shares = result[0].vesting_shares;

delegated_vesting_shares = result[0].delegated_vesting_shares;

received_vesting_shares = result[0].received_vesting_shares;

steemjs.api.getDynamicGlobalProperties(function(err, gresult) {

total_vesting_shares=gresult.total_vesting_shares;

total_vesting_fund=gresult.total_vesting_fund_steem;

var steem_power = steemjs.formatter.vestToSteem(vesting_shares, total_vesting_shares, total_vesting_fund);

var delegated_steem_power = steemjs.formatter.vestToSteem((received_vesting_shares.split(' ')[0]-delegated_vesting_shares.split(' ')[0])+' VESTS', total_vesting_shares, total_vesting_fund);


In our code above, after we got the values we needed for our datas, we made our calculations and got the results of our variables.

5.png

Now, we will take the account created day of to our "accountcreateddays" variable. And we will get our last vote date and time in our variable "lastvote".

Code:


var today= moment(Date.now());

var accountcreated = moment(result[0].created);

var accountcreateddays = today.diff(accountcreated, 'days');

var lastvote = moment(result[0].last_vote_time).add(3,'hours');


We got account last vote date time and account creation date to our variables.

6.png

Now, we will take the account follow and followers count of to our variables.

Code:


steemjs.api.getFollowCountAsync((accountname),

function(err,fresult)

{

var follower_count = fresult.follower_count;

var following_count = fresult.following_count;

})


7.png

Now we will prepare and send our message embed.

Code:


const embed = new Discord.RichEmbed()

.setThumbnail(profile_image)

.setColor(0x5795FE)

.setAuthor(name)

.addField('Reputation',reputation)

.addField('Account Created',accountcreateddays + ' days ago')

.addField('Steem Power',(steem_power+delegated_steem_power).toFixed(2) + ' SP ' + '(' + steem_power.toFixed(2) + ' / ' + delegated_steem_power.toFixed(2) + ')')

.addField('Voting Power',vtpower)

.addField('Last Vote',lastvote.format("YYYY-MM-DD HH:mm"))

.addField('Followers',follower_count)

.addField('Follows',following_count);

message.channel.send(embed);


We successfully created and sent our embed with all account datas.

8.png


Command Discord View:

9.png


New Command Help (help)

Now we will add a "help" command to see all our commands and their usages. This will make it easier for our users to use Parsbot commands.

Now, let's write the "else if" section of our command. And I add a image for our embed view.

Code:


else if (msg === prefix + 'HELP') {

var help_image = "https://i.hizliresim.com/nOW20l.png";

}


10.png

Now we will prepare and send our message embed.

Code:


const embed = new Discord.RichEmbed()

.setThumbnail(help_image)

.setColor(0xFEC057)

.setAuthor('Help')

.addField('Ping','Usage:$ping')

.addField('Hi','Usage:$hi')

.addField('Merhaba','Usage:$merhaba')

.addField('Selam','Usage:$selam')

.addField('Price','Usage:$price steem-dollars')

.addField('Account Reputation Score','Usage:$rep accountname')

.addField('Account Voting Power','Usage:$vpower accountname')

.addField('Account Created','Usage:$accreated accountname')

.addField('Account Steem Power','Usage:$accspower accountname')

.addField('Account Follow/Followers','Usage:$accfow accountname')

.addField('Account Last Vote','Usage:$accltvote accountname')

.addField('Account All Data','Usage:$accdata accountname')

message.channel.send(embed);


We added all our commands and uses to our embed and sent them.

11.png

Our help command is complete.

Command Discord View:

12.png


Run and test Parsbot

Now we have to run and test our discord bot.

Open cmd and write "cd yourbotdirectory" and press enter. I write "cd C:\Users\gffdg\Desktop\Discord Pars Bot\parsbot".

9.png

Now we will run our app.js file with node.

node app.js

11.png

Our bot started successfully and printed "Pars Bot started" message on our ready event.

Discord:


9.png


12.png


We came to the end of our education. Thank you for your attention.



Posted on Utopian.io - Rewarding Open Source Contributors

Sort:  

Thank you for the contribution. It has been approved.

You can contact us on Discord.
[utopian-moderator]

Hey @rdvn, 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!

Harika bir tutorial serisi oluyor. Teşekkürler! 😊

Ben teşekkür ederim herkese faydalı olması dileğiyle

Hey @pars11 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!

Suggestions

  • Contribute more often to get higher and higher rewards. I wish to see you often!
  • Work on your followers to increase the votes/rewards. I follow what humans do and my vote is mainly based on that. Good luck!

Get Noticed!

  • Did you know project owners can manually vote with their own voting power or by voting power delegated to their projects? Ask the project owner to review your contributions!

Community-Driven Witness!

I am the first and only Steem Community-Driven Witness. Participate on Discord. Lets GROW TOGETHER!

mooncryption-utopian-witness-gif

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

Coin Marketplace

STEEM 0.19
TRX 0.15
JST 0.029
BTC 63562.42
ETH 2649.15
USDT 1.00
SBD 2.77