Nekidev
Nekidev

Reputation: 77

Discord.py add roles

I am trying to add roles in discord.py but I can't really make it work.

@bot.command(brief="Report member")
async def member(ctx):
  if ctx.author.id != 783430063076147210:
    await ctx.send("Mention the member you want to report")

    message = await bot.wait_for('message', check=lambda message: ctx.author == ctx.author)
    msg = message.content.replace("<","")
    msg = msg.replace(">","")
    msg = msg.replace("@","")
    msg = msg.replace("!","")

    #try:
    msg = int(msg)
    user = bot.get_user(msg)
    await ctx.send('Are you sure you want to report ' + user.mention + "?")

    message = await bot.wait_for('message', check=lambda message: ctx.author == ctx.author)

    if message.content.lower() == "yes" or message.content.lower() == "y":
      member = ctx.message.author
      role = get(member.guild.roles, name="Reported")
      user = ctx.guild.get_member(msg)
      await bot.add_roles(user, role)
      await ctx.send(user.mention + " was reported by " + member.mention + ".")
    else:
      await ctx.send("User was not reported!")
    #except:
      await ctx.send("Oops! That was not a user!")

I'm getting an AttributeError: 'Bot' object has no attribute 'add_roles', and the same with the other way

await user.add_roles(role)

returns AttributeError: 'NoneType' object has no attribute 'add_roles'

Is there something I'm missing?

Upvotes: 2

Views: 741

Answers (5)

FoxyCoder
FoxyCoder

Reputation: 1

Your line

await bot.add_roles(user, role)

needs to be replaced by

await user.add_roles(role)

Upvotes: 0

minecraftpr03
minecraftpr03

Reputation: 56

Two ways of making this work:

1.

@bot.command()
async def role(ctx, member:commands.MemberConverter, role:commands.RoleConverter):
    await member.add_roles(role)

^^ This method uses a role converter that accepts role ID, name, or mention. I also use commands.MemberConverter because it accepts more methods of referencing a user than discord.Member, and the same thing for discord.Role.

2.

@bot.command()
async def role(ctx, member:commands.MemberConverter, role):
    role = discord.utils.get(ctx.guild.roles, name=role)
    await member.add_roles(role)

^^ This method uses discord.utils.get to get a guild role. From my knowledge, it only accepts role names and not role IDs or mentions.

Upvotes: 3

NOT kar1m yt
NOT kar1m yt

Reputation: 198

these are my addrole and remouverole commands:

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

@bot.command()
@commands.has_permissions(administrator=True)
async def removerole(ctx, member : discord.Member, role : discord.Role):
    await member.remove_roles(role) 
    await ctx.send(f"{role} is removed from {member}.")

Upvotes: -1

šeki
šeki

Reputation: 153

Try something like this:

@bot.command()
async def role(ctx):
    test = ctx.author
    role = discord.utils.get(test.guild.roles, name="Name")
    if role in test.roles:
        await ctx.send("you already have a role")
    else:
        await test.add_roles(role)

Upvotes: 0

pjones123
pjones123

Reputation: 396

bot.get_user() will return a User object, to which you cannot add roles, as no guild is specified. You could do ctx.guild.get_member(id) to get the Member object then add roles to that, but you'd be better of getting the member from the mentions attribute of a Message. Docs: https://discordpy.readthedocs.io/en/latest/api.html?highlight=mentions#discord.Message.mentions

Upvotes: 0

Related Questions