added JSON parsing data

This commit is contained in:
Marcus Netz
2020-11-22 12:37:30 +01:00
parent 07d5f2fdef
commit 210062b185
6 changed files with 101 additions and 11 deletions

1
.gitignore vendored
View File

@ -1,5 +1,6 @@
node_modules
.env
.eslintrc.js
.eslintrc.json
dsabot.db

3
package-lock.json generated
View File

@ -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"

View File

@ -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"

View File

@ -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.');
}
});
};*/

View File

@ -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);
}
}
};
};
function validateJSON(body) {
try {
var data = body.json();
// if came to here, then valid
return data;
} catch (e) {
// failed to parse
return null;
}
}

24
src/commands/skill.js Normal file
View File

@ -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
}
};