我想对discord.js进行切换,当用户已经拥有它时,提到的 Angular 色就会被删除,如果他没有它,它就会被附加。所以这里的代码是
let tempRole = message.guild.roles.find("name", args);
if(message.member.roles.has(tempRole)) {
console.log(`User has the role`);
}
args 是命令参数的数组,例如“!role [带有空格的 Angular 色名称]” 因此,如果我有 Angular 色“Test”,并且我输入“!role Test”,它会输出“User has the role” 那么,如果用户没有该 Angular 色,相反的代码又是怎样的呢? 因为
else if(message.member.roles.has(!tempRole)) {
console.log(`User has not the role`);
}
并没有真正起作用,但是'!'这是我所知道的唯一可以否定结果的事情
最佳答案
!
运算符否定右侧找到的任何内容,在您的情况下,您只是否定 tempRole
,但是您真正想要的是否定has
调用,如下所示:
!message.member.roles.has(tempRole)
尽管如此,由于已经有一个 if
语句来验证用户是否具有该 Angular 色,因此您可以使用 else
let tempRole = message.guild.roles.find("name", args);
if(message.member.roles.has(tempRole)) {
console.log(`User has the role`);
} else {
console.log(`User has not the role`);
}
或者使用三元运算符甚至更短
message.member.roler.has(tempRole)
? console.log('User has the role')
: console.log(`User has not the role`);
关于javascript - Javascript 中的变量取反,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50451049/