Reputation:
I'm trying to get the bot to respond with "Given User is already muted" but when I mute someone who is already muted, it just responds saying I have muted the user.
I also would like to integrate:
if (member.roles.highest.position >= message.member.roles.highest.position)
return message.channel.send('You cannot mute someone with an equal or higher role')
But when I try to, I get this error:
if (member.roles.highest.position >= message.member.roles.highest.position) ^
ReferenceError: member is not defined
AND
Cannot read property 'roles' of undefined
module.exports = {
name: 'mute',
description: "This mutes a member",
execute(message, args){
if (!message.member.hasPermission("MANAGE_ROLES")) {
return message.channel.send("Sorry but you do not have permission to mute anyone" );
}
if (!message.guild.me.hasPermission("MANAGE_ROLES")) {
return message.channel.send("I do not have permission to manage roles.");
}
const user = message.mentions.members.first();
if (!user) {
return message.channel.send("Please mention the member to who you want to mute");
}
if(user.id ===message.author.id) {
return message.channel.send("you can not mute yourself");
}
let reason = args.slice(1).join(" ")
if(!reason) {
return message.channel.send("Please Give the reason to mute the member");
}
//Mute ROLE
let muterole = message.guild.roles.cache.find(x => x.name === "Muted")
let mainrole = message.guild.roles.cache.find(x => x.name === "Verified")
if(!muterole) {
return message.channel.send("This server does not have role with name `Muted`")
}
if(user.roles.cache.has(muterole)) {
return message.channel.send("Given User is already muted")
}
user.roles.remove(mainrole)
user.roles.add(muterole)
message.channel.send(`You muted **${message.mentions.users.first().username}** Muted by:<@${message.author.id}> For \`${reason}\``)
user.send(`You are muted in **${message.guild.name}**,For \`${reason}\``)
}
}
Upvotes: 0
Views: 117
Reputation: 927
The issue is that you defined user
as the variable for the "member" that you want to target with the mute, rather than a variable member
(which your roles comparison code checks).
Your role checking code can be used like this:
const user = message.mentions.members.first();
if (!user) {
return message.channel.send("Please mention the member to who you want to mute");
}
// ... Other checks here
// Compared after `user` is defined, and undefined check is done
if (user.roles.highest.position >= message.member.roles.highest.position) {
return message.channel.send('You cannot mute someone with an equal or higher role');
}
Note that I use user.roles.highest.position
instead of member.roles.highest.position
.
Upvotes: 1