[Update] Voter.js v0.03: Discord Auto Upvote Nodejs Bot - How to Upvote Only Posts Older than 20 Minutes.steemCreated with Sketch.

in #utopian-io7 years ago (edited)

android-150996.png

After writing 2 Article about Discord Upvoting Nodejs Bot, Some users Requested New features.
so, Let's Work on this project too.

Project is for learning Javascript and Nodejs and using steem-js library

You can see last Posts Below:

  1. Complete Guide to Creating Discord Auto Upvote Bot by Javascript.
  2. [Update] Voter.js v0.02: Discord Auto Upvote Bot - How to Add Registering Feature
    Note: First Article has some missed parts which is Fixed in Second Article.

Checking Post's Created Time:

If you want to Earn more Curation Reward From you upvotes, you need to wait a few minutes before upvoting new Posts.
upvoting 0 Seconds after Creating post will give you 0% curation reward. and Upvoting Posts older than 30 minutes will give you 100% curation Reward.

For Earning more Curation Reward by your Discord Bot, you can add one if() and Check Post's Created time before broadcasting Upvote. and upvote that post if that was older than 20 Minutes. (You can Change it to any amount)

If you used Our Recent Bot (v 0.02) Full Code, Follow these instructions. otherwise, you can use it in your modified Script too. it is easy as just a Copy and a Past.

First I used a variable:

var postage = 1200; 

it will be our Minimum requirement Post Age for upvoting a post. (I used 1200 Seconds which is 20 minutes)

then we need to check post's created time and use one if() for checking this time:

steem.api.getContent(author, permlink, function(err, result) {
    var datee = new Date();
    var secondss = datee.getTime()/1000;
    var datee1 = new Date(result.created+'Z');
    var secondss1 = datee1.getTime()/1000;
    if((secondss-secondss1)> postage){
    }else{
    }
});

Then We can add our Upvote Broadcasting Codes and Failure Messages to if and else sections.

Full Code Will be sth like:

Full Code v0.03:

Note: It is Edited Code of Version 0.02


const Eris = require("eris");
var steem = require("steem");
var mysql = require('mysql');
var XMLHttpRequest = require("xmlhttprequest").XMLHttpRequest;
var con = mysql.createConnection({
  host: "127.0.0.1",
  user: "root",
  password: "mysql password",
  database: "voter"
});
con.connect();
var bot = new Eris("Your Bot Token");
var regex = /(\$)+(upvote)+.+(https:\/\/)+.+(@)+.+(\/)/;
var regex1 = /(@)+.+(\/)/;
var wifkey = 'Private Posting Key';
var votey = "mahdiyari";
var weight = 5000; // 10000 = 100%
bot.on("ready", () => {console.log('voter bot started!');}); //when it is ready
bot.on("messageCreate", (msg) => { // when a message is created
    if(msg.content.match(regex)){
        var permlink= msg.content.replace(msg.content.match(regex)[0],"");
        var au = msg.content.match(regex1)[0];
        var aut = au.replace("@","");
        var author = aut.replace("/","");
        var channel = msg.channel.id;
        var uid = msg.author.id;
        var x = '0';
        con.query('SELECT EXISTS(SELECT * FROM `voter` WHERE `userid` = "'+uid+'")', function (error, results, fields) {
            for(i in results){
                for(j in results[i]){
                    x = results[i][j];
                    if(x == '1'){
                        var last;
                        var postage = 1200; // 1200 Seconds = 20 minutes
                        steem.api.getContent(author, permlink, function(err, result) {
                            var datee = new Date();
                            var secondss = datee.getTime()/1000;
                            var datee1 = new Date(result.created+'Z');
                            var secondss1 = datee1.getTime()/1000;
                            if((secondss-secondss1)> postage){
                                con.query('SELECT `lastvote` FROM `voter` WHERE `userid`="'+uid+'"', function (error, results, fields) {
                                    for(i in results){
                                        for(j in results[i]){
                                            last = results[i][j];
                                        }
                                    }
                                    var time = Math.floor(new Date().getTime() / 1000);
                                        if((time - last) > 86400){
                                            con.query('UPDATE `voter` SET `lastvote`="'+time+'" WHERE `userid`="'+uid+'"', function (error, results, fields) {
                                                steem.broadcast.vote(wifkey,votey,author,permlink,weight,function(downerr, result){
                                                    if(downerr){
                                                        setTimeout(function(){bot.createMessage(channel,'Already Upvoted!');},1000);
                                                        con.query('UPDATE `voter` SET `lastvote`="'+last+'" WHERE `userid`="'+uid+'"', function (error, results, fields) {
                                                        });
                                                    }
                                                    if(result) {
                                                        setTimeout(function(){bot.createMessage(channel,'Done! Your Post Upvoted By @mahdiyari');},1000);
                                                    }
                                                });
                                            });
                                        }else{
                                            var come = 86400 - (time - last);
                                            setTimeout(function(){bot.createMessage(channel,'Sorry! Come back after '+come+' seconds.');},1000);
                                        }
                                }); 
                            }else{
                                setTimeout(function(){bot.createMessage(channel,'Your Post Must be Older Than '+postage/60+' Minutes.')},1000);
                            }
                        });
                    }else{
                        setTimeout(function(){bot.createMessage(channel,'You Are not Registered.');},1000);
                    }
                }
            }
        });
    }
    var regex2 = /(\$)+(register)+(\ )/;
    if(msg.content.match(regex2)){
        var sender = msg.content.replace(msg.content.match(regex2)[0],"");
        var memo = 'register';
        var transaction = 'Receive  0.001 SBD from '+sender;
        var r = 0;
        var channel = msg.channel.id;
        con.query('SELECT EXISTS(SELECT * FROM `voter` WHERE `userid` = "'+msg.author.id+'" OR `user`="'+sender+'")', function (error, results, fields) {
            for(i in results){
                for(j in results[i]){
                    x = results[i][j];
                    if(x == 1){
                        setTimeout(function(){bot.createMessage(channel,'Already Registered!');},1000);
                    }else if(x == 0){
                        var xmlhttp = new XMLHttpRequest();
                        xmlhttp.onreadystatechange = function() {
                            if (this.readyState == 4 && this.status == 200) {
                                var response = JSON.parse(this.responseText);
                                for(i in response){
                                    if(response[i].transaction == transaction && response[i].memo == memo){
                                        con.query('INSERT INTO `voter`(`user`, `lastvote`, `userid`) VALUES ("'+sender+'","0","'+msg.author.id+'")', function (error, results, fields) {
                                            r = 1;
                                            setTimeout(function(){bot.createMessage(channel,'User '+sender+' Registered by <@'+msg.author.id+'>');},1000);
                                        });
                                        break;
                                    }
                                }
                                setTimeout(function(){if(r == 0){
                                    setTimeout(function(){bot.createMessage(channel,'Please Send 0.001 SBD to '+votey+' with memo:`'+memo+'` and Try again.');},1000);
                                }},2000);
                            }
                        };
                        xmlhttp.open("GET", "https://steemfollower.com/voter-tx.php?user=" + votey , true);
                        xmlhttp.send(); 
                    }
                }
            }
        });
    }
});
bot.connect(); 

