From 6e0a5c704a7868730dd8c4ffc7317d7deceeefa4 Mon Sep 17 00:00:00 2001 From: Luca Schwan Date: Wed, 25 Mar 2020 12:27:00 +0100 Subject: [PATCH] command handler updated --- .sample.env | 1 + src/commands/add.js | 31 ++++++++++ src/commands/create.js | 12 ++++ src/commands/index.js | 59 +++++++++++++++++++ src/commands/remove.js | 37 ++++++++++++ src/commands/roll.js | 17 ++++++ src/commands/show.js | 8 +++ src/index.js | 131 +---------------------------------------- 8 files changed, 168 insertions(+), 128 deletions(-) create mode 100644 .sample.env create mode 100644 src/commands/add.js create mode 100644 src/commands/create.js create mode 100644 src/commands/index.js create mode 100644 src/commands/remove.js create mode 100644 src/commands/roll.js create mode 100644 src/commands/show.js diff --git a/.sample.env b/.sample.env new file mode 100644 index 0000000..365335d --- /dev/null +++ b/.sample.env @@ -0,0 +1 @@ +BOT_TOKEN = #YOUR_BOT_TOKEN_HERE \ No newline at end of file diff --git a/src/commands/add.js b/src/commands/add.js new file mode 100644 index 0000000..3c8c0d4 --- /dev/null +++ b/src/commands/add.js @@ -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.'); + }); + } + }); + } +}; \ No newline at end of file diff --git a/src/commands/create.js b/src/commands/create.js new file mode 100644 index 0000000..078faa3 --- /dev/null +++ b/src/commands/create.js @@ -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.'); + } + }); +}; \ No newline at end of file diff --git a/src/commands/index.js b/src/commands/index.js new file mode 100644 index 0000000..4a4b907 --- /dev/null +++ b/src/commands/index.js @@ -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); + */ +}; \ No newline at end of file diff --git a/src/commands/remove.js b/src/commands/remove.js new file mode 100644 index 0000000..1cd332c --- /dev/null +++ b/src/commands/remove.js @@ -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] ); + } + } + }); + } +}; \ No newline at end of file diff --git a/src/commands/roll.js b/src/commands/roll.js new file mode 100644 index 0000000..db7feff --- /dev/null +++ b/src/commands/roll.js @@ -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.'); + } +}; \ No newline at end of file diff --git a/src/commands/show.js b/src/commands/show.js new file mode 100644 index 0000000..bd2cacf --- /dev/null +++ b/src/commands/show.js @@ -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.' ); + } + ); +}; \ No newline at end of file diff --git a/src/index.js b/src/index.js index d8ee6d4..50721b0 100644 --- a/src/index.js +++ b/src/index.js @@ -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);