0

I'm coding a bot that will give a person 2 roles, one called 'Playing' and the other dependant on their current game. Right now, I can't figure out to add multiple roles at once. I'm using Minecraft as an example in this case. Here's the related code:

    totestafter = str(memberafter.game)
    print(totestafter)
    totestbefore = str(memberbefore.game)
    print(totestbefore)
    playing = discord.utils.get(memberafter.server.roles, name="Playing")
    if "Minecraft" in totestafter:
      print('if2 success')
      mcrole = discord.utils.get(memberafter.server.roles, name="Minecraft")
      addroles = [mcrole, playing]
      await client.add_roles(memberafter, addroles)
    elif "Minecraft" in totestbefore:
      mcrole = discord.utils.get(memberafter.server.roles, name="Minecraft")
      await client.remove_roles(memberafter, mcrole)
      await client.remove_roles(memberafter, playing)
      print("Removal Success")

And here's my error: AttributeError: 'list' object as no attribute 'id'

I get that it's for the list and should be an argument, but i'm still fairly new and can't figure this out.

iPlay G
  • 31
  • 1
  • 10

1 Answers1

2

The following is an excerpt of add_role()'s documentation:

add_roles(member, *roles)

This function is a coroutine. Gives the specified Member a number of Role's. You must have the proper permissions to use this function. The Member object is not directly modified afterwards until the corresponding WebSocket event is received. Parameters:

  • member (Member) – The member to give roles to.
  • *roles – An argument list of Role s to give the member

The documentation itself is a bit misleading, since it mentions the word, list. In fact, the * means that this parameter will take all of the leftover arguments and stores them into a list.

Therefore, your add_roles command needs to be changed to:

await client.add_roles(memberafter, mcrole, playing)

Or if you wanted to keep the roles in a list, just add an asterisk (*):

addroles = [mcrole, playing]
await client.add_roles(memberafter, *addroles)

Side note, like add_roles(), remove_roles() works almost the same, it removes role instead of adding them.

remove_roles(member, *roles)

Meaning that you can do remove multiple roles in one command as well:

await client.remove_roles(memberafter, mcrole, playing)

Your full code snippet should look like this:

playing = discord.utils.get(memberafter.server.roles, name="Playing")
mcrole = discord.utils.get(memberafter.server.roles, name="Minecraft")

if "Minecraft" in totestafter:     
  await client.add_roles(memberafter, mcrole, playing)
elif "Minecraft" in totestbefore:
  await client.remove_roles(memberafter, mcrole, playing)
Taku
  • 31,927
  • 11
  • 74
  • 85