首页 > 解决方案 > 命令权限

问题描述

我正在寻找开发个人网络机器人,我需要一个简单的“mod”、“admin”和其他审核命令。我已经完成了命令,我只需要弄清楚如何发送错误并在成员没有“admin”、“mod”或其他审核角色时不执行命令。这是我到目前为止所拥有的:

if (!message.member.roles.some(r=>[Tester].includes(r.name)) ) 
return message.channel.send("error_here").catch(console.error);

我希望代码包含一条错误消息:

message.channel.send(message.author + " you do not have permission to use this command!")

我还希望在成员没有“admin”、“mod”或其他审核角色时不执行该命令。

标签: discord.js

解决方案


我认为您走在正确的轨道上,只需添加一些代码即可进行额外验证。此代码检查用户是否具有任何审核角色,如果是,则执行该命令。

// Just some test role names
let modRoles = ['admin', 'moderator', 'helper'];

if (command === '<Some mod/admin command here>') {
  if (!message.member.roles.some(r=>modRoles.includes(r.name))) {
    // User does not have any moderation roles. User is not allowed to execute command
    return message.channel.send(message.author + ' you do not have permission to execute this command!');
      .catch(console.error);
  }

  // User does have a moderation role. User can execute command
  // Write code here for the command
}

推荐阅读