[Tutorial] Create Our Own Discord Bot (Part 4) - Votes

in #utopian-io6 years ago

DDDDD.png

[Repository]https://github.com/Carlososuna11/Discord-Tutorial-4)

What's up friends, with my most recent publication about Navi, many people have asked me, "Anonym0us, How can I make my own bot for my discord community?", So I decided to upload this simple, but essential tutorial about our own Discord bot, which will be adding more functions to these weeks, but for now we will do something simple: Votes.

What Is a Discord Bot?

If you do not know what a Discord bot is, friend let me tell you that you do not live in the 21st century or have never used Discord. The definition of a bot is simple

A Discord bot is like a phones Siri.
It’s not necessary to do what you need to do, but it provides functionality and various tools that enable your server to be more lightweight, and streamlined. For example, in order to ban someone from your server, you’d need to go to their profile, scroll down and find the ban @name option. Whereas a discord bot could allow you to do that by inputting a short command in to the chat line, e.g. *.Ban name — this reduces the hassle required to find and scroll. It's quicker, not necessarily better.

What Will I Learn?

You will learn how to make automatic votes, only having the privatekey and the user of the steem account.

Requirements

Difficulty

  • Basic

Votes

With the previous tutorials, we know how to create a bot, upload it to a server, create a database to have the user and password of each user, but this is the most important tutorial, votes, the steem network moves by votes Whoever has more votes and greater voting power is considered a big shot.

This tutorial is divided into two parts, the voting part, where with the data already stored in the database the bot to vote a post, and the part of knowing who voted the post, for it hands-on.

1st Function: Vote

For this function we need to have done the steps in previous tutorials before published, the function consists of the following: when executing a command, the bot will process the function, giving a confirmation message, to then access the user database and privatekey of steem, once access to said variables, will proceed with the execution of the vote, it is very simple.We will add this part of the code to the bot.js script

var regexc = /(\$)+(upvote)+.+(https:\/\/)+.+(@)+.+(\/)/; 
var regexa = /(@)+.+(\/)/;
if(msg.content.match(regexc) &&  msg.channel.id == 'CHANNEL ID'){
    var permlink= msg.content.replace(msg.content.match(regexc)[0],"");
    var aur = msg.content.match(regexa)[0];
    var aut = aur.replace("@","");
    var author = aut.replace("/","");
    var channel = msg.channel.id;
    var uid = msg.author.id;
        var power = 10000;//100%

   bot.createMessage(channel,"<@!" + uid + ">"+ ' The bot navi has just processed your request for votes, please wait a moment and do not send the post again :)'); //confirmation message of the function
        con.query('SELECT userid, usuario, wifkey FROM voter WHERE id = ?', [uid], function(error, result){
        if(error){
           throw error;
        }else{
           var resultado = result;
           if(resultado.length > 0){
               useridx = resultado[0].userid; //USER_ID
               votey = resultado[0].usuario;//STEEM USER VOTER
               wifkey = resultado[0].wifkey// PRIVATEKEY
               steem.broadcast.vote(wifkey,votey,author,permlink,power,function(downerr, result){
                if(downerr){
                    bot.createMessage(channel,"<@!" + uid + ">"+ ' It was already voted');
                    }
                if(result) {
                        bot.createMessage(channel,"<@!" + uid + ">"+ 'your post got a vote');
                    }
            });
           }else{
              console.log('not registered');
           }
        }
    });
}

with this script, you will be able to vote with your account the link that you place.

Now comes the second part, which would happen if we wanted to know the datso of a post, who voted, percentage, etc., for this we will use the following function.

2st Function:Know the data of a post .

