Home > Net >  How can i make it if user have a mange massage dont mute him?
How can i make it if user have a mange massage dont mute him?

Time:10-27

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

How can i make it if user have a mange massage dont mute him? i trying but its peek give a mute role So how can i resolve this

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