command handler updated

This commit is contained in:
Luca Schwan
2020-03-25 12:27:00 +01:00
parent 54d9cc9633
commit 6e0a5c704a
8 changed files with 168 additions and 128 deletions

1
.sample.env Normal file
View File

@ -0,0 +1 @@
BOT_TOKEN = #YOUR_BOT_TOKEN_HERE

31
src/commands/add.js Normal file
View File

@ -0,0 +1,31 @@
module.exports = async (message, args, db) => {
var n;
if(!isNaN(args[0]) && (args[1] === 'GD' || args[1] === 'ST' || args[1] === 'BH' || args[1] === 'EK')) {
// eslint-disable-next-line no-undef
db.query('SELECT * FROM DSAGeld WHERE userName = ' + '"' + message.author.tag + '"', function(err, row) { //the row is the user's data
if(row && err) {
message.reply('Es gab einen Fehler.');
}
if(row.length < 1) { //if the user is not in the database
message.reply('Es existiert kein Eintrag für dich füge ihn mit !create hinzu.');
} else { //if the user is in the database
if(args[1] === 'GD') {
n = parseInt(row[0].GD, 10) + parseInt(args[0], 10);
} else if(args[1] === 'ST') {
n = parseInt(row[0].ST, 10) + parseInt(args[0], 10);
} else if(args[1] === 'BH') {
n = parseInt(row[0].BH, 10) + parseInt(args[0], 10);
} else if(args[1] === 'EK') {
n = parseInt(row[0].EK, 10) + parseInt(args[0], 10);
}
// eslint-disable-next-line no-undef
db.query('UPDATE DSAGeld SET' + '`' + args[1] + '`' + ' = (' + n + ') WHERE userName = ' + '"' + message.author.tag + '"');
// eslint-disable-next-line no-undef
db.query('SELECT * FROM DSAGeld WHERE userName = ' + '"' + message.author.tag + '"', function(err, row) { //the row is the user's data
message.reply(args[0] + args[1] + ' hinzugefügt, du hast: ' + row[0].GD + 'GD, ' + row[0].ST + 'ST, ' + row[0].BH + 'BH, ' + row[0].EK + 'EK.');
});
}
});
}
};

12
src/commands/create.js Normal file
View File

@ -0,0 +1,12 @@
module.exports = async (message, args, db) => {
// eslint-disable-next-line no-undef
db.query('SELECT * FROM DSAGeld WHERE userName = ' + '"' + message.author.tag + '"', function(err, row) { //the row is the user's data
if(row.length < 1) {
// eslint-disable-next-line no-undef
db.query('INSERT INTO `DSAGeld` (`userName`, `GD`,`ST`, `BH`, `EK`) VALUES (' + '"' + message.author.tag + '"' + ', 0, 0, 0, 0)');
message.reply('Dein Eintrag wurde registriert.');
} else if(row) {
message.reply('Dein Eintrag existiert bereits.');
}
});
};

59
src/commands/index.js Normal file
View File

@ -0,0 +1,59 @@
const roll = require('./roll');
const create = require('./create');
const add = require('./add');
const remove = require('./remove');
const show = require('./show');
const mysql = require('mysql');
const prefix = '!';
const commands = {
roll,
create,
add,
remove,
show
};
var db = mysql.createConnection({
host : 'remotemysql.com',
port : '3306',
user : 'tftipudgeE',
password : 'GYKju7YA1p',
database : 'tftipudgeE'
});
db.connect((err) => {
if(err){
throw err;
}
console.log('MySql connected...');
});
module.exports = async (message) =>{
//command manager
if (!message.content.startsWith(prefix) || message.author.bot) return;
const args = message.content.slice(prefix.length).split(' ');
const command = args.shift().toLowerCase();
if(Object.keys(commands).includes(command)) {
commands[command](message, args, db);
}
/*//dice:
if(command === 'roll') roll(message, args);
//create money 'account'
if(command == 'create') create(message, args);
//add money
if(command === 'add') add(message, ags);
//remove money
if(command === 'remove') remove(message, args);
//show money
if(command === 'show') show(message, args);
*/
};

37
src/commands/remove.js Normal file
View File

