diff --git a/.eslintrc.json b/.eslintrc.json index d1a2d380..94c48f07 100644 --- a/.eslintrc.json +++ b/.eslintrc.json @@ -26,6 +26,7 @@ "no-eval": "warn", "max-len": "off", "no-underscore-dangle": ["error", { "allow": ["_events"] }], - "global-require": "warn" + "global-require": "warn", + "no-nested-ternary": "warn" } } diff --git a/commands/System/reload.js b/commands/System/reload.js index aa4c73db..b295aa7c 100644 --- a/commands/System/reload.js +++ b/commands/System/reload.js @@ -2,46 +2,46 @@ exports.run = (client, msg, [type, name]) => { switch (type) { case "function": msg.channel.sendMessage(`Attemping to reload function ${name}`).then((m) => { - client.funcs.reload.function(client, msg, client.clientBaseDir, name).then(() => { - m.edit(`:white_check_mark: Succesfully reloaded function ${name}`); + client.funcs.reload.function(client, client.clientBaseDir, name).then((message) => { + m.edit(`:white_check_mark: ${message}`); }).catch((e) => { - m.edit(e); + m.edit(`:x: ${e}`); }); }); break; case "inhibitor": msg.channel.sendMessage(`Attempting to reload inhibitor ${name}`).then((m) => { - client.funcs.reload.inhibitor(client, msg, client.clientBaseDir, name).then(() => { - m.edit(`:white_check_mark: Succesfully reloaded inhibitor ${name}`); + client.funcs.reload.inhibitor(client, client.clientBaseDir, name).then((message) => { + m.edit(`:white_check_mark: ${message}`); }).catch((e) => { - m.edit(e); + m.edit(`:x: ${e}`); }); }); break; case "monitor": msg.channel.sendMessage(`Attempting to reload monitor ${name}`).then((m) => { - client.funcs.reload.monitor(client, msg, client.clientBaseDir, name).then(() => { - m.edit(`:white_check_mark: Succesfully reloaded monitor ${name}`); + client.funcs.reload.monitor(client, client.clientBaseDir, name).then((message) => { + m.edit(`:white_check_mark: ${message}`); }).catch((e) => { - m.edit(e); + m.edit(`:x: ${e}`); }); }); break; case "provider": msg.channel.sendMessage(`Attempting to reload provider ${name}`).then((m) => { - client.funcs.reload.provider(client, msg, client.clientBaseDir, name).then(() => { - m.edit(`:white_check_mark: Succesfully reloaded provider ${name}`); + client.funcs.reload.provider(client, client.clientBaseDir, name).then((message) => { + m.edit(`:white_check_mark: ${message}`); }).catch((e) => { - m.edit(e); + m.edit(`:x: ${e}`); }); }); break; case "event": msg.channel.sendMessage(`Attempting to reload event ${name}`).then((m) => { - client.funcs.reload.event(client, msg, name).then(() => { - m.edit(`:white_check_mark: Succesfully reloaded event ${name}`); + client.funcs.reload.event(client, name).then((message) => { + m.edit(`:white_check_mark: ${message}`); }).catch((e) => { - m.edit(e); + m.edit(`:x: ${e}`); }); }); break; @@ -53,10 +53,10 @@ exports.run = (client, msg, [type, name]) => { break; default: msg.channel.sendMessage(`Attempting to reload command ${name}`).then((m) => { - client.funcs.reload.command(client, msg, client.clientBaseDir, name).then(() => { - m.edit(`:white_check_mark: Succesfully reloaded command ${name}`); + client.funcs.reload.command(client, client.clientBaseDir, name).then((message) => { + m.edit(`:white_check_mark: ${message}`); }).catch((e) => { - m.edit(e); + m.edit(`:x: ${e}`); }); }); break; diff --git a/functions/loadSingleCommand.js b/functions/loadSingleCommand.js index 2ec6ea4a..05672423 100644 --- a/functions/loadSingleCommand.js +++ b/functions/loadSingleCommand.js @@ -31,7 +31,7 @@ module.exports = (client, command, reload = false, loadPath = null) => new Promi let pathParts = loadPath.split(path.sep); pathParts = pathParts.slice(pathParts.indexOf("commands") + 1); category = client.funcs.toTitleCase(cmd.help.category ? cmd.help.category : (pathParts[0] && pathParts[0].length > 0 ? pathParts[0] : "General")); - subCategory = client.funcs.toTitleCase(cmd.help.subCategory ? cmd.help.subCategory : (pathParts[1] && pathParts[1].length > 0 && !~pathParts[1].indexOf(".") ? pathParts[1] : "General")); + subCategory = client.funcs.toTitleCase(cmd.help.subCategory ? cmd.help.subCategory : (pathParts[1] && pathParts[1].length > 0 && pathParts[1].indexOf(".") === -1 ? pathParts[1] : "General")); } catch (e) { if (e.code === "MODULE_NOT_FOUND") { const module = /'[^']+'/g.exec(e.toString()); diff --git a/functions/reload.js b/functions/reload.js index c4a3b980..39f2756d 100644 --- a/functions/reload.js +++ b/functions/reload.js @@ -1,13 +1,13 @@ const path = require("path"); -exports.function = (client, msg, dir, funcName) => new Promise((resolve, reject) => { - if (client.funcs.hasOwnProperty(funcName)) { - client.funcs.getFileListing(client, dir, "functions").then((files) => { +exports.function = (client, dir, funcName) => new Promise((resolve, reject) => { + client.funcs.getFileListing(client, dir, "functions").then((files) => { + if (client.funcs.hasOwnProperty(funcName)) { const oldFunction = files.filter(f => f.name === funcName); if (oldFunction[0]) { - client.funcs[funcName] = ""; try { oldFunction.forEach((file) => { + client.funcs[funcName] = ""; delete require.cache[require.resolve(`${file.path}${path.sep}${file.base}`)]; client.funcs[funcName] = require(`${file.path}${path.sep}${file.base}`); if (client.funcs[funcName].init) { @@ -15,22 +15,47 @@ exports.function = (client, msg, dir, funcName) => new Promise((resolve, reject) } }); } catch (error) { - reject(`:x: ${error}`); + reject(error); return; } - resolve(); + resolve(`Succesfully reloaded the function ${funcName}.`); } else { - reject(`:x: The function **${funcName}** does not reside in ${dir}functions`); + reject(`The function **${funcName}** does not reside in ${dir}functions`); } - }); - } else { - reject(`:x: The function **${funcName}** does not seem to exist!`); - } + } else { + const newFunction = files.filter(f => f.name === funcName); + if (newFunction[0]) { + try { + newFunction.forEach((file) => { + client.funcs[funcName] = require(`${file.path}${path.sep}${file.base}`); + if (client.funcs[funcName].init) { + client.funcs[funcName].init(client); + } + }); + resolve(`Succesfully loaded a new function called ${funcName}.`); + } catch (error) { + if (error.code === "MODULE_NOT_FOUND") { + const module = /'[^']+'/g.exec(error.toString()); + client.funcs.installNPM(module[0].slice(1, -1)).then(() => { + client.funcs.reload.function(client, dir, funcName); + }).catch((e) => { + console.error(e); + process.exit(); + }); + } else { + reject(`Could not load new function data: ${error}`); + } + } + } else { + reject(`Could not locate a new function ${funcName} in ${dir}functions`); + } + } + }); }); -exports.inhibitor = (client, msg, dir, inhibName) => new Promise((resolve, reject) => { - if (client.commandInhibitors.has(inhibName)) { - client.funcs.getFileListing(client, dir, "inhibitors").then((files) => { +exports.inhibitor = (client, dir, inhibName) => new Promise((resolve, reject) => { + client.funcs.getFileListing(client, dir, "inhibitors").then((files) => { + if (client.commandInhibitors.has(inhibName)) { const oldInhibitor = files.filter(f => f.name === inhibName); if (oldInhibitor[0]) { try { @@ -44,22 +69,48 @@ exports.inhibitor = (client, msg, dir, inhibName) => new Promise((resolve, rejec } }); } catch (error) { - reject(`:x: ${error}`); + reject(error); return; } - resolve(); + resolve(`Succesfully reloaded the inhibitor ${inhibName}`); } else { - reject(`:x: The inhibitor **${inhibName}** does not seem to reside in ${dir}inhibitors`); + reject(`The inhibitor **${inhibName}** does not seem to reside in ${dir}inhibitors`); } - }); - } else { - reject(`:x: The inhibitor **${inhibName}** does not seem to exist!`); - } + } else { + const newInhibitor = files.filter(f => f.name === inhibName); + if (newInhibitor[0]) { + try { + newInhibitor.forEach((file) => { + const props = require(`${file.path}${path.sep}${file.base}`); + client.commandInhibitors.set(file.name, props); + if (props.init) { + props.init(client); + } + }); + resolve(`Succesfully loaded a new inhibitor called ${inhibName}.`); + } catch (error) { + if (error.code === "MODULE_NOT_FOUND") { + const module = /'[^']+'/g.exec(error.toString()); + client.funcs.installNPM(module[0].slice(1, -1)).then(() => { + client.funcs.reload.inhibitor(client, dir, inhibName); + }).catch((e) => { + console.error(e); + process.exit(); + }); + } else { + reject(`Could not load new inhibitor data: ${error}`); + } + } + } else { + reject(`Could not locate a new inhibitor ${inhibName} in ${dir}inhibitors`); + } + } + }); }); -exports.monitor = (client, msg, dir, monitName) => new Promise((resolve, reject) => { - if (client.commandMonitors.has(monitName)) { - client.funcs.getFileListing(client, dir, "monitors").then((files) => { +exports.monitor = (client, dir, monitName) => new Promise((resolve, reject) => { + client.funcs.getFileListing(client, dir, "monitors").then((files) => { + if (client.commandMonitors.has(monitName)) { const oldMonitor = files.filter(f => f.name === monitName); if (oldMonitor[0]) { try { @@ -73,22 +124,48 @@ exports.monitor = (client, msg, dir, monitName) => new Promise((resolve, reject) } }); } catch (error) { - reject(`:x: ${error}`); + reject(error); return; } - resolve(); + resolve(`Succesfully reloaded the monitor ${monitName}.`); } else { - reject(`:x: The monitor **${monitName}** does not reside in ${dir}monitors`); + reject(`The monitor **${monitName}** does not reside in ${dir}monitors`); } - }); - } else { - reject(`:x: The monitor **${monitName}** does not seem to exist!`); - } + } else { + const newMonitor = files.filter(f => f.name === monitName); + if (newMonitor[0]) { + try { + newMonitor.forEach((file) => { + const props = require(`${file.path}${path.sep}${file.base}`); + client.commandMonitors.set(file.name, props); + if (props.init) { + props.init(client); + } + }); + resolve(`Succesfully loaded a new monitor called ${monitName}.`); + } catch (error) { + if (error.code === "MODULE_NOT_FOUND") { + const module = /'[^']+'/g.exec(error.toString()); + client.funcs.installNPM(module[0].slice(1, -1)).then(() => { + client.funcs.reload.monitor(client, dir, monitName); + }).catch((e) => { + console.error(e); + process.exit(); + }); + } else { + reject(`Could not load new monitor data: ${error}`); + } + } + } else { + reject(`Could not locate a new monitor ${monitName} in ${dir}monitors`); + } + } + }); }); -exports.provider = (client, msg, dir, providerName) => new Promise((resolve, reject) => { - if (client.dataProviders.has(providerName)) { - client.funcs.getFileListing(client, dir, "dataProviders").then((files) => { +exports.provider = (client, dir, providerName) => new Promise((resolve, reject) => { + client.funcs.getFileListing(client, dir, "dataProviders").then((files) => { + if (client.dataProviders.has(providerName)) { const oldProvider = files.filter(f => f.name === providerName); if (oldProvider[0]) { try { @@ -102,20 +179,46 @@ exports.provider = (client, msg, dir, providerName) => new Promise((resolve, rej } }); } catch (error) { - reject(`:x: ${error}`); + reject(error); return; } - resolve(); + resolve(`Succesfully reloaded the provider ${providerName}.`); } else { - reject(`:x: The provider **${providerName}** does not seem to reside in ${dir}dataProviders`); + reject(`The provider **${providerName}** does not seem to reside in ${dir}dataProviders`); } - }); - } else { - reject(`:x: The provider **${providerName}** does not seem to exist!`); - } + } else { + const newProvider = files.filter(f => f.name === providerName); + if (newProvider[0]) { + try { + newProvider.forEach((file) => { + const props = require(`${file.path}${path.sep}${file.base}`); + client.dataProviders.set(file.name, props); + if (props.init) { + props.init(client); + } + }); + resolve(`Succesfully loaded a new provider called ${providerName}.`); + } catch (error) { + if (error.code === "MODULE_NOT_FOUND") { + const module = /'[^']+'/g.exec(error.toString()); + client.funcs.installNPM(module[0].slice(1, -1)).then(() => { + client.funcs.reload.provider(client, dir, providerName); + }).catch((e) => { + console.error(e); + process.exit(); + }); + } else { + reject(`Could not load new provider data: ${error}`); + } + } + } else { + reject(`Could not locate a new provider ${providerName} in ${dir}providers`); + } + } + }); }); -exports.event = (client, msg, eventName) => new Promise((resolve, reject) => { +exports.event = (client, eventName) => new Promise((resolve, reject) => { client.funcs.getFileListing(client, client.clientBaseDir, "events").then((files) => { const oldEvent = files.filter(f => f.name === eventName); if (oldEvent[0] && oldEvent[0].name === eventName) { @@ -132,17 +235,17 @@ exports.event = (client, msg, eventName) => new Promise((resolve, reject) => { client.on(file.name, (...args) => require(`${file.path}${path.sep}${file.base}`).run(client, ...args)); }); } catch (error) { - reject(`:x: ${error}`); + reject(error); return; } - resolve(); + resolve(`Sucesfully reloaded the event ${eventName}`); } else { - reject(`:x: The event **${eventName}** does not seem to exist!`); + reject(`The event **${eventName}** does not seem to exist!`); } }); }); -exports.command = (client, msg, commandName) => new Promise((resolve, reject) => { +exports.command = (client, dir, commandName) => new Promise((resolve, reject) => { let command; if (client.commands.has(commandName)) { command = commandName; @@ -156,18 +259,18 @@ exports.command = (client, msg, commandName) => new Promise((resolve, reject) => newCommands.forEach((file) => { client.funcs.loadSingleCommand(client, command, false, `${file.path}${path.sep}${file.base}`) .catch((e) => { - reject(`:x: ${e}`); + reject(e); }); }); }); - resolve(); + resolve(`Succesully loaded a new command called ${commandName}`); } else { client.funcs.loadSingleCommand(client, command, true) .then(() => { - resolve(); + resolve(`Succesfully reloaded the command ${commandName}`); }) .catch((e) => { - reject(`:x: ${e}`); + reject(e); }); } });