Discord.js 权限

Posted

技术标签:

【中文标题】Discord.js 权限【英文标题】:Discord.js permission 【发布时间】:2020-12-25 11:12:23 【问题描述】:

我正在使用 discord.js 模块制作一个不和谐机器人,并且我打算制作这些功能:

    踢(和阻止)用户 设置用户昵称 管理渠道 分配和创建角色 编辑和创建频道

我想将这些(第 1、2、4 节)功能限制为服务器版主或管理员。我应该使用什么权限位标志?目前我正在使用1475677686

这是我的代码

Kick Command:

if(!message.member.hasPermission("ADMINISTRATOR"))
  return message.reply("You don't have permission to do that.");


if(message.mentions.users.size === 0)
  return message.reply("Usage: `_kick [mention]`");


message.mentions.members.forEach((member) => 
  member.ban(
    reason: "Banned by admin",
    days: 7
  ).then((value) => 
    message.reply("Succeed to kick member");
  ).catch((error) => 
    message.reply("Internal bot error");
    console.error(error);
  );
);

Nickname Command:

if(!message.member.hasPermission("ADMINISTRATOR"))
  return message.reply("You don't have permission to do that.");


var nickname = message.content.replace("_nickname ", "");

if(nickname.trim() === "")
  return message.reply("Enter the nickname.");


if(message.mentions.users.size === 0)
  return message.reply("Usage: `_nickname [mention] [nick]`");


message.mentions.members.forEach((member) => 
  nickname = nickname.replace("<@!" + member.id + "> ", "");
  member.setNickname(nickname).then((value) => 
    message.reply("Succeed to change nickname");
  ).catch((error) => 
    message.reply("Internal bot error");
    console.error(error);
  );
);

【问题讨论】:

【参考方案1】:

GuildMember 类具有成员函数hasPermission(),如果成员具有该权限,则返回 true 值,否则返回 false。您可以查看 discord.js 文档以获取来自 here 的附加参数。您可以检查可以作为参数传递的可用标志 hasPermissions() here 用例:

