-1

I'm trying to create a command for my Discord bot: When someone runs -profile @user, it will display their username and id, and if they don't mention a user, it will show their own username and id. However, when I try to do this, I get this error: 'NoneType' object has no attribute 'display_name'

Here is my code:

@bot.command(name = "profile", help = "View someone's profile")
async def profile(ctx, user: discord.User = None):
    colorOne = random.randint(0, 255)
    colorTwo = random.randint(0, 255)
    colorThree = random.randint(0, 255) 
    
    ownProfileEmbed = discord.Embed(
      title = str(ctx.message.author.display_name) + "'s profile'", description = "**Username:** " + str(ctx.message.author) + "\n" + "**User ID:** " + str(ctx.message.author.id), color = discord.Colour.from_rgb(colorOne, colorTwo, colorThree))

    otherProfileEmbed = discord.Embed(
      title = str(user.display_name) + "'s profile", description = "**Username:** " + str(user) + "\n" + "**User ID:** " + str(user.id), color = discord.Colour.from_rgb(colorOne, colorTwo, colorThree))
    
    if (user is None):
      await ctx.send(embed = ownProfileEmbed)
    else:
      await ctx.send(embed = otherProfileEmbed)
BlazingLeaf12
  • 190
  • 2
  • 3
  • 11
  • Is this helping you? [Get someone's avatar and use it on my profile discord.py](https://stackoverflow.com/questions/66551181/get-someones-avatar-and-use-it-on-my-profile-discord-py/66551764#66551764) – Dominik Sep 16 '21 at 10:53
  • Thanks, this solution works. I set user to the message author if a user wasn't mentioned, instead of using a different embed. – BlazingLeaf12 Sep 16 '21 at 12:11

1 Answers1

0

You are getting this error because you are trying to add a display_name attribute to a NoneType object.

To be more specific, you are not checking if the user variable is None until the very end, so when you use user.display_name when you do not specify a user, you are trying to add that attribute to a NoneType object.

Adding something like this at the beginning of your command should fix it:

if not user:
    user = ctx.author
ConchDev
  • 79
  • 7