Home > database >  Check if user has MANAGE_MESSAGES permission
Check if user has MANAGE_MESSAGES permission

Time:10-27

I am trying to give a muted role to users that send a message within a channel, however I do not want to issue it to anyone who has the MANAGE_MESSAGES permission.

How can I implement this? Here is my attempt below though it is still giving the muted rule.

client.on('message', async (message) => {
  let data = await antilinks.findOne({ Guild: message.guild.id });
  if (data.run === 'off') return;
  if (data.run === 'on') {
    if (links(message.content) === true) {
      message.delete()
      try {
        let muterole = message.guild.roles.cache.find(ro => ro.name === 'Muted');
        if (!muterole) {
          message.guild.roles.create({
            data: {
              name: 'Muted',
              color: 'RANDOM',
              permissions: []
            }
          })
        } else {
          message.guild.member(message.author).roles.add(muterole);
        }
      } catch (err) {
        console.log(err);
      }
    }
  }
})

CodePudding user response:

You can check to see if someone has a specific permission using hasPermmission().

client.on('message', async (message) => {
  let data = await antilinks.findOne({ Guild: message.guild.id });

  // If this user has MANAGE_MESSAGES permission, stop here and do nothing.
  if (message.member.hasPermmission('MANAGE_MESSAGES')) return; 

  if (data.run === 'off') return;
  if (data.run === 'on') {
    if (links(message.content) === true) {
      message.delete()
      try {
        let muterole = message.guild.roles.cache.find(ro => ro.name === 'Muted');
        if (!muterole) {
          message.guild.roles.create({
            data: {
              name: 'Muted',
              color: 'RANDOM',
              permissions: []
            }
          })
        } else {
          message.guild.member(message.author).roles.add(muterole);
        }
      } catch (err) {
        console.log(err);
      }
    }
  }
})

CodePudding user response:

Just incase, you will need to use a new import for this method.
Import code: const { Permissions } = require('discord.js');

Method:
For discord.js we use the Permissions method to check if the user has any roles that has the permissions you set

Example Code:

if(message.guild.member(message.author).permissions.has(Permissions.FLAGS.MANAGE_MESSAGES){
    //code here
}

Your Code:

client.on('message', async (message) => {
  let data = await antilinks.findOne({ Guild: message.guild.id });
  if (data.run === 'off') return;
  if (data.run === 'on') {
    if (links(message.content) === true) {
      message.delete()
      try {
        let muterole = message.guild.roles.cache.find(ro => ro.name === 'Muted');
        if (!muterole) {
          message.guild.roles.create({
            data: {
              name: 'Muted',
              color: 'RANDOM',
              permissions: []
            }
          })
        } else if(message.guild.member(message.author).permissions.has(Permissions.FLAGS.MANAGE_MESSAGES){
          return;
        } else if(!message.guild.member(message.author).permissions.has(Permissions.FLAGS.MANAGE_MESSAGES){
          message.guild.member(message.author).roles.add(muterole);
        }
      } catch (err) {
        console.log(err);
      }
    }
  }
})

Docs:
Permissions | Discord.js

  • Related