client.on("message", msg=>
    if(!msg.member.hasPermission("MANAGE_ROLES")) return;
    // It returns the function if the member doesn't have the permission and doesn't execute the code below from here.

如果你想检查特定的角色,那么你可以使用这个

member.roles.cache.has('role-id-here');
// returns true if the member has the role

查看 discord.js 指南以获得更深入的解释here

【讨论】:

【参考方案2】:

首先,您的权限位似乎有点缺陷,您错过了MANAGE_NICKNAMES,这是机器人能够更改其他成员昵称所必需的。尝试使用号码1609895414

现在,你说:

另外,我想让担任经理角色的人能够执行所有这些操作。 (第 1、2、4 节)

这取决于您如何开发代码,例如限制没有权限的人并检查它并确保机器人具有权限,因为服务器管理员可以删除您的机器人的预设权限,这将引发错误你的机器人不会响应。为此,我们需要开发一个错误处理程序。

然后,您需要查看您的机器人与其他用户相比的排名,然后您将检查机器人是否有权这样做,如果没有,则使用错误处理程序进行响应。错误处理程序看起来像这样:

try 
  // Some code
 catch (error) 
  console.log(error);

<>.catch(error => console.log(error));

完成后,您可以对代码进行编程以匹配所需的结果。 有关权限和检查权限的更多信息,请尝试访问这些指南:

Discordjs.guide - Guide - Handling Permissions Discordjs.guide - Guide - Permissions Discord.js.org - Docs - GuildMember property (as used for checking and applying permissions)

我认为这是您的事件message 的处理方式,所以我的建议是:

client.on("message", async message =>  // Use `async` it's highly recommended.

  // If member doesn't have the Manage Roles permission, return, or you can use else to make a custom reply.
  // Also check if the user is an administrator, this way it doesn't need all those permissions.
  // You can require Manage Server and or other Permissions as well.
  if (!message.member.permissions.has("MANAGE_SERVER") && !clientMember.permissions.has("ADMINISTRATOR")) return;
  
  // Gets the `member` property of the `client`.
  const clientMember = message.guild.members.cache.get(client.user.id);

  // All the permissions you requested that you needed.
  // These are what's absolutely neccesary as you described.
  // The IDs of the permissions matches with their name.
  const requiredPermissions = ["MANAGE_ROLES", "MANAGE_NICKNAMES", "MANAGE_CHANNELS", "KICK_MEMBERS", "BAN_MEMBERS", "READ_MESSAGES", "SEND_MESSAGES"];
  
  // Check if the client has these permissions, check as well if the client is an administrator, this way the bot doesn't necessarily need all the permissions.
  if (!clientMember.permissions.has(requiredPermissions) && !clientMember.permissions.has("ADMINISTRATOR")) return;

  // Run your desired code after all these requirements are fulfilled.

以上是如何构建代码的示例,要回答您有关代码的问题,请尝试以下代码:

client.on("message", async message => 
  const kickPermissions = ["KICK_MEMBERS"]

  if(!message.member.permissions.has("ADMINISTRATOR") && !message.member.permissions.has(kickPermissions))
    return message.reply("You don't have permission to do that.");
  

  const clientMember = message.guild.members.cache.get(client.user.id);

  if(!clientMember.permissions.has("ADMINISTRATOR") && !clientMember.permissions.has(kickPermissions))
    return message.reply("The bot doesn't have permission to do that.");
  

  if(!message.mentions.users.length)
     return message.reply("Usage: `_kick [mention]`");
  

  message.mentions.members.forEach((member) => 
    member.kick(
      reason: "Kicked by admin"
    ).then((value) => 
      message.reply("Succeed to kick member");
    ).catch((error) => 
      message.reply("Internal bot error");
      console.error(error);
    );
  );
  
  const banPermissions = ["BAN_MEMBERS"]

  if(!message.member.permissions.has("ADMINISTRATOR") && !message.member.permissions.has(banPermissions))
    return message.reply("You don't have permission to do that.");
  

  const clientMember = message.guild.members.cache.get(client.user.id);

  if(!clientMember.permissions.has("ADMINISTRATOR") && !clientMember.permissions.has(banPermissions))
    return message.reply("The bot doesn't have permission to do that.");
  

  if(!message.mentions.users.length)
     return message.reply("Usage: `_ban [mention]`");
  

  message.mentions.members.forEach((member) => 
    member.ban(
      reason: "Banned by admin",
      days: 7
    ).then((value) => 
      message.reply("Succeed to ban member");
    ).catch((error) => 
      message.reply("Internal bot error");
      console.error(error);
    );
  );

  const nicknamePermissions = ["MANAGE_ROLES", "MANAGE_NICKNAMES"]

  if(!message.member.permissions.has("ADMINISTRATOR") && !message.member.permissions.has(nicknamePermissions))
    return message.reply("You don't have permission to do that.");
  

  const clientMember = message.guild.members.cache.get(client.user.id);

  if(!clientMember.permissions.has("ADMINISTRATOR") && !clientMember.permissions.has(nicknamePermissions))
    return message.reply("The bot doesn't have permission to do that.");
  

  if(!message.mentions.users.length)
     return message.reply("Usage: `_nickname [mention]`");
  

  var nickname = message.content.replace("_nickname ", "");

  if(nickname.trim() === "")
    return message.reply("Enter the nickname.");
  

  message.mentions.members.forEach((member) => 
    nickname = nickname.replace("<@!" + member.id + "> ", "");
    member.setNickname(nickname).then((value) => 
      message.reply("Succeed to change nickname");
    ).catch((error) => 
      message.reply("Internal bot error");
      console.error(error);
     );
   );
);

要了解有关踢出和禁止成员或设置他们的昵称的更多信息,请尝试查看以下指南:

Discordjs.guide - Guide - Banning a User Discordjs.guide - Guide - Kicking a User Change user nickname with discord.js

【讨论】:

以上是关于Discord.js 权限的主要内容,如果未能解决你的问题,请参考以下文章

Discord.js API 中的权限?

discord.js - 有角色授予权限

.send() 上的 discord.js 缺少权限错误

Discord.js 斜线命令权限系统不起作用

(discord.js) 检查机器人是不是有权限不起作用

具有用户权限、客户端权限和限制的 Discord.js 命令处理程序