Complete Guide to Creating Discord Auto Upvote Bot by Javascript.

android-150996_640.png

It is a complete guide for creating an auto voter bot like banjo or minnowsupport or like 'Steemfollower-voter' bot in this discord server
For running a discord bot, you need a VPS or a always online system.
minimum requirment system is 200~500 mb RAM.

How it works?

this bot can work with a spicific commands like $upvote or without any command in spicific channels.
we will user Eris Discord library to connecting our bot. you can find this library here on github.

After upvoting a post, we will store that Discord User in Mysql Database with upvote time. we will need this time for checking next upvotes time.

Our bot will detect commands and post links, then it will check last upvote time for that user. if it was greater that 12 hours (you can change this) then post will get an upvote from spicific account. or User will receive an error message.

First Step: Installing Mysql

this guide is for ubuntu 16.04
if you are using another IOS or another version, Search in Google.com

Start by entering these commands:
sudo apt-get update
sudo apt-get install mysql-server

You'll be prompted to create a root password during the installation. Choose a secure one and make sure you remember it, because you'll need it later. Next, we'll finish configuring MySQL.

Run the security script.
sudo mysql_secure_installation

This will prompt you for the root password you created in recent step. You can press Y and then ENTER to accept the defaults for all the subsequent questions, with the exception of the one that asks if you'd like to change the root password. You just set it in Step 1, so you don't have to change it now.

Finally, let's test the MySQL installation.
systemctl status mysql.service


Creating Database:

Once you have MySQL installed on your droplet, you can access the MySQL shell by typing the following command into terminal:
mysql -u root -p
Enter your mysql password and press 'Enter'.

Now we need to create a new database:
CREATE DATABASE database voter;
it will create 'voter' database. Don't forget ; at the end of codes.

Let’s open up the database we want to use:
USE voter;

Now create a new Table:
CREATE TABLE voter (
id int(11) NOT NULL,
user text NOT NULL,
lastvote text NOT NULL,
userid text NOT NULL);

or If you get any error, Try this one:
CREATE TABLE `voter` (
`id` int(11) NOT NULL,
`user` text NOT NULL,
`lastvote` text NOT NULL,
`userid` text NOT NULL);


Creating a Discord Bot:

Open: https://discordapp.com/developers/applications/me
and Create New Application.

Follow this instruction and Create a new application and a new bot and add that bot to your server.
We will use bot token in our .js file.

Installing Required Libraries

Run These codes one by one:
sudo apt-get install nodejs
sudo apt-get install npm
npm install steem --save
npm install --no-optional eris
npm install mysql
npm install pm2


Creating .js File

Create a .js file in your system and add this codes.
you can do it by:
nano voter.js

we need to add some library to our file:
const Eris = require("eris");
const steem = require("steem");
var mysql = require('mysql');

Mysql Connection Details:
var con = mysql.createConnection({
host: "127.0.0.1",
user: "root",
password: "mysql",
database: "voter"
});

And connecting to Mysql:
con.connect();

Enter your Voter account name and private Posting key. and add voting period.

var wifkey = 'myprivatepostingkey';
var votey = "mahdiyari";
var per = 86400;

86400 seconds = 24 hour.

Add Voting weight. (10000 = 100% and 100 = 1%)
var weight = 2000;

Add your Bot Token here:
var bot = new Eris("Your Bot Token");

We need to know when our bot is connected and is ready:
bot.on("ready", () => {console.log('voter bot started! weight is '+weight/100+'%);});

now we must detect a spicific command and posts permlinks and Author:
var regex = /(\$)+(upvote)+.+(https:\/\/)+.+(@)+.+(\/)/;
var regex1 = /(@)+.+(\/)/;

regex will check if message includes '$upvote @-/' or not.
and regex1 will export post Author.

Full Code:

const Eris = require("eris");
const steem = require("steem");
var mysql = require('mysql');
var con = mysql.createConnection({
  host: "127.0.0.1",
  user: "root",
  password: "mysql",
  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 = 2000; // 10000 = 100%
var per = 86400; // 86400 seconds = 24hour 
bot.on("ready", () => {console.log('voter bot started! weight '+weight+' %');}); //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;
                        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) > per){
                                    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 = per - (time - last);
                                    setTimeout(function(){bot.createMessage(channel,'Sorry! Come back after '+come+' seconds.');},1000);
                                }
                        });
                        
                    }else{
                        var time = Math.floor(new Date().getTime() / 1000);
                        con.query('INSERT INTO `voter`(`user`, `lastvote`, `userid`) VALUES ("'+author+'","'+time+'","'+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`="10" WHERE `userid`="'+uid+'"', function (error, results, fields) {
                                    });
                                }
                                if(result) {
                                    setTimeout(function(){bot.createMessage(channel,'Done! Your Post Upvoted By @mahdiyari');},1000);
                                }
                            });
                        });
                    }
                }
            }
        });

    }
});
bot.connect(); 

Running Bot:

You must Add account username, posting private key, voting weight and Bot Token before running this file.
for running your bot, enter:
pm2 start voter.js

if you edited your file anytime, you must restart it by:
pm2 restart voter.js

and for stopping:
pm2 stop voter

'voter' is my .js file name. change it if your file is different.

You can add a channel filter fo your bot, so your bot will work only in spicific channels.
Also, in this case, my Mysql password for root user was 'mysql'. so change it in above codes.

Ask your questions in comments or join Steemdevs discord server.


if you think I can be helpful for steem community, please vote me as a witness. Thanks.

1- open https://steemit.com/~witnesses
2- scroll down.
3- type mahdiyari and once click on the vote button.


upvote-follow1.gif

first image source: pixabay.com

Regards,
2017-09-06

H2
H3
H4
3 columns
2 columns
1 column
39 Comments