所以我尝试创建一个代码,如果用户已经拥有该角色,它将删除它,如果没有,则它将添加该角色。我想出了如何简单地做到这一点,但它删除了它,然后再次添加它。代码内部没有错误。该命令像“s!role”一样运行。
var hasRole = false;
SupervisorRoles.forEach(findrole =>{
if(message.member.roles.cache.some(role => role.id === findrole)) hasRole = true; //if user has role, sets bool to true
})
if(hasRole === true){
let User = message.guild.member(message.mentions.members.first()) || message.guild.members.cache.get(args[0])
if(!User) {
message.delete()
return;
}
if(args[1] === "Recruit") {
if(User.roles.cache.get("846953430378610744")) {
message.delete()
User.roles.remove("846953430378610744")
const removedembed1 = new Discord.MessageEmbed()
.setAuthor(`${message.author.tag}`, message.author.displayAvatarURL())
.setDescription(`Removed role from ${User}`)
.setColor("#feb90f")
.setFooter(`Removed role at: ${moment(message.createdAt).format('DD/MM/YY/H/ h:mm:ss a')}`)
message.channel.send(removedembed1)
const removedembed2 = new Discord.MessageEmbed()
.setAuthor(`${User.user.tag}`, User.user.displayAvatarURL())
.setDescription(`${User} roles has been updated by ${message.author}. \n \n __**Updated Roles:**__ \n \n ❌ Recruit`)
.setColor("#feb90f")
.setFooter(`Removed role at: ${moment(message.createdAt).format('DD/MM/YY/H/ h:mm:ss a')}`)
client.channels.cache.get('851670920926199808').send(removedembed2)
}
User.roles.add("846953430378610744")
message.delete()
const addedembed1 = new Discord.MessageEmbed()
.setAuthor(`${message.author.tag}`, message.author.displayAvatarURL())
.setDescription(`Added role to ${User}`)
.setColor("#feb90f")
.setFooter(`Added role at: ${moment(message.createdAt).format('DD/MM/YY/H/ h:mm:ss a')}`)
message.channel.send(addedembed1)
const addedembed2 = new Discord.MessageEmbed()
.setAuthor(`${User.user.tag}`, User.user.displayAvatarURL())
.setDescription(`${User} roles has been updated by ${message.author}. \n \n __**Updated Roles:**__ \n \n ✅ Recruit`)
.setColor("#feb90f")
.setFooter()
.setFooter(`Added role at: ${moment(message.createdAt).format('DD/MM/YY/H/ h:mm:ss a')}`)
client.channels.cache.get('851670920926199808').send(addedembed2)
} else {
message.delete()
return;
}
}
else{
message.channel.send("no perms")
}
}
```
答案 0 :(得分:0)
你可以试试这个:
const roleName = message.guild.roles.cache.find(r => (r.name === args[1].toString()) || (r.id === args[1].toString().replace(/[^\w\s]/gi, '')));
const alreadyHasRole = member._roles.includes(roleName.id);
if(alreadyHasRole) {
member.roles.remove(roleName).then(() => message.channel.send(removeEmbed))
client.channels.cache.get(`${process.env.MSGLOG}`).send(logEmbed2);
return;
}
这将检查是否有角色。您可以随时更改 roleName 变量。