Test And Let me know If you find Any Errors.
You can find me in Discord and steemit.chat

Also,

Don't Forget @inertia's great tool: cosgrove
ask your questions from @inertia about this tool.


Only First Image Source: pixabay.com
and Also, Checkout Our https://steemauto.com and https://steemfollower.com

Regards,
2017-11-08


Open Source Contribution posted via Utopian.io

Sort:  

this is the info i was looking for thanks indeed.
upvoted 6 upvotes from team @steemit-arabia -- #promo-steem

You have collected your daily Power Up! This post received an upvote worth of 0.84$.
Learn how to Power Up Smart here!

Hey @mahdiyari I am @utopian-io. I have just super-voted you at 28% Power!

Suggestions https://utopian.io/rules

-Utopian has detected 2 bot votes. I am the only bot you should love!!

Achievements

-I am a bot...I love developers... <3
-Good amount of information. Thank you!
-Much more informative than others in this category. Good job!
-Votes on this contribution are going well. Nice!
-You are having more votes than average for this category. Nice!
-This is your first accepted contribution here in Utopian. Welcome!
-You have just unlocked 6 achievements. Yeah!
Up-vote this comment to grow my power and help Open Source contributions like this one.

Accepted in Utopian @mahdiyari. Thanks

[utopian-moderator]

Hi @mahdiyari. I just noticed you are missing line 1 in your Full code:

const Eris = require("eris");

amazing tutorial
bookmarked

Thanks Again for another Great Guide!

Hey @mahdiyari thanks for this contribution. As per the Utopian rules there should be no banners at the bottom of your posts and especially sentences like "upvote, resteem, comment" since they do not make sense in the Utopian website. For example we have no resteem or witness. In order to accept this contribution please remove any unrelated info or banner from your post. You can do this here https://utopian.io/utopian-io/@mahdiyari/update-voter-js-v0-03-discord-auto-upvote-nodejs-bot-how-to-upvote-only-posts-older-than-20-minutes by clicking on Edit post. Let me know once done and I will accept this post in the Utopian feed.

edit screen

Sorry.
Edits done.
and Thanks:)

Coin Marketplace

STEEM 0.17
TRX 0.15
JST 0.028
BTC 60385.82
ETH 2321.90
USDT 1.00
SBD 2.51