Parsbot | Discord Bot Tutorial Node.js | Part 6

in #utopian-io8 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 Account Follow and followers (accfow)

  • New Command Account Last Vote (accltvote)

  • Run and test Parsbot


Requirements


Difficulty

  • Intermediate

Curriculum

Parsbot | Discord Bot Tutorial Node.js | Part 3

Parsbot | Discord Bot Tutorial Node.js | Part 4

Parsbot | Discord Bot Tutorial Node.js | Part 5

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 account follow and followers. Our second command will show the last vote date and time of the account.

New Command Account Follow and followers (accfow)

Now we will add a new command to see Steem account follow and followers.

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

Code:

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

}

1.png

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

Code:


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


In this way we have successfully acquired steem account name.

Now we can create a request for follow and followers with steem.js.

Code:


steemjs.api.getFollowCountAsync((accountname),

function(err,fresult)

{

});


Okay, now I declare two variable "follower_count" and "following_count". And I am making assignments from our request.

Code:


var follower_count = fresult.follower_count;

var following_count = fresult.following_count;


4.png

Now I define message and send.

Code:


var follower_count = fresult.follower_count;

var following_count = fresult.following_count;


5.png

Now, I am adding "if invalid account id" part for invalid account names.

Code:


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

{

console.log("Invalid Acccount ID");

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

}

else

{

var follower_count = fresult.follower_count;

var following_count = fresult.following_count;

var accfow = (accountname + " Follower:" + (follower_count) + ' Following:' + following_count);

message.channel.send(accfow);

}


6.png

We say that if result (our account data) not defined, print "Invalid Acccount ID" message to our console and send the "Invalid Acccount ID" messsage to channel. Else get the account follow and followers info and send message.

All codes:


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

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

steemjs.api.getFollowCountAsync((accountname),

function(err,fresult)

{

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

{

console.log("Invalid Acccount ID");

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

}

else

{

var follower_count = fresult.follower_count;

var following_count = fresult.following_count;

var accfow = (accountname + " Follower:" + (follower_count) + ' Following:' + following_count);

message.channel.send(accfow);

}

});

}


7.png

In this code if received message is "$accfow steemaccountname" we will send the steemaccountname follower count and follow count message using our bot and Steem API.

We have integrate successfuly our bot into Steem account Follow and followers control.


New Command Account Last Vote (accltvote)

Now we are adding our second command to see the last vote date and time of the account.

Code:

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

}

8.png

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

Code:

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

In this way we have successfully acquired steem account name.

9.png

Now we can create a request for account last vote date and time.

Code:


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

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

steemjs.api.getAccounts([accountname],

function(err,result)

{

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

{

console.log("Invalid Acccount ID");

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

}

else

{

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

var lastvotemes = accountname + " Last Vote:" + (lastvote.format("YYYY-MM-DD HH:mm"));

message.channel.send(lastvotemes);

}

});

}


In our code above, we sent the api our steem account name. We created our request function to capture the incoming data (result). We say that if result (our account data) not defined, print "Invalid Acccount ID" message to our console and send the "Invalid Acccount ID" messsage to channel.
Else I get the date and time of the account last vote.And for Turkey UTC add 3 hours (you can customize it for yourself) and send "accountname + Last Vote: + (lastvote)" message.

10.png

In this code if received message is "$accltvote steemaccountname" we will send the steemaccountname last vote date and time message using our bot and Steem API.

We have integrate successfuly our bot into Steem account last vote date time control.


Run and test Parsbot

Okay, 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:
11.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 @kizilelma, 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!

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.04
TRX 0.32
JST 0.090
BTC 61862.56
ETH 1728.70
USDT 1.00
SBD 0.38