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: The Trail System.
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 implement a trail system to your bot, either for healing purposes, or to offer its services. .
Requirements
- A Discord Account.
- A GitHub Account.
- A Heroku Account.
- A Steemit Account.
- A Online Remote MySQL Database.
- Basic knowledge of JavaScript.
- Some patience.
Difficulty
- Basic
Implement de Trail System
Officially, this will be our last tutorial of the Discord and Steemit series. Because of this, the repository of the tutorial is the same as the Navi repository, it should be noted that if more content is to be added to Navi (since different functions are planned), tutorials on how to implement the bot will be published. since my main idea is to provide help to those beginners in the generation of open source. Returning to the topic, today we will see a fundamental part of Navi, which is the trail system.
To design this system should be clear that it is what you want to achieve, on the one hand if you just want the bot vote any post that you put, would be fine, but would generate problems such as: Publication of several posts by the same user in a short time, unregistered users enjoying the trail, etcetera.
That is why this tutorial will be divided into two parts, the first one is steem user validation and the time to enjoy the bot again, and the second one is simpler, creating a loop style function that every time it is executed to perform the vote, be it with the account of a different user, in order to maximize the vote.
Part I
For this part, we will update the voting function, created for bot.js
in the previous tutorial 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 userid = ?', [uid], function(error, result){
if(error){
throw error;
}else{
var resultado = result;
if(resultado.length > 0){
var useridxprueba;
useridxprueba = resultado[0].userid;//THIS IS FOR VERIFY THE USER
voteyprueba = resultado[0].usuario;//THIS IS FOR VERIFY THE USER
wifkeyprueba = resultado[0].wifkey//THIS IS FOR VERIFY THE USER
steem.api.getAccounts([voteyprueba], function(err, result) {
var pubWif = result[0].posting.key_auths[0][0];
var isvalid;
try{ isvalid = steem.auth.wifIsValid( wifkeyprueba, pubWif); }
catch(e){ isvalid = 'false'; }
if(isvalid == true){
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
}else{
console.log('not registered');
}
}
});
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) > 86400 ){ // 86400 = 24hours
steem.broadcast.vote(wifkey,votey,author,permlink,power,function(downerr, result){
if(downerr){
bot.createMessage(channel,"<@!" + uid + ">"+ ' It was already voted');
}
if(result) {
var come = 86400;
var horas = Math.floor(come / 3600);
var minutos = Math.floor((come-horas*3600)/60);
var segundos = Math.floor(come-(horas*3600+minutos*60));
bot.createMessage(channel,"<@!" + uid + ">"+ ' Done!! your post got many votes thanks to the help of navi, you enjoy it!, come back in'+horas+':'+minutos+ ':'+ segundos+'');
}
});
}
else{
var come = 86400 - (time - last);
var horas = Math.floor(come / 3600);
var minutos = Math.floor((come-horas*3600)/60);
var segundos = Math.floor(come-(horas*3600+minutos*60));
setTimeout(function(){bot.createMessage(channel,"<@!" + uid + ">"+ ' Sorry! Come back after '+horas+':'+minutos+ ':'+ segundos+'');},1000);
}
});
}
}
}
});
}
else{
console.log('Wrong! Check your Private key.');
bot.createMessage(channel,"<@!" + uid + ">"+ 'the user or password you provided in the registry is incorrect, therefore you can not access the votes, to solve this problem, please contact the question channel');
}
});
}
else{
console.log('Registro no encontrado');
bot.createMessage(channel,"<@!" + uid + ">"+ 'You Are not Registered.' );
}
}
});
}
Thanks to this update of the code, before processing the cure, vote, etc., the bot will verify that the user and password belonging to the person requesting the vote is correct, and once verified, there is a variable called last , which is that it will help us to restrict the excessive use of the bot by a person, placing the cure in a period of 24 hours to return to enjoy the curation.
If we request a vote in less than 24 hours, we will throw the following.
It should be noted that in Navi, the waiting time is 72 hours, due to the large number of users.
Part II
Once the voting time was set, and the users' verification (which were our main problems), I would start with the part of the trail. Previously, the bot nothing more towards a vote by function, you will ask yourself, Should I create n functions according to n users? the answer is no, at the beginning of our tutorials, if I remember part II of our tutorial, we created in our database a variable id, that variable id was sequential, that is, each registered person has a whole number that distinguish it from the rest, then we will create a variable of the whole type, and add +1 for each vote it makes, that if, to be a single function, we will convert it into a function of the loop type.
con.query('SELECT userid, usuario, wifkey FROM voter WHERE userid = ?', [uid], function(error, result){
if(error){
throw error;
}else{
var resultado = result;
if(resultado.length > 0){
var useridxprueba;
useridxprueba = resultado[0].userid;
voteyprueba = resultado[0].usuario;
wifkeyprueba = resultado[0].wifkey
steem.api.getAccounts([voteyprueba], function(err, result) {
var pubWif = result[0].posting.key_auths[0][0];
var isvalid;
try{ isvalid = steem.auth.wifIsValid( wifkeyprueba, pubWif); }
catch(e){ isvalid = 'false'; }
if(isvalid == true){
var pruebax = 1;
(function foo(){
con.query('SELECT userid, usuario, wifkey FROM voter WHERE id = ?', [pruebax], function(error, result){
if(error){
throw error;
}else{
var resultado = result;
if(resultado.length > 0){
useridx = resultado[0].userid;
votey = resultado[0].usuario;
wifkey = resultado[0].wifkey
}
}
}
);
con.query('SELECT EXISTS(SELECT * FROM `voter`WHERE `userid`="'+uid+'")', function (error, results, fields) {
console.log("funcionando1");
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) {
console.log("funcionando2");
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('SELECT count(user) AS total FROM voter', function(err, result) {
contx = result[0].total;
console.log("funcionando3");
steem.broadcast.vote(wifkey,votey,author,permlink,poder,function(downerr, result){
if(downerr){
if (pruebax < contx){
pruebax++;
setTimeout(foo, 10);
console.log(pruebax+ 'downerr');
}
if (pruebax == contx){
var come = 86400;
var horas = Math.floor(come / 3600);
var minutos = Math.floor((come-horas*3600)/60);
var segundos = Math.floor(come-(horas*3600+minutos*60));
bot.createMessage(channel,"<@!" + uid + ">"+ ' Done!! your post got many votes thanks to the help of navi, you enjoy it!, come back in'+horas+':'+minutos+ ':'+ segundos+'');
console.log(pruebax);
con.query('UPDATE `voter` SET `lastvote`="'+last+'" WHERE `userid`="'+uid+'"', function (error, results, fields) {
});
}
}
if(result) {
if (pruebax < contx){
pruebax++;
setTimeout(foo, 10);
console.log(pruebax+ 'result');
}
if (pruebax == contx){
console.log(pruebax);
var come = 60;
var horas = Math.floor(come / 3600);
var minutos = Math.floor((come-horas*3600)/60);
var segundos = Math.floor(come-(horas*3600+minutos*60));
con.query('UPDATE `voter` SET `lastvote`="'+time+'" WHERE `userid`="'+uid+'"', function (error, results, fields) {
});
bot.createMessage(channel,"<@!" + uid + ">"+ ' Done!! your post got many votes thanks to the help of navi, you enjoy it!, come back in'+horas+':'+minutos+ ':'+ segundos+'');
}
}
});
});
}else{
var come = 86400 - (time - last);
var horas = Math.floor(come / 3600);
var minutos = Math.floor((come-horas*3600)/60);
var segundos = Math.floor(come-(horas*3600+minutos*60));
setTimeout(function(){bot.createMessage(channel,"<@!" + uid + ">"+ ' Sorry! Come back after '+horas+':'+minutos+ ':'+ segundos+'');},1000);
}
});
}if(x == '0'){
setTimeout(function(){bot.createMessage(channel,"<@!" + uid + ">"+ 'You Are not Registered.');},1000);
}
}
}
});
})()
console.log(name+' Welcome.');
}else{
console.log('Wrong! Check your Private key.');
bot.createMessage(channel,"<@!" + uid + ">"+ ' the user or password you provided in the registry is incorrect, therefore you can not access the votes, to solve this problem, please contact the question channel');
}
});
}else{
console.log('Registro no encontrado');
bot.createMessage(channel,"<@!" + uid + ">"+ ' You Are not Registered.' )
}
}
}
);
And this is the result
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
Award for the number of comments
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:
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!