What Will I Learn?
You will learn how to make your discord bot run a record function by collecting and verifying and storing the data of a steemit user so that it can be used in a bot type, or bot that shows you your voting power, your data, etc.
Requirements
- a Heroku Account.
- a Discord Account.
- a Steemit Account.
- a GitHub Account.
- a Online remote MySQL Database.
Difficulty
- Intermediate
Tutorial Contents
If you do not know how to publish the bot in heroku, access this link that has the first part of my turorial: How to Host a Discord Bot on Heroku 24/7.
If you are going to start this part, you are supposed to know how to upload a heroku script to work 24/7.
Setup the Database
Configuring the database is very simple, it is not nothing of the other world, but in this case you need an online database and this is where the thing is difficult, since most of the online databases and that can be accessed remotely they are pay; but there is no need to be alarmed, I found a page that offers free database, up to 200mb of storage, which is with which I will provide the tutorial
First we have to create a new database , You can use this link to access the website
We configure our new database, name, user, password, mail (Note that DB4free only accepts a database by mail)
After we set up our database we will receive an email saying that to continue with the registration click on the link and the registration and creation of our database will be complete
Creating the Script
For this tutorial we are just going to need these scripts, which we are going to place in a GitHub repository, or you can simply download the repository from my account
These are the scripts you must create
bot.js
const Eris = require("eris");
const steem = require("steem");
var mysql = require('mysql');
var con = mysql.createConnection({
host: "db4free.net",// the db used
user: "YOUR USER",
password: "YOUR PASS",
database: "YOUR DB NAME"
});
con.connect();
var bot = new Eris("THE BOT TOKEN", { // We create a new instance of our bot (usually named client)
disableEveryone: true, // Makes it programmatically impossible for the bot to mention @everyone
getAllUsers: true // It fetches all users, Good for small bots. (recommend)
});
bot.on("messageCreate", (msg) => {
var regex2 = /(\$)+(register)+(\ )/;
if(msg.content.match(regex2)){
var usuario = msg.content.replace(msg.content.match(regex2)[0],"");
var channel = msg.channel.id;
var uid = msg.author.id;
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){
bot.createMessage(channel,"<@!" + uid + ">"+ ' Already Registered! || ya estas registrado');
}
else{
con.query('INSERT INTO `voter`(`usuario`,`userid`, `wifkey`) VALUES ("'+usuario+'","'+msg.author.id+'","0")', function (error, results, fields) {
msg.author.getDMChannel().then(channel => channel.createMessage('to register '+usuariox +' please execute in this chanel (direct message) $privatekey yourprivatekey || para registrar '+ usuariox +', ejecute en este canal (mensaje directo) $privatekey privatekey para saber donde esta su privatekey , vea el video '));
msg.author.getDMChannel().then(channel => channel.createMessage('if your user '+usuario +' was incorrect, please execute $change yournewuser in the channnel #register || si escribiste mas el nombre de usuario, o tu usuario en incorrecto, por favor ejecuta en el canal #register $change, si no logra solucionar nada, comuniquese al canal help'));
bot.createMessage(channel,"<@!" + uid + ">" + 'the bot send you a message');
});
}
}
}
});
}
var regexd = /(\$)+(change)+(\ )/;
if(msg.content.match(regexd)){
var usuariox = msg.content.replace(msg.content.match(regexd)[0],"");
var channel = msg.channel.id;
var uid = msg.author.id;
con.query('SELECT EXISTS(SELECT * FROM `voter` WHERE `userid`="'+uid+'")', function (error, results, fields) {
console.log("funcioncambiarusuario");
for(i in results){
for(j in results[i]){
x = results[i][j];
if(x == 1){
con.query('UPDATE `voter` SET `usuario`="'+ usuariox +'", `lastvote`= "0" WHERE `userid`="'+uid+'"' , function (error, results, fields) {
msg.author.getDMChannel().then(channel => channel.createMessage('to register '+usuariox +' please execute in this chanel (direct message) $privatekey yourprivatekey || para registrar '+ usuariox +', ejecute en este canal (mensaje directo) $privatekey privatekey para saber donde esta su privatekey , vea el video '));
});
}
else{
bot.createMessage(channel,"<@!" + uid + ">" + 'there is no user to change, first register || no hay ningun usuario que cambiar, primero registrate ');
}
}
}
});
}
var regex3 = /(\$)+(privatekey)+(\ )/;
if(msg.content.match(regex3)){
var privWif = msg.content.replace(msg.content.match(regex3)[0],"");
var channel = msg.channel.id;
var uid = msg.author.id;
var user;
con.query('SELECT `usuario` FROM `voter` WHERE `userid`="'+uid+'"', function (error, results, fields) {
for(i in results){
for(j in results[i]){
name = results[i][j];
con.query('SELECT count(user) AS total FROM voter', function(err, result) {
console.log("funcionandouserid");
user = result[0].total;
steem.api.getAccounts([name], function(err, result) {
var pubWif = result[0].posting.key_auths[0][0];
var isvalid;
try{ isvalid = steem.auth.wifIsValid(privWif, pubWif); }
catch(e){ isvalid = 'false'; }
if(isvalid == true){
con.query('UPDATE `voter` SET `wifkey`="'+privWif+'", `usuario`="'+ name+'" WHERE `userid`="'+uid+'" OR `user`="'+ user+'"' , function (error, results, fields) {
bot.createMessage(channel,"<@!" + uid + ">" + ' the registration process was successful');
});
console.log(name+' Welcome.');
}else{
console.log('Wrong! Check your Private key.');
bot.createMessage(channel,"<@!" + uid + ">"+ ' the password you provided from the user is incorrect, please try again remember that it is the PRIVATEKEY that the bot needs, if you need help, check the help channel || la contraseña que proporcionaste del usuario es incorrecto, por favor vuelve a intentarlo recuerda que es la PRIVATEKEY la que necesita el bot, si deaseas ayuda, consulta el canal de ayuda');
}
});
});
}
}
});
}
});
bot.connect();
configure the database
server.js
var mysql = require('mysql');
var con = mysql.createConnection({
host: "db4free.net",// the database used
user: "YOUR USER",
password: "YOUR PASS",
database: "YOUR DB NAME"
});
con.connect(function(err) {
if (err) throw err;
console.log("Connected!");
var sql = "CREATE TABLE voter (id INT NOT NULL AUTO_INCREMENT , PRIMARY KEY (id), user text NOT NULL, userid text NOT NULL, usuario text NOT NULL, wifkey text NOT NULL)";
con.query(sql, function (err, result) {
if (err) throw err;
console.log("Table created");
});
});
package.json
{
"name": "Navi",
"main": "bot.js",
"dependencies": {
"express": "4.13.3",
"discord.js": "11.1.0",
"discord.js-commando": "^0.9.0",
"eris": "^0.8.4",
"mysql": "^2.15.0",
"steem": "^0.7.1",
"pg": "^6.1.4"
},
"version": "1.0.0",
"scripts": {
"start": "bot.js",
"start": "server.js"
}
}
Procfile
bot: node bot.js
Db: node server.js
Remember that all these files are uploaded to heroku through the connection heroku github and you have to select in desploy both the bot and the db, after this the bot would be working perfectly, here a picture!
Curriculum
Posted on Utopian.io - Rewarding Open Source Contributors
Your contribution cannot be approved because it is not as informative as other contributions. See the Utopian Rules. Contributions need to be informative and descriptive in order to help readers and developers understand them.
Submissions that include a GitHub repository with additional materials (like code samples), should be linked to the repository of the original project discussed in the tutorial and not the supplementary repository created for the contribution. Links to the supplementary repository for the post should be included in the submission post.
Submissions containing unexplained essential steps, codes or examples will be rejected.
tutorials are what talking about how to achieve the function but not just listing what you have done . more detail is needed
You can contact us on Discord.
[utopian-moderator]
Downvoting a post can decrease pending rewards and make it less visible. Common reasons:
Submit