【问题标题】:Nodejs how to put multiple id's into an ifNodejs如何将多个id放入if
【发布时间】:2020-12-30 19:10:56
【问题描述】:
当我尝试将多个 id 放入一个 if 它只注册第一个时。
if (message.author.id != '196568585202565121' || '110324497353052160') return message.reply("You don't have the perms")
它只会回复第一个 id 而不会回复第二个。第二个会得到你没有权限的消息。
【问题讨论】:
标签:
javascript
node.js
discord
【解决方案1】:
if (message.author.id != '196568585202565121' || '110324497353052160') return message.reply("You don't have the perms")
这说明:
如果邮件作者的 ID 不是 196568585202565121 或 110324497353052160。
你想要它说的是:
如果消息作者的 ID 不是 196568585202565121 或者消息作者的 ID 不是 196568585202565121。
将您的线路更改为:
if (message.author.id !== '196568585202565121' || message.author.id !== '110324497353052160')
还要注意 != 被替换为双等号。 !==。在 JavaScript 中,!= 不够严格。我们需要使用 !==
【解决方案2】:
你必须像这样在 if 的两个部分进行比较:
if (message.author.id !== '196568585202565121' || message.author.id !== '110324497353052160') return message.reply("You don't have the perms")
您还可以将 id 添加到数组并过滤数组。
let arr = ['196568585202565121', '110324497353052160']
let filter = arr.filter ( e => e === message.author.id)
arr.length > 0 return message.reply("You don't have the perms")
【解决方案3】:
您可以将数组与.includes() 方法一起使用。
if(!['196568585202565121','110324497353052160'].includes(message.author.id)) {
// to the things...
}
阅读更多:Array.prototype.includes()
【解决方案4】:
如果没有匹配的 id,为了返回消息,这样做:
if (message.author.id !== '196568585202565121' || message.author.id !== '110324497353052160') {
return message.reply("You don't have the perms");
}