@ -0,0 +1,37 @@
module.exports = async (message, args, db) => {
var n;
if(!isNaN(args[0]) && (args[1] === 'GD' || args[1] === 'ST' || args[1] === 'BH' || args[1] === 'EK')) {
// eslint-disable-next-line no-undef
db.query('SELECT * FROM DSAGeld WHERE userName = ' + '"' + message.author.tag + '"', function(err, row) { //the row is the user's data
if(row && err) {
message.reply('Es gab einen Fehler.');
}
if(!row) { //if the user is not in the database
message.reply('Es existiert kein Eintrag für dich füge ihn mit !create hinzu.');
} else { //if the user is in the database
if(args[1] === 'GD') {
n = parseInt(row[0].GD, 10) - parseInt(args[0], 10);
} else if(args[1] === 'ST') {
n = parseInt(row[0].ST, 10) - parseInt(args[0], 10);
} else if(args[1] === 'BH') {
n = parseInt(row[0].BH, 10) - parseInt(args[0], 10);
} else if(args[1] === 'EK') {
n = parseInt(row[0].EK, 10) - parseInt(args[0], 10);
}
if(n >= 0) {
// eslint-disable-next-line no-undef
db.query('UPDATE DSAGeld SET' + '`' + args[1] + '`' + ' = (' + n + ') WHERE userName = ' + '"' + message.author.tag + '"');
// eslint-disable-next-line no-undef
db.query('SELECT * FROM DSAGeld WHERE userName = ' + '"' + message.author.tag + '"', function(err, row) { //the row is the user's data
message.reply(args[0] + args[1] + ' abgezogen, du hast: ' + row[0].GD + 'GD, ' + row[0].ST + 'ST, ' + row[0].BH + 'BH, ' + row[0].EK + 'EK.');
});
} else if(n < 0) {
message.reply('du hast nicht genügend ' + args[1] );
}
}
});
}
};

17
src/commands/roll.js Normal file
View File

@ -0,0 +1,17 @@
// eslint-disable-next-line no-unused-vars
module.exports = async (message, args, db) => {
if (!args.length == 3) {
message.reply('Du hast die Würfel nicht korrekt angegeben.');
}
else if(!isNaN(args[0]) && !isNaN(args[2]) && args[0] > 0 && args[2] > 0) {
var roll = [];
for (let i = 0; i < args[0]; i++) {
var a = Math.floor(Math.random() * args[2]) + 1;
roll.push(a);
}
message.reply('Deine Würfe(' + args[0] + 'W' + args[2] + '): ' + roll.join(', ') + '.');
} else {
message.reply('Du hast die Würfel nicht korrekt angegeben.');
}
};

8
src/commands/show.js Normal file
View File

@ -0,0 +1,8 @@
// eslint-disable-next-line no-unused-vars
module.exports = async (message, args, db) => {
// eslint-disable-next-line no-undef
db.query('SELECT * FROM DSAGeld WHERE userName = ' + '"' + message.author.tag + '"', function(err, row) { //the row is the user's data
message.reply('du hast: ' + row[0].GD + ' GD, ' + row[0].ST + ' ST, ' + row[0].BH + ' BH, ' + row[0].EK + ' EK.' );
}
);
};

View File

