Refactoring (#29)
* refactor Attack command * add test to Attack functions * changed README.md * install dev dependency rewire * stop autoloading due to ci errors
This commit is contained in:
129
index.js
129
index.js
@ -4,6 +4,7 @@ const fs = require('fs');
|
||||
const fetch = require('node-fetch');
|
||||
const globals = require('./globals');
|
||||
const db = globals.db;
|
||||
db.loadDatabase();
|
||||
const cmdprefix = process.env.CMDPREFIX || '!';
|
||||
const Discord = require('discord.js');
|
||||
const client = new Discord.Client();
|
||||
@ -11,75 +12,85 @@ client.commands = new Discord.Collection();
|
||||
client.on('message', commandHandler);
|
||||
client.login(process.env.BOT_TOKEN);
|
||||
client.once('ready', () => {
|
||||
console.log('Ready!');
|
||||
console.log('Ready!');
|
||||
});
|
||||
|
||||
|
||||
const commandFiles = fs.readdirSync('./commands/').filter(file => file.endsWith('.js'));
|
||||
for (const file of commandFiles) {
|
||||
const command = require(`./commands/${file}`);
|
||||
client.commands.set(command.name, command);
|
||||
const command = require(`./commands/${file}`);
|
||||
client.commands.set(command.name, command);
|
||||
}
|
||||
|
||||
async function commandHandler(message) {
|
||||
//console.log(`${new Date().toUTCString()} ${message.author.tag} (size: ${message.attachments.size})`);
|
||||
if ((message.attachments.size > 0) && message.channel.type == 'dm' && !message.author.bot) {
|
||||
try {
|
||||
const response = await fetch(message.attachments.first().url);
|
||||
const data = await validateJSON(response);
|
||||
if (data) await CreateFromFile(message, data);
|
||||
} catch (e) {
|
||||
console.log(e);
|
||||
return message.reply(globals.Replies.find(x => x.id === 'ERROR').string);
|
||||
}
|
||||
} else {
|
||||
if (!message.content.startsWith(cmdprefix) || message.author.bot) {
|
||||
return;
|
||||
}
|
||||
const args = message.content.slice(cmdprefix.length).split(' ');
|
||||
const commandName = args.shift().toLowerCase();
|
||||
//if (!client.commands.has(commandName)) return;
|
||||
try {
|
||||
const command = client.commands.get(commandName) || client.commands.find(cmd => cmd.aliases && cmd.aliases.includes(commandName));
|
||||
if (!command) return;
|
||||
if (command.needs_args && !args.length) {
|
||||
return message.reply(
|
||||
globals.Replies.find(x => x.id === 'TOO_FEW_ARGS').string +
|
||||
cmdprefix + commandName + ' ' + command.usage
|
||||
);
|
||||
} else {
|
||||
command.exec(message, args);
|
||||
}
|
||||
} catch (e) {
|
||||
message.reply(globals.Replies.find(x => x.id === 'ERROR').string);
|
||||
}
|
||||
}
|
||||
//console.log(`${new Date().toUTCString()} ${message.author.tag} (size: ${message.attachments.size})`);
|
||||
if (message.attachments.size > 0 && message.channel.type == 'dm' && !message.author.bot) {
|
||||
try {
|
||||
const response = await fetch(message.attachments.first().url);
|
||||
const data = await validateJSON(response);
|
||||
if (data) await CreateFromFile(message, data);
|
||||
} catch (e) {
|
||||
console.log(e);
|
||||
return message.reply(globals.Replies.find(x => x.id === 'ERROR').string);
|
||||
}
|
||||
} else {
|
||||
if (!message.content.startsWith(cmdprefix) || message.author.bot) {
|
||||
return;
|
||||
}
|
||||
const args = message.content.slice(cmdprefix.length).split(' ');
|
||||
const commandName = args.shift().toLowerCase();
|
||||
//if (!client.commands.has(commandName)) return;
|
||||
try {
|
||||
const command =
|
||||
client.commands.get(commandName) ||
|
||||
client.commands.find(cmd => cmd.aliases && cmd.aliases.includes(commandName));
|
||||
if (!command) return;
|
||||
if (command.needs_args && !args.length) {
|
||||
return message.reply(
|
||||
globals.Replies.find(x => x.id === 'TOO_FEW_ARGS').string +
|
||||
cmdprefix +
|
||||
commandName +
|
||||
' ' +
|
||||
command.usage
|
||||
);
|
||||
} else {
|
||||
command.exec(message, args);
|
||||
}
|
||||
} catch (e) {
|
||||
message.reply(globals.Replies.find(x => x.id === 'ERROR').string);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
function validateJSON(body) {
|
||||
try {
|
||||
const data = body.json();
|
||||
return data;
|
||||
} catch (e) {
|
||||
return null;
|
||||
}
|
||||
try {
|
||||
const data = body.json();
|
||||
return data;
|
||||
} catch (e) {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
async function CreateFromFile(message, data) {
|
||||
try {
|
||||
db.find({
|
||||
user: message.author.tag,
|
||||
}, function (err, docs) {
|
||||
if (docs.length === 0) {
|
||||
db.insert({
|
||||
user: message.author.tag,
|
||||
character: data,
|
||||
}, function (err, docs) {
|
||||
message.reply(globals.Replies.find(r => r.id === 'SAVED_DATA').string);
|
||||
});
|
||||
}
|
||||
});
|
||||
} catch (e) {
|
||||
throw e;
|
||||
}
|
||||
}
|
||||
try {
|
||||
db.find(
|
||||
{
|
||||
user: message.author.tag,
|
||||
},
|
||||
function (err, docs) {
|
||||
if (docs.length === 0) {
|
||||
db.insert(
|
||||
{
|
||||
user: message.author.tag,
|
||||
character: data,
|
||||
},
|
||||
function (err, docs) {
|
||||
message.reply(globals.Replies.find(r => r.id === 'SAVED_DATA').string);
|
||||
}
|
||||
);
|
||||
}
|
||||
}
|
||||
);
|
||||
} catch (e) {
|
||||
throw e;
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user