From b01670dcbfb379a48b49e320c2d232b0ebce78a5 Mon Sep 17 00:00:00 2001 From: Emily J Date: Wed, 21 Oct 2020 18:57:00 +1100 Subject: [PATCH] (WIP) message handler --- bot/util/handlers/messageHandler.js | 90 +++++++++++++++++++++++++++++ 1 file changed, 90 insertions(+) diff --git a/bot/util/handlers/messageHandler.js b/bot/util/handlers/messageHandler.js index e69de29..ee991a1 100644 --- a/bot/util/handlers/messageHandler.js +++ b/bot/util/handlers/messageHandler.js @@ -0,0 +1,90 @@ +class MessageHandler { + constructor (client) { + this.client = client; + } + + async handle (message) { + if (message.author.bot) return; + + const prefixes = []; + const data = {}; + + data.user = await this.client.db.getUser(message.author.id); + prefixes.push(data.user.prefix); + + if (message.channel.guild) { + if (!message.channel.permissionsOf(this.client.user.id).has('sendMessages')) return; + data.guild = await this.client.db.getGuild(message.channel.guild.id); + data.member = await this.client.db.getMember(message.channel.guild.id, message.author.id); + prefixes.push(data.guild.prefix); + + if (data.guild.blacklist.includes(message.author.id)) return; + + if (message.content === `<@${this.client.user.id}>` || message.content === `<@!${this.client.user.id}>`) { + return message.channel.createMessage(`Hi! The server prefix is \`${data.guild.prefix}\`, and your personal prefix is \`${data.user.prefix}\`. You can also ping me ^-^`); + } + } + + if (message.content === `<@${this.client.user.id}>` || message.content === `<@!${this.client.user.id}>`) { + return message.channel.createMessage(`Hi! Your personal prefix is \`${data.user.prefix}\`. You can also ping me ^-^`); + } + + prefixes.push(`<@${this.client.user.id}> `, `<@!${this.client.user.id}> `); + + let prefix; + + for (const thisPrefix of prefixes) { + if (message.content.startsWith(thisPrefix)) { + prefix = thisPrefix; + break; + } + } + + if (!prefix) return; + + const args = message.content.slice(prefix.length).trim().split(/ +/g); + const commandName = args.shift().toLowerCase(); + const command = this.client.commands.get(commandName) || this.client.commands.get(this.client.aliases.get(commandName)); + + if (!command) return; + + if (message.channel.guild) { + if (data.guild.disabledcommands.includes(command.name)) return message.channel.createMessage( + 'This command has been disabled by a server administrator.' + ); + + if (data.guild.disabledcategories.includes(command.category)) return message.channel.createMessage( + 'The category this command is apart of has been disabled by a server administrator.' + ); + + const missingUserPerms = this.client.helpers.checkPermissions(command, message); + if (missingUserPerms) return message.channel.createMessage( + `You can't use this command because you lack these permissions: \`${missingUserPerms.join('`, `')}\`` + ); + + const missingBotPerms = this.client.helpers.checkPermissions(command, message); + if (missingBotPerms) return message.channel.createMessage( + `I can't run this command because I lack these permissions: \`${missingUserPerms.join('`, `')}\`` + ); + } + + if (command.enabled === false) return message.channel.createMessage('This command has been disabled by my developers.'); + + if (command.devOnly === true && this.client.helpers.isDeveloper(message.author.id) !== true) { + return message.channel.send('This command\'s usage is restricted to developers only. Sorry!'); + } + + if (command.guildOnly === true && !message.channel.guild) { + return message.channel.createMessage('This command is unavailable in DM\'s, try running it in a server instead!'); + } + + try { + command.run(this.client, message, args, data); + this.client.logger.command(`Ran ${command.name}`); + } catch (error) { + this.client.logger.error('COMMAND_EXECUTION_ERROR', `${command.name}: ${error}`); + } + } +} + +module.exports = MessageHandler; \ No newline at end of file