使 discord.js 命令仅对所有者可用

时间:2021-03-11 01:26:33

标签: node.js discord discord.js

我目前正在我的不和谐机器人上使用 say 命令,但想让它只对我可用。正常命令运行良好,但一旦我插入“if (message.author.id !== '411308845714112513') return;”那么它不起作用。这是下面的原始代码以供参考,我是从这里的另一篇文章中得到的,我已经对其进行了测试,效果很好:

const Discord = require("discord.js");

module.exports = {
  name: "say",
  description: "Send a message via the bot",
 async execute(message, args){

    const sayMessage = args.join(" ");
    message.delete().catch(O_o => {});
    message.channel.send(`${sayMessage}`);
  }
}

3 个答案:

答案 0 :(得分:0)

这就是我为我的机器人做的方式。第一种方式使用权限,第二种方式使用角色。

权限:

 if(!message.member.hasPermission("BAN_MEMBERS")) return message.reply("I cannot let you do that");

角色:

        if(message.member.roles.cache.has('758292763946123295')){
 
        }

答案 1 :(得分:0)

你所做的完全正确。包含 if(message.author.id!=='yourid') return; 应该管用。确保您输入了正确的 ID。还要确保此条件在命令处理程序本身之前。

答案 2 :(得分:0)

当我这样做时,我通常使用 if else。例如:

if (message.author.id === 'your id here') {
//only executes if the author is you
} else {
//only executes if the author is not you
}