简体   繁体   English

不同的用户 ID [discord.js]

[英]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 you want to have other commands that only these two users can use, you don't have to copy-paste that big long if block elsewhere, it's fairly simple and easy.如果你想拥有只有这两个用户可以使用的其他命令,你不必复制粘贴那么长的if block 到别处,这相当简单易行。
  • If you do add another command like this, and then later want to add a third special user, you only have to change your code in one place (the admins array), rather than potentially missing a spot.如果您确实添加了另一个这样的命令,然后想要添加第三个特殊用户,您只需在一个地方(管理员数组)更改您的代码,而不是可能错过一个地方。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM