【问题标题】:How to make a bot identify you either by ID or permission如何让机器人通过 ID 或权限识别您
【发布时间】:2019-07-08 05:23:26
【问题描述】:

我遇到了一个问题,我的机器人只能通过许可进行识别,我什至确保我的 ID 是 !message.author.id == 所在的位置。我从!message.author.id 中删除了!,我的机器人仍然依赖于权限

我试过了 - 删除!来自!message.author.id - 在返回消息后添加else - 寻找类似问题 - 并搜索了 Discord.JS 文档。

if(!message.author.id ==`329023088517971969` || !message.member.hasPermission(['MANAGE_GUILD'])) return message.channel.send(`You don't have the permissions to do this!`)
<code here>

我希望输出是如果机器人发现我的 ID(作者)与之后的值相同,它会让我通过,或者如果两个陈述都是真的,它会让我通过,或者如果 hasPermission是真的,它会让我通过。 实际结果是只依赖hasPermission,而忽略了作者ID。

基本上 机器人应该:要么在if()之后运行代码,如果作者的ID等于代码中显示的ID,或者成员具有以下权限

【问题讨论】:

  • if(message.author.id ==="329023088517971969" || message.member.hasPermission(['MANAGE_GUILD'])) 不适合你?
  • 奇怪的是,if(!message.member.hasPermission(['MANAGE_GUILD']) || message.author.id ==="329023088517971969") return message.channel.send(您没有权限执行此操作!) MANAGE_GUILD 方面,if(message.member.hasPermission(['MANAGE_GUILD']) || message.author.id !="329023088517971969") return message.channel.send(您没有执行此操作的权限! ) 只有作者 ID 有效,将它们组合起来是行不通的。我想知道其中一个是否优先于另一个。

标签: javascript discord.js


【解决方案1】:

使用所有否定条件时,应使用 logical AND 运算符 (&amp;&amp;),而不是 logical OR 运算符 (||)。

if (message.author.id !== '329023088517971969' && !message.member.hasPermission('MANAGE_GUILD')) {
  return message.channel.send(':x: Insufficient permission.')
    .catch(console.error);
}

在英语中,这段代码基本上是说“如果这不是作者的 ID 并且他们没有权限标志,请停止。”从逻辑上讲,这比“如果这不是作者的 ID 他们没有权限标志”更有意义,因为如果一个为真而另一个为假,它会阻止你。

考虑这个例子:

const str = 'abcd';
const arr = ['foo'];

if (str !== 'abcd' || !arr.includes('bar')) console.log('This is WRONG.');
//      false      OR        true             OUTPUTS                true

if (str !== 'abcd' && !arr.includes('bar')) console.log('This is correct (you won\'t see this).');
//      false      AND       true             OUTPUTS                false

【讨论】:

    【解决方案2】:

    尝试将条件放在单独的函数中 return true/false 并检查其输出将是什么。检查条件的哪一部分没有正常工作可能会更容易。

    【讨论】:

      【解决方案3】:

      你必须把你的条件改成这个

      if(message.author.id !='329023088517971969' || !message.member.hasPermission(['MANAGE_GUILD'])) return message.channel.send(`You don't have the permissions to do this!`)
      

      请注意您的 !message.author.id =='329023088517971969' 不正确,因此您需要使用 message.author.id !='329023088517971969;' 进行更改

      【讨论】:

      • 我没有公会的管理公会权限,所以它必须依赖我的ID。此代码总是为我返回
      【解决方案4】:

      我不确定我是否理解了这个问题,但听起来下面的代码就足够了。我建议避免使用否定条件,因为它很难阅读。

      boolean authorHasNotPermission = message.author.id !== '329023088517971969';
      boolean memberHasNotPermission = !message.member.hasPermission(['MANAGE_GUILD']);
      
      if (authorHasNotPermission || memberHasNotPermission) {
         return message.channel.send(`You don't have the permissions to do this!`)
      }
      

      【讨论】:

        猜你喜欢
        • 2012-08-06
        • 1970-01-01
        • 2018-07-29
        • 1970-01-01
        • 2010-09-30
        • 2019-06-21
        • 2021-10-02
        • 2021-04-10
        • 2021-02-14
        相关资源
        最近更新 更多