diff --git a/Episode 9/handlers/message.js b/Episode 9/handlers/message.js index 982fbdf..1a16e25 100644 --- a/Episode 9/handlers/message.js +++ b/Episode 9/handlers/message.js @@ -2,11 +2,9 @@ const {bot} = require('../index'); const config = require("../config.json"); bot.on("message", async message => { - if (message.author.bot) return; - if (message.channel.type === "dm") return; let prefix = config.prefix; - let args = message.content.slice(prefix.length).trim().split(' '); + let args = message.content.slice(prefix.length).split(/ +/); let cmd = args.shift().toLowerCase(); let command; @@ -20,16 +18,19 @@ bot.on("message", async message => { let afkcheck = bot.afk.get(message.author.id); if (afkcheck) return [bot.afk.delete(message.author.id), message.reply(`you have been removed from the afk list!`).then(msg => msg.delete(5000))]; - if (!message.content.startsWith(prefix)) return; - - if (bot.commands.has(cmd)) { - command = bot.commands.get(cmd); - } else { - command = bot.commands.get(bot.aliases.get(cmd)); - } + if (!message.content.startsWith(prefix) || message.author.bot || message.channel.type === "dm") return; + var command = bot.commands.get(cmd) || bot.commands.find(al => al.aliases && a.aliases.includes(cmd)); + + if (!command) return; + // checks if the message doesn't match a command name because this will make the bot crash every time you send the bot prefix only or if you type a wrong command name + + try { if (command) command.run(bot, message, args); - + } catch (error) { + message.reply("there was an error") + console.log(error) + } // let cmd = bot.commands.get(command.slice(prefix.length)); // if (cmd) cmd.run(bot, message, args); -}); \ No newline at end of file +});