I have a command which allows users to make the bot to say a message, but I would like it to be able to check whether the user is able to send messages in that channel before it sends it. Currently, I just have it locked to the MANAGE_MESSAGES
permission.
Here is the code:
if (message.member.hasPermission("MANAGE_MESSAGES")) { let msg; let textChannel = message.mentions.channels.first(); message.delete(); if (textChannel) { msg = args.slice(1).join(" "); if (msg === "") { message.channel.send("Please input a valid sentence/word.") } else { textChannel.send(`**Message from ${message.author.tag}:** ${msg}`) } } else { msg = args.join(" "); if (msg === "") { message.channel.send("Please input a valid sentence/word.") } else { message.channel.send(`**Message from ${message.author.tag}:** ${msg}`) } } } else { message.reply('You lack the required permissions to do that. (Required Permissions: ``MANAGE_MESSAGES``)') }
I have searched and couldn’t find much on this, any help is appreciated
Advertisement
Answer
I’m not sure if I understand your question correctly. You can check if the member has permission in a certain channel using channel.permissionFor(member).has('PERMISSION_NAME')
. I’m not sure if you really wanted the user to have MANAGE_MESSAGES
permission, I think SEND_MESSAGES
should be enough, so I used that in my code below. I also made it a bit cleaner and added some comments:
const mentionedChannel = message.mentions.channels.first(); // if there is no mentioned channel, channel will be the current one const channel = mentionedChannel || message.channel; message.delete(); // returns true if the message author has SEND_MESSAGES permission // in the channel (the mentioned channel if they mentioned one) const hasPermissionInChannel = channel .permissionsFor(message.member) .has('SEND_MESSAGES', false); // if the user has no permission, just send an error message and return // so the rest of the code is ignored if (!hasPermissionInChannel) { return message.reply( `You can't send messages in ${mentionedChannel}. You don't have the required permission: `SEND_MESSAGES``, ); } const msg = mentionedChannel ? args.slice(1).join(' ') : args.join(' '); if (!msg) { // send an error message in the same channel the command was coming // from and return return message.reply('Please input a valid sentence/word.'); } // if the user has permission and has a message to post send it to the // mentioned or current channel channel.send(`**Message from ${message.author.tag}:** ${msg}`);