Stagger
Stagger

Reputation: 115

Discord.py add role to user

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.

Upvotes: 1

Views: 11574

Answers (2)

Oska boska
Oska boska

Reputation: 21

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 member.add_roles(role)

A minor update for the REWRITE version.

Upvotes: 2

Patrick Haugh
Patrick Haugh

Reputation: 60944

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)

Upvotes: 1

Related Questions