Check if a user can send message in a specific channel discord.js

beans

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

Zsolt Meszaros

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}`);

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Java

Assign discord role when user reacts to message in certain channel

From Dev

Can I send a message to an IRC channel without joining it?

From Dev

How Can I Send Message To specific User with signalR

From Dev

Send message to specific user in signalr

From Dev

discord channel link in message

From Dev

Unable to send a message to specific channel in Slack with Hubot - SlackRTMError: no channel id

From Dev

Linux + send wall message only to the specific user

From Dev

how can i make my discord.py bot send a message i choose to a channel i choose?

From Dev

How do I send a message to a specific channel in discord.js?

From Dev

How to check if a specific user has a role? Discord js

From Dev

How do I make it so that a user can not post more than one message in a row, discord.js

From Dev

Discord.py - Send message under different name or user

From Dev

Use Discord API to send a private message given a user ID

From Dev

Get last message from text channel with discord.js

From Dev

Discord js / Check if user is in an specific voice Channel

From Dev

How to send message with discord.js with ID

From Dev

Check if Specific user id is in Voice Channel?

From Dev

Trying to send message to specific channel, but can't define my client

From Dev

How can I check the role of a message author + How do I DM specific roles on Discord.py?

From Dev

discord py - Send a different message if a user replies to my bot

From Dev

Discord.JS Send message to tagged user

From Dev

Send a message if there is a role Discord.js

From Dev

Linux + send wall message only to the specific user

From Dev

discord channel link in message

From Dev

Unable to send a message to specific channel in Slack with Hubot - SlackRTMError: no channel id

From Dev

Sending Message to Discord Channel without message from user

From Dev

message.channel.send not working discord.js

From Dev

Discord.js -- make presenceUpdate send a message

From Dev

Discord Bot shall delete "user pinned message to channel" message

Related Related

  1. 1

    Assign discord role when user reacts to message in certain channel

  2. 2

    Can I send a message to an IRC channel without joining it?

  3. 3

    How Can I Send Message To specific User with signalR

  4. 4

    Send message to specific user in signalr

  5. 5

    discord channel link in message

  6. 6

    Unable to send a message to specific channel in Slack with Hubot - SlackRTMError: no channel id

  7. 7

    Linux + send wall message only to the specific user

  8. 8

    how can i make my discord.py bot send a message i choose to a channel i choose?

  9. 9

    How do I send a message to a specific channel in discord.js?

  10. 10

    How to check if a specific user has a role? Discord js

  11. 11

    How do I make it so that a user can not post more than one message in a row, discord.js

  12. 12

    Discord.py - Send message under different name or user

  13. 13

    Use Discord API to send a private message given a user ID

  14. 14

    Get last message from text channel with discord.js

  15. 15

    Discord js / Check if user is in an specific voice Channel

  16. 16

    How to send message with discord.js with ID

  17. 17

    Check if Specific user id is in Voice Channel?

  18. 18

    Trying to send message to specific channel, but can't define my client

  19. 19

    How can I check the role of a message author + How do I DM specific roles on Discord.py?

  20. 20

    discord py - Send a different message if a user replies to my bot

  21. 21

    Discord.JS Send message to tagged user

  22. 22

    Send a message if there is a role Discord.js

  23. 23

    Linux + send wall message only to the specific user

  24. 24

    discord channel link in message

  25. 25

    Unable to send a message to specific channel in Slack with Hubot - SlackRTMError: no channel id

  26. 26

    Sending Message to Discord Channel without message from user

  27. 27

    message.channel.send not working discord.js

  28. 28

    Discord.js -- make presenceUpdate send a message

  29. 29

    Discord Bot shall delete "user pinned message to channel" message

HotTag

Archive