'Command for posting user's avatar
@bot.command(aliases=['av'])
async def avatar(ctx, *, avamember : discord.Member = None):
userAvatarUrl = avamember.avatar_url
await ctx.send(userAvatarUrl)
I would like the command to post the mentioned user's avatar. If no user is mentioned, it should post my own avatar.
Solution 1:[1]
As the previous answer says, you need to put an if statement if you didn't mention the user, but I updated the code to something good (like put it into an embed to make it more prettier):
@bot.command(aliases = ['av'])
async def avatar(ctx, member : discord.Member = None):
if member == None:
member = ctx.author # It will show your avatar if you didn't mention a user (if statement)
embed = discord.Embed(title = f"{member.name}'s avatar", color = 0x2F3136)
em.set_image(url = member.avatar_url) # Shows the avatar
em.set_footer(text = f'Requested by {ctx.author}', icon_url = ctx.author.avatar_url)
await ctx.send(embed = em)
Thank me later :D
Solution 2:[2]
You can check if member
is None
, then use the avatar of the ctx.author
:
@bot.command(aliases=['av'])
async def avatar(ctx, *, member : discord.Member = None):
if member == None:
user_avatar_url = ctx.author.avatar_url
else:
user_avatar_url = member.avatar_url
await ctx.send(user_avatar_url)
Solution 3:[3]
@bot.command(aliases=["av"])
async def avatar(ctx, *, member: discord.Member = None):
if member!=None:
pfp = member.avatar_url
embed = discord.Embed(title=f"{member.name}'s profile picture!")
embed.set_image(url=pfp)
await ctx.send(embed=embed)
else:
author = ctx.message.author
pfp = author.avatar_url
embed = discord.Embed(title=f"{author.name}'s profile picture!")
embed.set_image(url=pfp)
await ctx.send(embed=embed)
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 | Pixie |
Solution 2 | cbrxyz |
Solution 3 |