0
votes

So I tried to make an add role command for my bot:

@bot.command()
async def addrole(ctx, member: discord.Member, role: discord.Role):
    await member.add_roles(role)
    await ctx.send(f'{member} now has the {role} role')

but it did not work, the bot didn't add the role or send the message, no errors either, is there something I did wrong?

1
Everything looks fine, what intents have you enabled?Łukasz Kwieciński
I can also confirm that the code works. So make sure the members name has the same caps as it is case sensitive and as Lukasz mentioned you might need to enable intents. See my answer here on how to do that stackoverflow.com/a/65356391/13151806Lemon.py
I have the members and presences intents enabled, both in the dev portal and codehuss_a
Is the member name entered case sensitive? For example, if my name on discord is Lemonpy and I type lemonpy for the member name it won't find me and the code won't work.Lemon.py
I'm mentioning the member when testinghuss_a

1 Answers

1
votes

Sorry for being late on this but the answer is simple. Your code is fine just when typing the command use

(prefix) @user mention @role

Ex. !addrole @huss_a @example

I would also recommended putting @commands.has_permissions reference at the beginning so only admins can use this and users can't give themselves any role they want like so:

@bot.command()
@commands.has_permissions(manage_roles=True)
async def addrole(ctx, member: discord.Member, role: discord.Role):
  await member.add_roles(role)
  await ctx.send(f"{member} now had the {role} role")

You can also use this code:

@bot.command()
@commands.has_permissions(manage_roles=True)
async def addrole(ctx, role: discord.Role, user: discord.Member):
    await user.add_roles(role)
    await ctx.send(f"{role.mention} was added to {user.mention})