var regex78 = /(\$)+(Votes)+.+(https:\/\/)+.+(@)+.+(\/)/; 
var regex77 = /(@)+.+(\/)/;
if(msg.content.match(regex78)){
   var permlink= msg.content.replace(msg.content.match(regex78)[0],"");
   var au = msg.content.match(regex77)[0];
   var aut = au.replace("@","");
   var author = aut.replace("/","");
   var channel = msg.channel.id;
   var uid = msg.author.id;
   steem.api.getContent(author, permlink, function(err, result) {
       var info1 = result.author;
       var info2 = result.permlink;
       var info3 = result.category;
       var info4 = result.parent_author;
       var info5 = result.parent_permlink;
       var info6 = result.title;
       var info7 = result.body;
       var info8 = result.json_metadata;
       var info9 = result.last_update;
       var info10 = result.created;
       var info11 = result.active;
       var info12 = result.last_payout;
       var info13 = result.depth;
       var info14 = result.children;
       var info15 = result.cashout_time;
       var info16 = result.reward_weight;
       var info17 = result.total_payout_value ;
       var info18 = result.curator_payout_value;
       var info19 = result.author_rewards;
       var info20 = result.max_accepted_payout;
       var info21 = result.percent_steem_dollars;
       var info22 = result.allow_replies;
       var info23 = result.allow_votes;
       var info24 = result.allow_curation_rewards;
       var info25 = result.beneficiaries;
       var info26 = result.pending_payout_value;
       var info27 = result.total_pending_payout_value;
       var info30 = result.promoted;
       bot.createMessage(channel, "<@!" + uid + ">" + '  The information provided from the following steemit link is:...' );
       bot.createMessage(channel, 'Steem User: ' + info1 + ' ' );
       bot.createMessage(channel, 'Link: ' + info2 + ' ' );
       bot.createMessage(channel, 'Category: ' + info3 + ' ' );
       bot.createMessage(channel, 'Parent Author: ' + info4 + ' ' );
       bot.createMessage(channel, 'Parent Link: ' + info5 + ' ' );
       bot.createMessage(channel, 'Title Of Post: ' + info6 + ' ' );
       bot.createMessage(channel, 'Body Of Post: ' + info7 + ' ' );
       bot.createMessage(channel, 'JSON Metadata: ' + info8 + ' ' );
       bot.createMessage(channel, 'Last Update: ' + info9 + ' ' );
       bot.createMessage(channel, 'Created: ' + info10 + ' ' );
       bot.createMessage(channel, 'Active: ' + info11 + ' ' );
       bot.createMessage(channel, 'Last Payout: ' + info12 + ' ' );
       bot.createMessage(channel, 'Depth: ' + info13 + ' ' );
       bot.createMessage(channel, 'Children: ' + info14 + ' ' );
       bot.createMessage(channel, 'Cashout Time: ' + info15 + ' ' );
       bot.createMessage(channel, 'Reward Weight: ' + info16 + ' ' );
       bot.createMessage(channel, 'Total Payout Value: ' + info17 + ' ' );
       bot.createMessage(channel, 'Curator Payout Value: ' + info18 + ' ' );
       bot.createMessage(channel, 'Author Rewards: ' + info19 + ' ' );
       bot.createMessage(channel, 'Max Accepted payout: ' + info20 + ' ' );
       bot.createMessage(channel, 'Percent Steem Dollars: ' + info21 + ' ' );
       bot.createMessage(channel, 'Allow Replies: ' + info22 + ' ' );
       bot.createMessage(channel, 'Allow Votes: ' + info23 + ' ' );
       bot.createMessage(channel, 'Allow Curation Rewards: ' + info24 + ' ' );
       bot.createMessage(channel, 'Beneficiaries: ' + info25 + ' ' );
       bot.createMessage(channel, 'Pending Payout Value:  ' + info26 + ' ' );
       bot.createMessage(channel, 'Total Pending Payout Value: ' + info27 + ' ' );
       bot.createMessage(channel, 'Promoted: ' + info30 + ' ' );
       steem.api.getActiveVotes(author, permlink, function(err, result) {
           var show = JSON.stringify(result);
           bot.createMessage(channel, 'Votes: ' + ' ' );
           bot.createMessage(channel, '' + show +' ' );
           steem.api.getAccounts([author], function(err, result) {
               if (!err) {
                   const secondsago = (new Date().getTime() - new Date(result[0].last_vote_time + "Z").getTime()) / 1000;
                   const votingPower = (result[0].voting_power + (10000 * secondsago / 432000)) / 100 ;
                   var n = votingPower.toFixed(2);
                   bot.createMessage(channel, "<@!" + uid + ">" + ' Steem Account Reputation: ' + steem.formatter.reputation(result[0].reputation) + '' );
                   bot.createMessage(channel, "<@!" + uid + ">" + ' Percent of Voting Power'+n+ '%' );
                   bot.createMessage(channel, 'These are all the data shown about the post' );
                   }
               });
           });        
     });
}

