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

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