@ -1,134 +1,9 @@
require('dotenv').config();
const Discord = require('discord.js');
const bot = new Discord.Client();
const mysql = require('mysql');
const prefix = '!';
const commandHandler = require('./commands');
var db = mysql.createConnection({
host : 'remotemysql.com',
port : '3306',
user : 'tftipudgeE',
password : 'GYKju7YA1p',
database : 'tftipudgeE'
});
bot.on('message', commandHandler);
db.connect((err) => {
if(err){
throw err;
}
console.log('MySql connected...');
});
bot.on('message', (message) =>{
var n;
//command manager
if (!message.content.startsWith(prefix) || message.author.bot) return;
const args = message.content.slice(prefix.length).split(' ');
const command = args.shift().toLowerCase();
//dice:
if(command === 'roll') {
if (!args.length == 3) {
return message.reply('Du hast die Würfel nicht korrekt angegeben.');
}
else if(!isNaN(args[0]) && !isNaN(args[2]) && args[0] > 0 && args[2] > 0) {
var roll = [];
for (let i = 0; i < args[0]; i++) {
var a = Math.floor(Math.random() * args[2]) + 1;
roll.push(a);
}
message.reply('Deine Würfe(' + args[0] + 'W' + args[2] + '): ' + roll.join(', ') + '.');
} else {
return message.reply('Du hast die Würfel nicht korrekt angegeben.');
}
}
//create money 'account'
if(command == 'create'){
db.query('SELECT * FROM DSAGeld WHERE userName = ' + '"' + message.author.tag + '"', function(err, row) { //the row is the user's data
if(row.length < 1) {
db.query('INSERT INTO `DSAGeld` (`userName`, `GD`,`ST`, `BH`, `EK`) VALUES (' + '"' + message.author.tag + '"' + ', 0, 0, 0, 0)');
message.reply('Dein Eintrag wurde registriert.');
} else if(row) {
message.reply('Dein Eintrag existiert bereits.');
}
});
}
//add money
if(command === 'add') {
if(!isNaN(args[0]) && (args[1] === 'GD' || args[1] === 'ST' || args[1] === 'BH' || args[1] === 'EK')) {
db.query('SELECT * FROM DSAGeld WHERE userName = ' + '"' + message.author.tag + '"', function(err, row) { //the row is the user's data
if(row && err) {
message.reply('Es gab einen Fehler.');
}
if(row.length < 1) { //if the user is not in the database
message.reply('Es existiert kein Eintrag für dich füge ihn mit !create hinzu.');
} else { //if the user is in the database
if(args[1] === 'GD') {
n = parseInt(row[0].GD, 10) + parseInt(args[0], 10);
} else if(args[1] === 'ST') {
n = parseInt(row[0].ST, 10) + parseInt(args[0], 10);
} else if(args[1] === 'BH') {
n = parseInt(row[0].BH, 10) + parseInt(args[0], 10);
} else if(args[1] === 'EK') {
n = parseInt(row[0].EK, 10) + parseInt(args[0], 10);
}
db.query('UPDATE DSAGeld SET' + '`' + args[1] + '`' + ' = (' + n + ') WHERE userName = ' + '"' + message.author.tag + '"');
db.query('SELECT * FROM DSAGeld WHERE userName = ' + '"' + message.author.tag + '"', function(err, row) { //the row is the user's data
message.reply(args[0] + args[1] + ' hinzugefügt, du hast: ' + row[0].GD + 'GD, ' + row[0].ST + 'ST, ' + row[0].BH + 'BH, ' + row[0].EK + 'EK.');
});
}
});
}
}
//remove money
if(command === 'remove') {
if(!isNaN(args[0]) && (args[1] === 'GD' || args[1] === 'ST' || args[1] === 'BH' || args[1] === 'EK')) {
db.query('SELECT * FROM DSAGeld WHERE userName = ' + '"' + message.author.tag + '"', function(err, row) { //the row is the user's data
if(row && err) {
message.reply('Es gab einen Fehler.');
}
if(!row) { //if the user is not in the database
message.reply('Es existiert kein Eintrag für dich füge ihn mit !create hinzu.');
} else { //if the user is in the database
if(args[1] === 'GD') {
n = parseInt(row[0].GD, 10) - parseInt(args[0], 10);
} else if(args[1] === 'ST') {
n = parseInt(row[0].ST, 10) - parseInt(args[0], 10);
} else if(args[1] === 'BH') {
n = parseInt(row[0].BH, 10) - parseInt(args[0], 10);
} else if(args[1] === 'EK') {
n = parseInt(row[0].EK, 10) - parseInt(args[0], 10);
}
if(n >= 0) {
db.query('UPDATE DSAGeld SET' + '`' + args[1] + '`' + ' = (' + n + ') WHERE userName = ' + '"' + message.author.tag + '"');
db.query('SELECT * FROM DSAGeld WHERE userName = ' + '"' + message.author.tag + '"', function(err, row) { //the row is the user's data
message.reply(args[0] + args[1] + ' abgezogen, du hast: ' + row[0].GD + 'GD, ' + row[0].ST + 'ST, ' + row[0].BH + 'BH, ' + row[0].EK + 'EK.');
});
} else if(n < 0) {
message.reply('du hast nicht genügend ' + args[1] );
}
}
});
}
}
//show money
if(command === 'show') {
db.query('SELECT * FROM DSAGeld WHERE userName = ' + '"' + message.author.tag + '"', function(err, row) { //the row is the user's data
message.reply('du hast: ' + row[0].GD + ' GD, ' + row[0].ST + ' ST, ' + row[0].BH + ' BH, ' + row[0].EK + ' EK.' );
}
);}
});
bot.login('process.env.BOT_TOKEN');
bot.login(process.env.BOT_TOKEN);