Ethan Agar
Ethan Agar

Reputation: 102

How to find th author of a message in discord, and how to use that to figure out if it has the right permissions for a command

I'm trying to figure out if the author a command, has the right permissions to carry out the command. But the console keeps saying TypeError: Cannot read property 'permission' of undefined. Heres my code.

  name: 'mute',
  description: 'mutes someone',
  execute(message,args) { 
    const target = message.mentions.members.first();
    const muteRole = message.guild.roles.cache.find(role => role.name === 'MUTED')
    let sender = message.author
    let role = message.member.roles

   if(!target || !message.user.permission.has('MANAGE_ROLES') ) {
    message.channel.send("You cant mute that member.")
   } else if(message.sender.permission.has('MANAGE_ROLES')) {  
    target.roles.set([muteRole])    
    //target.roles.add(muteRole)
    message.channel.send("User has been muted")
  }
  }
}

I know that it can't find the author, but I don't know how to fix that. Also I think the message.user.permission.has('MANAGE_ROLES') is also wrong, but again I don't know how to fix it. Online it has conflicting information.

Upvotes: 0

Views: 77

Answers (2)

pauliesnug
pauliesnug

Reputation: 205

Replace the message.user.permissions.has('MANAGE_ROLES') with:

message.member.permission.has('MANAGE_ROLES')

This uses the GuildMember object instead of the User object because you can’t fetch roles from a User object.

Upvotes: -1

Ethan Agar
Ethan Agar

Reputation: 102

Ok so I found the answer

const { Permissions } = require('discord.js');

module.exports = {
  name: 'mute',
  description: 'mutes someone',
  execute(message,args) { 
    const target = message.mentions.members.first();
    const muteRole = message.guild.roles.cache.find(role => role.name === 'MUTED')
    let user = message.author;

   if(!target || !message.member.permissions.has('MANAGE_ROLES')) {
    message.channel.send("You cant mute that member.")
   } else if(message.member.permissions.has('MANAGE_ROLES')) {  
    target.roles.set([muteRole])    
    //target.roles.add(muteRole)
    message.channel.send("User has been muted")
  }
  }
}

Upvotes: 3

Related Questions