with this script, we can know the different data about a post, which to execute the function, will return the following values:
Steem User: steem user.
Link: link of the post.
Category: The main category/tag this content belongs to.
Parent Author: Parent author, in case this content is a comment (reply).
Parent Link: Parent permanent link
Title Of Post:
Body Of Post:
JSON Metadata:
Last Update: The date and time of the last update to this content.
Created:
Active:
Last Payout:
Depth:
Children:
Cashout Time:
Reward Weight:
Total Payout Value:
Curator Payout Value:
Author Rewards:
Max Accepted payout:
Percent Steem Dollars:
Allow Replies:
Allow Votes:
Allow Curation Rewards:
Beneficiaries:
Pending Payout Value
Total Pending Payout Value:
Promoted:
Votes:
Steem Account Reputation
Percent of Voting Power

And this is the result

455.png

I hope you have learned with this tutorial, the following tutorial will be dedicated to the votes !!!!
The work test done is called Navi

Curriculum

You can go through my profile to see other publications of other categories approved by @utopian.

Sort:  

Thank you for your contribution @anonym0us.
We have been examining your contribution and suggest the following points:

  • We suggest you put comments in your code. The comments in the code help the reader to interpret the code more easily.

  • Instead of putting the value in a variable, it is easier to print the value directly, for example:

bot.createMessage(channel, 'Steem User: ' + result.author + ' ' );



Thanks for the elaboration of your tutorial, we are waiting for the next one.

Your contribution has been evaluated according to Utopian policies and guidelines, as well as a predefined set of questions pertaining to the category.

To view those questions and the relevant answers related to your post, click here.


Need help? Write a ticket on https://support.utopian.io/.
Chat with us on Discord.
[utopian-moderator]

Thank you for your review, @portugalcoin!

So far this week you've reviewed 8 contributions. Keep up the good work!

Hi @anonym0us!

Your post was upvoted by @steem-ua, new Steem dApp, using UserAuthority for algorithmic post curation!
Your post is eligible for our upvote, thanks to our collaboration with @utopian-io!
Feel free to join our @steem-ua Discord server

Congratulations @anonym0us! You have completed the following achievement on the Steem blockchain and have been rewarded with new badge(s) :

Award for the total payout received

Click on the badge to view your Board of Honor.
If you no longer want to receive notifications, reply to this comment with the word STOP

Do not miss the last post from @steemitboard:

SteemitBoard Ranking update - Steem Power, Followers and Following added

Support SteemitBoard's project! Vote for its witness and get one more award!

Hey, @anonym0us!

Thanks for contributing on Utopian.
We’re already looking forward to your next contribution!

Get higher incentives and support Utopian.io!
Simply set @utopian.pay as a 5% (or higher) payout beneficiary on your contribution post (via SteemPlus or Steeditor).

Want to chat? Join us on Discord https://discord.gg/h52nFrV.

Vote for Utopian Witness!

Coin Marketplace

STEEM 0.15
TRX 0.16
JST 0.028
BTC 67628.32
ETH 2424.36
USDT 1.00
SBD 2.35