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:
JavaScript
x
25
25
1
if (message.member.hasPermission("MANAGE_MESSAGES")) {
2
let msg;
3
let textChannel = message.mentions.channels.first();
4
5
message.delete();
6
7
if (textChannel) {
8
msg = args.slice(1).join(" ");
9
if (msg === "") {
10
message.channel.send("Please input a valid sentence/word.")
11
} else {
12
textChannel.send(`**Message from ${message.author.tag}:** ${msg}`)
13
}
14
} else {
15
msg = args.join(" ");
16
if (msg === "") {
17
message.channel.send("Please input a valid sentence/word.")
18
} else {
19
message.channel.send(`**Message from ${message.author.tag}:** ${msg}`)
20
}
21
}
22
} else {
23
message.reply('You lack the required permissions to do that. (Required Permissions: ``MANAGE_MESSAGES``)')
24
}
25
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:
JavaScript
1
32
32
1
const mentionedChannel = message.mentions.channels.first();
2
// if there is no mentioned channel, channel will be the current one
3
const channel = mentionedChannel || message.channel;
4
5
message.delete();
6
7
// returns true if the message author has SEND_MESSAGES permission
8
// in the channel (the mentioned channel if they mentioned one)
9
const hasPermissionInChannel = channel
10
.permissionsFor(message.member)
11
.has('SEND_MESSAGES', false);
12
13
// if the user has no permission, just send an error message and return
14
// so the rest of the code is ignored
15
if (!hasPermissionInChannel) {
16
return message.reply(
17
`You can't send messages in ${mentionedChannel}. You don't have the required permission: `SEND_MESSAGES``,
18
);
19
}
20
21
const msg = mentionedChannel ? args.slice(1).join(' ') : args.join(' ');
22
23
if (!msg) {
24
// send an error message in the same channel the command was coming
25
// from and return
26
return message.reply('Please input a valid sentence/word.');
27
}
28
29
// if the user has permission and has a message to post send it to the
30
// mentioned or current channel
31
channel.send(`**Message from ${message.author.tag}:** ${msg}`);
32