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:
- Complete Guide to Creating Discord Auto Upvote Bot by Javascript.
- [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