【问题标题】:discord.js discord owner command only仅 discord.js 不和谐所有者命令
【发布时间】:2021-08-05 12:53:41
【问题描述】:

我正在尝试创建一个只能由不和谐机器人所有者使用的命令。如果我使用下面应该可以工作的代码,它就会崩溃。

所有推荐都有自己的文件,以保持更简洁。

module.exports = {
  name: 'pi',
  aliases: [],
  category: 'beta',
  utilisation: '{prefix}pi',
]
    [const userID = '<@654353673513861130>'

    if(!message.author === userID)
    execute(_client, message) {
        message.channel.send(`yooo`);
    },
};

【问题讨论】:

  • 您能否重新格式化您的代码以使其更清晰

标签: javascript node.js discord discord.js


【解决方案1】:

我注意到的一个错误是您提供了带有 作为字符串的 ID,将 const 替换为此

const userID = '654353673513861130';

如果您对为命令制作单独的文件感到困惑,请阅读this doc

【讨论】:

    【解决方案2】:

    您不能在 if 语句中包装对象方法(在本例中为 execute)。 if 语句应该在该方法本身内。

    如果您知道 discord bot 的所有者 ID(654353673513861130,而不是 &lt;@654353673513861130&gt;),您可以检查它是否与消息作者的 ID 相同。如果不一样,可以提前通过returning 停止执行命令。仅当作者是机器人所有者时,此 return 语句之后的任何内容才会运行。检查下面的代码:

    module.exports = {
      name: 'pi',
      aliases: [],
      category: 'beta',
      utilisation: '{prefix}pi',
      execute(_client, message) {
        const ownerID = '654353673513861130';
    
        // if the ID is NOT the same, return early to exit
        if (message.author.id !== ownerID) return;
    
        // rest of the code is only run if the author is the bot owner
        message.channel.send(
          `I know you're the owner, ${message.author}. I mean, I know that your discord ID is ${ownerID}`,
        );
      },
    };
    

    【讨论】:

      猜你喜欢
      • 2021-06-09
      • 1970-01-01
      • 2022-01-06
      • 2020-05-10
      • 1970-01-01
      • 2021-06-20
      • 2019-03-05
      • 1970-01-01
      • 2021-12-28
      相关资源
      最近更新 更多