【问题标题】:Discord.py add role to userDiscord.py 为用户添加角色
【发布时间】:2021-02-25 16:08:45
【问题描述】:

我是 python 新手,一般都在创建不和谐的机器人,但我终其一生都无法弄清楚如何让我的机器人根据用户的请求为用户分配角色。

我已经在互联网上搜索了几个小时,并找到了一些示例,但它们都产生了错误。

这是我的命令代码:

@client.command(pass_context=True)
@commands.has_role("Bots")
async def add_bot(ctx):
    member = ctx.message.author
    role = discord.utils.get(member.server.roles, name="Bots")
    await client.add_roles(member, role)

这是我得到的错误:

in _verify_checks raise CheckFailure('The check functions for command {0.qualified_name} failed.'.format(self))
discord.ext.commands.errors.CheckFailure: The check functions for command add_bot failed.

【问题讨论】:

    标签: python bots discord.py


    【解决方案1】:

    对于重写版本,事情发生了一些变化,add_roles 不再是 client 的一部分,而是 discord.Member 类的一部分,因此 discord.py 重写版本的代码是:

    @client.command(pass_context=True)
    async def add_role(ctx):
        member = ctx.author
        role = discord.utils.get(member.guild.roles, name="Bots")
        await member.add_roles(role)
    

    REWRITE 版本的小更新。

    【讨论】:

      【解决方案2】:

      去掉has_role 勾选。检查调用者是否具有角色以便他们可以为自己分配该角色是没有意义的。

      @client.command(pass_context=True)
      async def add_bot(ctx):
          member = ctx.message.author
          role = discord.utils.get(member.server.roles, name="Bots")
          await client.add_roles(member, role)
      

      【讨论】:

      • 谢谢帕特里克..我也试过了然后我得到另一个错误......明天会确定它是什么并发布它。
      • 如果我没记错的话,它会说我没有权限,尽管我的机器人拥有 Discord 上的所有权限,但我会确保。
      • 试过你说的,然后我明白了:命令引发异常:禁止:禁止(状态代码:403):缺少权限
      • 刚刚想通了!事实证明,我的机器人角色在不和谐的角色层次结构中必须高于我想要分配的角色。感谢您的帮助!
      猜你喜欢
      • 2019-08-11
      • 1970-01-01
      • 2022-11-24
      • 2021-07-06
      • 2021-09-21
      • 2021-07-26
      • 2021-03-26
      • 2019-02-12
      • 1970-01-01
      相关资源
      最近更新 更多