'NoneType' object has no attribute 'avatar_url' in discord.py

@client.command()
async def avatar(self, ctx, *,  avamember : discord.Member=None):
    userAvatarUrl = avamember.avatar_url
    await ctx.send(userAvatarUrl)

Hi, the code above is what I'm using in discord.py. I'm trying to display the avatar, but it won't work. What I get instead is: AttributeError: 'NoneType' object has no attribute 'avatar_url'.

What is wrong here? How can I fix it?


Solution 1:

You set avamember to None in the function declaration and then tried to access an attribute. Since avamember is None, it has no such attribute. To fix this, get an actual discord.Member object.

Edit: I'm assuming that object in fact has the requested attribute.

Solution 2:

It's probably because you invoked your command wrong. To get the discord.Member object you have to mention the user, otherwise, it will return None.

Delete the * and self (unless it's in a cog). In this case discord.Member=None is kinda useless too (unless you want to use it to check if the command was used correctly).

@client.command()
async def avatar(ctx, avamember : discord.Member):
    userAvatarUrl = avamember.avatar_url
    await ctx.send(userAvatarUrl)

Then use your command properly:

screenshot image


If your command is in the Cog:

  1. Change @client.command() to @commands.command()
  2. Add self before ctx as you had.