为了账号安全,请及时绑定邮箱和手机立即绑定

Discord.py 为用户添加角色

Discord.py 为用户添加角色

慕森王 2021-12-26 15:04:27
我是 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.
查看完整描述

2 回答

?
呼如林

TA贡献1798条经验 获得超3个赞

对于重写版本,事情发生了一些变化,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版本的小更新。


查看完整回答
反对 回复 2021-12-26
?
开心每一天1111

TA贡献1836条经验 获得超13个赞

取下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)


查看完整回答
反对 回复 2021-12-26
  • 2 回答
  • 0 关注
  • 121 浏览
慕课专栏
更多

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信