[英]Different user IDs [discord.js]
So I'm working in a discord bot where I want to add different user Ids in a command but now it doesn't work.所以我在 discord 机器人中工作,我想在命令中添加不同的用户 ID,但现在它不起作用。 the code looks like this:
代码如下所示:
if (message.author.id !== '513773028606476308', '749446086493470751') return message.channel.send("no")
I'm assuming you want a command only to run ONLY for users with specific IDs.我假设您希望一个命令只为具有特定 ID 的用户运行。 Therefore that
if
statement would be in some kind of function that handles that specific command.. your answer would be to change that if statement to因此,
if
语句将在某种处理该特定命令的 function 中..您的答案是将 if 语句更改为
if (!['513773028606476308', '749446086493470751'].includes(message.author.id)){return message.channel.send("no")}
However, the best solution would be to have an array of these special IDs in the first place like但是,最好的解决方案是首先拥有这些特殊 ID 的数组,例如
let allowed=['513773028606476308', '749446086493470751']
if (!allowed.includes(message.author.id)){return message.channel.send("no")}
There's a few things that are contributing to the problem you're seeing here.有几件事会导致您在此处看到的问题。
The first is that your if
statement isn't set up properly.首先是您的
if
语句设置不正确。 To do what you're looking for, it'd have to look like if (message.author.id.== '513773028606476308' && message.author.id !== '749446086493470751')
.要做你正在寻找的东西,它必须看起来像
if (message.author.id.== '513773028606476308' && message.author.id !== '749446086493470751')
。 While being able to just comma-seperate those values would be a nice feature, it's not currently something you can do in Javascript.虽然能够以逗号分隔这些值将是一个不错的功能,但目前在 Javascript 中无法做到这一点。 You have to explicitly say "if the author ID is not equal to
513773028606476308
and the author ID is not equal to 749446086493470751
" etc.您必须明确地说“如果作者 ID 不等于
513773028606476308
并且作者 ID 不等于749446086493470751
”等。
All that said, just continuously adding IDs on that if
statement probably isn't a really good idea.综上所述,只是在
if
语句上不断添加 ID 可能不是一个好主意。 Like commenters have pointed out, structuring this a bit differently would probably result in code that's both more readable and easier to change in the future.就像评论者指出的那样,稍微不同的结构可能会导致代码在未来更易读且更容易更改。 You'd do this by putting those user IDs in an array and then checking if the array doesn't contain those IDs, like so:
为此,您可以将这些用户 ID 放入一个数组中,然后检查该数组是否不包含这些 ID,如下所示:
let admins = ['749446086493470751', '749446086493470751'];
if (!admins.includes(message.author.id)) {
return message.channel.send("no");
}
This makes your life a little easier down the line for a few reasons:出于以下几个原因,这使您的生活更轻松:
if
block elsewhere, it's fairly simple and easy.if
block 到别处,这相当简单易行。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.