Reputation: 9
I have been trying to make a bot using python for discord. This bot adds the member to another room depending in the room s/he is in. When I tried to run the code I keep getting this error
AttributeError: 'NoneType' object has no attribute 'roles'
This is the definition where I get the error
def get_sibling_role(member):
roles = member.roles; ret = None #<==there is an issue on this
for role in roles:
if role.name == "Brothers Waiting Room":
ret = ("Brother", role); break
elif role.name == "Sisters Waiting Room":
ret = ("Sister", role); break
return ret
#I already have member defined which returns the member ID
Can anyone help me find out the issue in my definition?
Upvotes: 1
Views: 3249
Reputation: 9
The NoneType
issue is an error where many programmers face discord.py for a few main reasons
1- My own mistake in the previous problem was outside the given code. The code that I wrote was correct, however, I had two client instances defined in the same code, which made the whole program get confused, which returned none for members and for roles, so modify your discord.Client or commands.Bot constructor.
client = discord.Client(intents=intents)
if you want to test and debug, I would advise printing the first member in the server by using the following code under the on_ready():
function.
print (guild.members[1])
2- The second common mistake is that people tend to not use intents, they have to use intents to get members https://discordpy.readthedocs.io/en/latest/intents.html also, you should enable them (Privileged Gateway) as explained in the previous doc in the discord portal https://discord.com/developers/applications
3- Update your discord.py to version 1.5.0. or higher. Make sure you're installing it to the correct environment
Upvotes: 0
Reputation: 332
The obvious reason is that value of member is None. To avoid that you can add a check for "not None". I am also assuming that the misalignment is because of posting the code in question rather than in the code.
def get_sibling_role(member):
if member is None:
return None
roles = member.roles; ret = None #<==there is an issue on this
for role in roles:
if role.name == "Brothers Waiting Room":
ret = ("Brother", role); break
elif role.name == "Sisters Waiting Room":
ret = ("Sister", role); break
return ret
Upvotes: 1