【问题标题】:Checking if my bot has permissions to manage roles检查我的机器人是否有权管理角色
【发布时间】:2018-12-20 09:59:22
【问题描述】:

所以,标题简要说明了我想要什么。

当成员加入我的 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 帐户加入,它在控制台中报告 hasPermission 不是函数,然后停止。

对于如何解决此代码有什么建议吗?

【问题讨论】:

  • 你在哪里找到了 hasPermission 方法?我在文档中找不到它。所以hasPermission不是函数是正确的

标签: javascript discord discord.js


【解决方案1】:

错误信息是正确的,用户没有名为hasPermission 的函数。但角色属性确实如此。

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

【讨论】:

  • 啊,我明白了。我写的 if 语句是指机器人用户,而不是角色。我想你可能刚刚在电脑显示器前目睹了官方最大的白痴。
  • 这行不通,因为用户对象没有角色。详情见我的回答。
  • @André 这正是我所说的......我没有说用户有角色
【解决方案2】:

在 Discord.js 上,我们有用户成员。用户是 Discord 的用户。成员是 Discord 服务器的成员。用户对象没有关于角色、昵称、权限的信息。成员会这样做,因为他们与该服务器相关。
所以如果你想把机器人作为公会的成员,你需要使用这样的东西:

<guild>.me

这将从所选公会返回机器人的成员对象。
将成员添加到公会时,您将获得 GuildMember 对象,并且您也将获得公会。所以你可以使用这个:

member.guild.me

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

if(member.guild.me.hasPermission("MANAGE_ROLES"))

注意:

hasPermission()hasPermissions() 将在 DiscordJS v13 中被弃用并替换为 permissions.has()

例如:member.permissions.has(Permissions.FLAGS.SEND_MESSAGES);

【讨论】:

    猜你喜欢
    • 2020-09-14
    • 2018-04-02
    • 2020-10-03
    • 2020-12-22
    • 2020-12-02
    • 2020-09-21
    • 2021-02-11
    • 2021-04-24
    • 2018-01-01
    相关资源
    最近更新 更多