Discord.py add role to user

 ̄綄美尐妖づ 提交于 2020-06-18 05:58:52

问题


I am new to python and creating discord bots in general and I can't for the life of me figure out how to make my bot assign a role to a user upon the users request.

I have scoured the internet for hours on end and have found a few examples but they all produce and error.

Here is the code I have for the command:

@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)

Here is the error I get:

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.

回答1:


Remove the has_role check. It doesn't make sense to check if the caller has a role so they can assign themselves that 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)



回答2:


For the rewrite version things have changed a little bit, add_roles is no more part of client but part of the discord.Member class therefor the code for the discord.py rewrite version is:

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

A minor update for the REWRITE version.



来源:https://stackoverflow.com/questions/55478282/discord-py-add-role-to-user

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!