【问题标题】:Command for roles in discord.pydiscord.py 中的角色命令
【发布时间】:2021-02-07 14:36:50
【问题描述】:

我正在尝试为我的 discord 机器人添加一个验证系统,其中一个执行 c.verify 以获得 Verified 角色,但我在分配角色时遇到了问题,并且我发现的所有内容都不起作用。这就是我所拥有的:

    @commands.command(pass_context=True)
    async def verify(self, ctx):
        role = get(ctx.author.roles, name="TOS Verified")
        if ctx.channel.id == 769016529450303519:
            await ctx.message.delete()
            await ctx.send(f"{ctx.author.mention} has been verified.")
            sleep(1)
            await ctx.channel.purge(limit = 1)
            await bot.add_roles(ctx.message.author, role)
        else:
            await ctx.message.delete()
            await ctx.send("You can not verify here.")

我得到的错误是“机器人没有属性 add_roles”。一切正常,直到必须添加角色,但我不知道为什么我不能添加角色。

【问题讨论】:

    标签: python discord discord.py


    【解决方案1】:

    好吧,让我们从头开始,

    role = get(ctx.author.roles, name="TOS Verified")
    

    在这里,您正试图从运行该命令的人那里获得一个名为TOS Verified 的角色,这是没有意义的,因为如果他们试图验证他们将不会拥有该角色。我们实际上想从公会的角色中得到这个,如下所示:

    role = get(ctx.guild.roles, name="TOS Verified")
    

    第二个问题

    await bot.add_roles(ctx.message.author, role)
    

    Bot 没有add_roles 属性,查看文档告诉我们它实际上是一个成员方法。在您的情况下,您的 Member 对象将是 ctx.author

    await ctx.author.add_roles(role)
    

    【讨论】:

    • 我已经这样做了,现在我得到的错误是“未知角色”有没有办法可以通过 ID 找到角色?我认为这是一个问题,因为它没有正确抓住角色,因为我只是给它起了名字,即使我将它复制/粘贴到代码中。
    • 可以使用get_role,role = ctx.guild.get_role(role_id)通过ID获取角色
    猜你喜欢
    • 2021-07-06
    • 2021-04-09
    • 2020-09-26
    • 2021-07-29
    • 1970-01-01
    • 2021-07-17
    • 2022-01-10
    • 2021-10-03
    • 1970-01-01
    相关资源
    最近更新 更多