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.
The following is an excerpt of add_role()
's documentation:
This function is a coroutine . Gives the specified
Member
a number ofRole
's. You must have the proper permissions to use this function. TheMember
object is not directly modified afterwards until the correspondingWebSocket
event is received. Parameters:
member
(Member
) – The member to give roles to.*roles
– An argument list ofRole
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.
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)
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.