检查我的机器人是否有权管理角色

时间:2018-07-12 08:47:16

标签: javascript discord discord.js

因此,标题简要说明了我想要的内容。

当成员加入我的Discord服务器时,我希望机器人自动为他们分配用户角色。在此之前,我想验证该机器人是否具有管理角色的权限,到目前为止,我的代码中已有以下内容。

 /** Event will trigger when a user joins the server **/
 bot.on("guildMemberAdd", function(member) {
  if (!bot.user.hasPermission("MANAGE_ROLES")) {
    var embed2 = new discord.RichEmbed()
     .setTitle("Error")
     .setDescription("The bot doesn't have the appropriate permissions to assign new members roles automatically.\nTo resolve this problem, go to **Server Settings** and then navigate to the **Roles** option. Next, click on **Bots**, and then click on the slider next to **Manage Roles** to activate it.")
     .setColor("#3937a5")
    bot.channels.get("466878539980079105").send(embed2);
 } else {
  member.addRole(member.guild.roles.find("name", "User"));
 }
});

如果漫游器没有权限,则会通过向#alert频道发布警报来引起工作人员的注意。

但是,我尝试加入一个新的Discord帐户,它在控制台中报告h​​asPermission不是函数,然后停止。

关于如何解析此代码会有任何建议吗?

2 个答案:

答案 0 :(得分:1)

错误消息正确,用户没有名为hasPermission的功能。但是角色属性可以。

https://discord.js.org/#/docs/main/stable/class/Role?scrollTo=hasPermission

答案 1 :(得分:1)

在Discord.js上,我们有用户成员。用户是Discord的用户。成员是Discord服务器的成员。用户对象没有有关角色,昵称,权限的信息。成员这样做,因为它们与该服务器有关。
因此,如果您想将机器人作为公会的成员,则需要使用以下命令:

<guild>.me

这将从选定的公会返回机器人的成员对象。
将成员添加到公会后,您将获得GuildMember对象,并随之获得公会。因此,您可以使用以下代码:

member.guild.me

最后检查机器人是否具有权限:

if(member.guild.me.hasPermission("MANAGE_ROLES"))
相关问题