1

I start creating a Discord bot with the Python API of Discord. So, I tried to make a command that give roles on a discord server. Here is my code :

client = commands.Bot(command_prefix = '/')

@client.command()
async def addrole(ctx, role: discord.Role, user: discord.Member):
    if ctx.author.guild_permissions.administrator:
        await user.add_roles(role)
        await ctx.send(f"Successfully given {role.mention} to {user.mention}.")

When I try to use the command, any error happear. I try to make like the websearch I've done but still don't work. Can someone help me ?

Leonardo Scotti
  • 1,069
  • 8
  • 21
Zratey
  • 11
  • 2
  • What kind of error appear? This could be an intents issue. Have a look at [this answer](https://stackoverflow.com/a/64832812/14196628). Another thing is to make sure your bot isn't trying to give a role that is higher than it's own role. That wont work either. – AbdurJ Jan 25 '21 at 14:36
  • Please provide the error, also, what is 'role'? – Codeize Jan 25 '21 at 22:20

1 Answers1

0

I don't believe basic things like these would require Discords intents, especially the "member intent". What I have done is I've cleaned up your code and required a member to be passed before a multiple string role, if needed.

@client.command()
@commands.has_permissions(manage_roles=True)
async def addrole(ctx, member: discord.Member = None, *, role: discord.Role = None):
        if role == None:
                await ctx.send(f'Provide a role to add')
                return

        if member == None:
                await ctx.send(f'Provide a member to add a role')
                return

        await member.add_roles(role)
        await ctx.send(f"Successfully added role, {role} to {member.name}")

Cohen
  • 2,375
  • 3
  • 16
  • 35