From eb041f385e78b9c03028557eb4e72585f210c129 Mon Sep 17 00:00:00 2001 From: Faith Date: Thu, 2 Feb 2017 23:23:55 -0500 Subject: [PATCH] [FEATURES] Destruction of App.js (#137) * The Destruction of App.js * [PR] Changelog && Patch Bump * [PR] Eslint && Array-prefixing fix --- CHANGELOG.md | 3 +++ app.js | 22 ++++---------------- commands/System/transfer.js | 2 +- functions/commandHandler.js | 23 --------------------- functions/handleCommand.js | 40 +++++++++++++++++++++++++++++++++++++ functions/handleMessage.js | 8 ++++++++ functions/parseCommand.js | 30 ++++++++++++++++++++++++++++ package.json | 2 +- 8 files changed, 87 insertions(+), 43 deletions(-) delete mode 100644 functions/commandHandler.js create mode 100644 functions/handleCommand.js create mode 100644 functions/handleMessage.js create mode 100644 functions/parseCommand.js diff --git a/CHANGELOG.md b/CHANGELOG.md index 78db48e2..f9bf06d3 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -6,6 +6,7 @@ and this project adheres to [Semantic Versioning](http://semver.org/). ## [Unreleased] ### Added +- Added a bunch of unusable configuration options that'll make their debut soon. - All Bad Requests/Forbiddens.. etc, now properly give a human readable error in console or chat, depending on the error. (Not as of (0.17.0).. must be fixed) *** - New Error Creator - New CommandHandler (Removed it from message event) @@ -17,6 +18,7 @@ and this project adheres to [Semantic Versioning](http://semver.org/). - New Download Command ### Changed +- Broke down App.js Message Event into several smaller, changeable parts. - newError changed to send arguments to awaitMessage when errors are from usage - awaitMessage changed to work perfectly with the new system - msg.author.permLevel is now available immediately on a message, instead of after inhibitors run correctly. @@ -38,6 +40,7 @@ and this project adheres to [Semantic Versioning](http://semver.org/). - dataProviders renamed to Providers ### Fixed +- Fixed Typo in transfer command - Fixed Usage not working properly with selective - permissionLevels -> permissionLevel - Unchanged Package.json links to the repository diff --git a/app.js b/app.js index d9e29793..be818a03 100644 --- a/app.js +++ b/app.js @@ -52,27 +52,13 @@ exports.start = async (config) => { client.on("disconnect", e => client.funcs.log(e, "error")); client.on("message", async (msg) => { - if (msg.author.bot) return; + await client.funcs.runMessageMonitors(client, msg); msg.author.permLevel = await client.funcs.permissionLevel(client, msg.author, msg.guild); msg.guildConf = Config.get(msg.guild); client.i18n.use(msg.guildConf.lang); - await client.funcs.runMessageMonitors(client, msg); - if (client.config.selfbot && msg.author.id !== client.user.id) return; - const cmd = client.funcs.commandHandler(client, msg); - if (!cmd) return; - try { - const params = await client.funcs.runCommandInhibitors(client, msg, cmd); - cmd.run(client, msg, params); - } catch (error) { - if (error) { - if (error.code === 1 && client.config.cmdPrompt) { - client.funcs.awaitMessage(client, msg, cmd, [], error.message); - } else { - if (error.stack) client.emit("error", error.stack); - msg.channel.sendCode("JSON", (error.message || error)).catch(err => client.emit("error", err)); - } - } - } + if (!client.funcs.handleMessage(client, msg)) return; + const command = client.funcs.parseCommand(client, msg); + client.funcs.handleCommand(client, msg, command); }); client.login(client.config.botToken); diff --git a/commands/System/transfer.js b/commands/System/transfer.js index 463e627f..7a6818a2 100644 --- a/commands/System/transfer.js +++ b/commands/System/transfer.js @@ -16,7 +16,7 @@ exports.conf = { exports.help = { name: "transfer", - description: "Transfers a core command to the user folders", + description: "Transfers a core piece to its respected folder", usage: " ", usageDelim: " ", }; diff --git a/functions/commandHandler.js b/functions/commandHandler.js deleted file mode 100644 index e0515a87..00000000 --- a/functions/commandHandler.js +++ /dev/null @@ -1,23 +0,0 @@ -module.exports = (client, msg) => { - let thisPrefix; - if (msg.guildConf.prefix instanceof Array) { - msg.guildConf.prefix.forEach((prefix) => { - if (msg.content.startsWith(prefix)) thisPrefix = prefix; - else thisPrefix = prefix[0]; - }); - } else { - thisPrefix = msg.guildConf.prefix; - } - if (!msg.content.startsWith(thisPrefix) && client.config.prefixMention && !client.config.prefixMention.test(msg.content)) return false; - let prefixLength = thisPrefix.length; - if (client.config.prefixMention && client.config.prefixMention.test(msg.content)) prefixLength = client.config.prefixMention.exec(msg.content)[0].length + 1; - const command = msg.content.slice(prefixLength).split(" ")[0].toLowerCase(); - let cmd; - if (client.commands.has(command)) { - cmd = client.commands.get(command); - } else if (client.aliases.has(command)) { - cmd = client.commands.get(client.aliases.get(command)); - } - if (!cmd) return false; - return cmd; -}; diff --git a/functions/handleCommand.js b/functions/handleCommand.js new file mode 100644 index 00000000..2552fe02 --- /dev/null +++ b/functions/handleCommand.js @@ -0,0 +1,40 @@ +module.exports = async (client, msg, command, args = undefined) => { + const validCommand = this.getCommand(client, command); + if (!validCommand) return; + const response = this.runInhibitors(client, msg, validCommand); + if (response) return msg.reply(response); + try { + const params = await client.funcs.usage.run(client, msg, validCommand, args); + validCommand.run(client, msg, params); + } catch (error) { + if (error) { + if (error.code === 1 && client.config.cmdPrompt) { + client.funcs.awaitMessage(client, msg, validCommand, [], error.message); + } else { + if (error.stack) client.emit("error", error.stack); + msg.channel.sendCode("JSON", (error.message || error)).catch(err => client.emit("error", err)); + } + } + } +}; + +exports.getCommand = (client, command) => { + if (client.commands.has(command)) { + return client.commands.get(command); + } else if (client.aliases.has(command)) { + return client.commands.get(client.aliases.get(command)); + } + return false; +}; + +exports.runInhibitors = (client, msg, command) => { + const priority = client.commandInhibitors.array().sort((low, high) => low.conf.priority < high.conf.priority); + let response; + priority.some((inhibitor) => { // eslint-disable-line + if (inhibitor.conf.enabled) { + response = inhibitor.run(client, msg, command); + if (response) return true; + } + }); + return response; +}; diff --git a/functions/handleMessage.js b/functions/handleMessage.js new file mode 100644 index 00000000..11793b4c --- /dev/null +++ b/functions/handleMessage.js @@ -0,0 +1,8 @@ +module.exports = (client, msg, edited = false) => { + if (client.config.ignoreBots && msg.author.bot) return false; // Ignore Bots if True + if (client.config.ignoreSelf && msg.author.id === client.user.id) return false; // Ignore Self if true + if (client.config.selfbot && msg.author.id !== client.user.id) return false; // Ignore other users if selfbot is true + if (!client.config.selfbot && msg.author.id === client.user.id) return false; // Ignore other users if selfbot but config option is false + if (!client.config.editableCommands && edited) return false; // Ignore message if owner doesn't allow editableCommands + return true; +} diff --git a/functions/parseCommand.js b/functions/parseCommand.js new file mode 100644 index 00000000..fad054ad --- /dev/null +++ b/functions/parseCommand.js @@ -0,0 +1,30 @@ +module.exports = (client, msg, usage = false) => { + const prefix = this.getPrefix(client, msg); + if (!prefix) return; + const prefixLength = this.getLength(client, msg, prefix); + if (usage) return prefixLength; + return msg.content.slice(prefixLength).split(" ")[0].toLowerCase(); +}; + +exports.getLength = (client, msg, prefix) => { + if (client.config.prefixMention === prefix) { + return prefix.exec(msg.content)[0].length + 1; + } + return prefix.exec(msg.content)[0].length; +}; + +exports.getPrefix = (client, msg) => { + if (client.config.prefixMention.test(msg.content)) { + return client.config.prefixMention; + } + const prefix = msg.guildConf.prefix; + if (prefix instanceof Array) { + prefix.forEach((prefix) => { + if (msg.content.startsWith(prefix)) prefix = RegExp(`^${prefix}`); + else prefix = false; + }); + return prefix; + } + if (msg.content.startsWith(prefix)) return new RegExp(`^${prefix}`); // eslint-disable-line + return false; +}; diff --git a/package.json b/package.json index 79b52488..fa04cf7a 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "komada", - "version": "0.17.1", + "version": "0.17.2", "author": "Evelyne Lachance", "description": "Komada: Croatian for 'pieces', is a modular bot system including reloading modules and easy to use custom commands.", "main": "app.js",