繁体   English   中英

Discord.py 为用户添加角色

[英]Discord.py add role to user

我是 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.

对于重写版本,事情已经改变了一点点, 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版本的小更新。

删除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)

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM