Reputation:
If the user does not have the role, it will add the role, but if the user already has the role, it will remove the role. How can I do that?
if (message.member.roles.has(role.id)){
const embed = new MessageEmbed()
.setColor('#5865F2')
.setDescription(`Role added ${role} to ${member}.`)
.setFooter(`Requested by ${message.author.username}`)
member.roles.remove(role.id)
message.channel.send({ embeds: [embed] });
} else {
const embed = new MessageEmbed()
.setColor('#5865F2')
.setDescription(`Role removed ${role} to ${member}.`)
.setFooter(`Requested by ${message.author.username}`)
member.roles.add(role.id)
message.channel.send({ embeds: [embed] });
}
Upvotes: 0
Views: 474
Reputation: 2337
The reason you are getting this error is because the .has()
function does not exist in the roles
property. Instead, you need to fetch them using .fetch()
or use the cached roles by using roles.cache.has()
. So your code might look something like this:
if (message.member.roles.cache.has(role.id)){
const embed = new MessageEmbed()
.setColor('#5865F2')
.setDescription(`Role added ${role} to ${member}.`)
.setFooter(`Requested by ${message.author.username}`)
member.roles.remove(role.id)
message.channel.send({ embeds: [embed] });
} else {
const embed = new MessageEmbed()
.setColor('#5865F2')
.setDescription(`Role removed ${role} to ${member}.`)
.setFooter(`Requested by ${message.author.username}`)
member.roles.add(role.id)
message.channel.send({ embeds: [embed] });
}
Upvotes: 1