diff --git a/.gitignore b/.gitignore index d4813f0..9aa9b4b 100644 --- a/.gitignore +++ b/.gitignore @@ -1,5 +1,6 @@ node_modules .env .eslintrc.js +.eslintrc.json dsabot.db diff --git a/package-lock.json b/package-lock.json index 720f2db..3b588e8 100644 --- a/package-lock.json +++ b/package-lock.json @@ -11,7 +11,8 @@ "dependencies": { "discord.js": "^12.0.2", "dotenv": "^8.2.0", - "nedb": "^1.8.0" + "nedb": "^1.8.0", + "node-fetch": "^2.6.1" }, "devDependencies": { "jest": "^26.6.3" diff --git a/package.json b/package.json index 2f5116d..d89f1e3 100644 --- a/package.json +++ b/package.json @@ -12,7 +12,8 @@ "dependencies": { "discord.js": "^12.0.2", "dotenv": "^8.2.0", - "nedb": "^1.8.0" + "nedb": "^1.8.0", + "node-fetch": "^2.6.1" }, "devDependencies": { "jest": "^26.6.3" diff --git a/src/commands/createFromFile.js b/src/commands/createFromFile.js new file mode 100644 index 0000000..3ded293 --- /dev/null +++ b/src/commands/createFromFile.js @@ -0,0 +1,37 @@ +module.exports = async (message, data, db) => { + try { + db.find({ + user: message.author.tag + }, function (err, docs) { + // docs is an array containing documents Mars, Earth, Jupiter If no document is + // found, docs is equal to [] + if(!docs.length>0) + db.insert({user: message.author.tag, + gold: 0, + silver: 0, + bronce: 0, + iron: 0, + hp: 0, + character: data + + }) + + console.log(docs) + }); + } catch (e) { + throw e + } +} + + + // 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`, `LP`) VALUES (' + '"' + message.author.tag + '"' + ', 0, 0, 0, 0, 0)'); + message.reply('Dein Eintrag wurde registriert.'); + } else if(row.length >= 1) { + message.reply('Dein Eintrag existiert bereits.'); + } + }); +};*/ \ No newline at end of file diff --git a/src/commands/index.js b/src/commands/index.js index 2bee7db..02467d2 100644 --- a/src/commands/index.js +++ b/src/commands/index.js @@ -1,11 +1,16 @@ +const fs = require('fs'); +const fetch = require('node-fetch') const roll = require('./roll'); const create = require('./create'); const add = require('./add'); const remove = require('./remove'); const show = require('./show'); const talent = require('./talent') +const skill = require('./skill') +const createFromFile = require('./createFromFile') require('dotenv').config(); -const prefix = '!'; + +const prefix = process.env.PREFIX || '!'; const commands = { roll, @@ -13,7 +18,8 @@ const commands = { add, remove, show, - talent + skill, + talent, }; var Datastore = require('nedb'), db = new Datastore({ @@ -22,13 +28,33 @@ var Datastore = require('nedb'), }); module.exports = async (message) => { - 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); + if ((message.attachments.length > 0) && message.channel.type == 'dm') { + try { + let response = await fetch(message.attachments.first().url) + let data = await validateJSON(response); + if (data) createFromFile(message, data, db); + } catch (e) { + return null + } + } else { + + 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); + } } +}; -}; \ No newline at end of file +function validateJSON(body) { + try { + var data = body.json(); + // if came to here, then valid + return data; + } catch (e) { + // failed to parse + return null; + } +} \ No newline at end of file diff --git a/src/commands/skill.js b/src/commands/skill.js new file mode 100644 index 0000000..c23e335 --- /dev/null +++ b/src/commands/skill.js @@ -0,0 +1,24 @@ +// eslint-disable-next-line no-unused-vars +module.exports = async (message, args, db) => { + // eslint-disable-next-line no-undef + try { + //console.log(message) + db.find({ + user: message.author.tag + }, function (err, docs) { + // docs is an array containing documents Mars, Earth, Jupiter If no document is + // found, docs is equal to [] + if (!docs.length > 0) + message.reply('Sorry, Für dich habe ich keinen Eintrag 😥') + else { + let level = 0 + for(i in docs[0].character.skills) { + if(docs[0].character.skills[i].id == args[0]) level = docs[0].character.skills[i].level + } + message.reply('Du hast Folgenden Skill in ' + args[0] + ': ' + level) + } + }); + } catch (e) { + throw e + } +}; \ No newline at end of file