added JSON parsing data
This commit is contained in:
@ -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;
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user