Stuck on a code while creating a discord bot using python
Question:
this is the first time i am using this site. So, please do point out the mistake I did while writing this question. Thank you!
Actually I am making a discord bot using python. I was planning to use it as a bot which welcomes new user or notify when someone leave and stuff. So when I was writing the code about when the user joins my server the bot will send a embed message to the welcome channel, the embed message show’s some messages and a image. The code is mentioned below.
@bot.event
async def on_member_join(member):
print('|---------------------------------------------------------->')
print(f' New member joined as User: {member.name}, ID: ({member.id})')
print('|---------------------------------------------------------->')
welcome_channel_id = 1190887440433557544
welcome_channel = member.guild.get_channel(welcome_channel_id)
if not welcome_channel:
print('|-------------------------->')
print(' Welcome channel not found.')
print('|-------------------------->')
return
images_folder = r'Z:Spooks [Discord Bot]Images'
image_files = [f for f in os.listdir(images_folder) if f.lower().endswith(('.png', '.jpg', '.jpeg', '.gif', '.bmp', '.webp'))]
if not image_files:
print('|-------------------------------------->')
print(' No valid image is found in the folder.')
print('|-------------------------------------->')
return
random_image_file = random.choice(image_files)
image_url = f'attachment://{random_image_file}'
if welcome_channel:
embed = discord.Embed(
title=f'Welcome to the server, {member.name}!',
description='We are glad to have you here. nWe hope you will find this server enjoyable.',
color=0x3498db
)
embed.add_field(name = 'Rules', value = 'Strictly follow the rules mentioned in the Rules channel.', inline = False)
embed.add_field(name = 'What to do next', value = 'Example 1, nExample 2, nExample 3', inline = False)
embed.set_thumbnail(url = member.user.avatar_url)
embed.set_image(url = image_url)
await welcome_channel.send(embed=embed, file=discord.File(os.path.join(images_folder, random_image_file), filename=random_image_file))
In this code I tried to fetch some random images from a folder which contains images of dimension 320×320 (which is working fine). Then i want the user profile photo shows on top of the image, I tried bunch of different codes which is not working and then i thought i will add the user profile photo as thumbnail so I tried adding this
embed.set_thumbnail(url = member.user.avatar_url)
also I tried this too
embed.set_thumbnail(url = member.avatar_url)
But nothing seems to work, it always gives error similar to this (for the thumbnail part)
AttributeError: 'Member' object has no attribute 'user'. Did you mean: '_user'?
So, I appreciate the help on this topic. Either it give’s user profile photo on top of the image or as a thumbnail, both will work for me. And also I tried googling the image dimension required for embed message it says 320×320 which shows as a square image in the embed (I edited all my images to this dimension). Is it possible to show these image’s in rectangle.
Thanks in advance.
Answers:
As the error explains there is no attribute user or avatar_url, the attribute is avatar.url as explained by the discordpy on_member_join documentation located at https://discordpy.readthedocs.io/en/latest/api.html?highlight=on_member_join#discord.on_member_join
The member object has an attribute avatar which has an attribute url however the member object does not have an attribute called ‘user’ or ‘avatar_url’ according to the docs.
The correct code is
embed.set_thumbnail(url = member.avatar.url)
this is the first time i am using this site. So, please do point out the mistake I did while writing this question. Thank you!
Actually I am making a discord bot using python. I was planning to use it as a bot which welcomes new user or notify when someone leave and stuff. So when I was writing the code about when the user joins my server the bot will send a embed message to the welcome channel, the embed message show’s some messages and a image. The code is mentioned below.
@bot.event
async def on_member_join(member):
print('|---------------------------------------------------------->')
print(f' New member joined as User: {member.name}, ID: ({member.id})')
print('|---------------------------------------------------------->')
welcome_channel_id = 1190887440433557544
welcome_channel = member.guild.get_channel(welcome_channel_id)
if not welcome_channel:
print('|-------------------------->')
print(' Welcome channel not found.')
print('|-------------------------->')
return
images_folder = r'Z:Spooks [Discord Bot]Images'
image_files = [f for f in os.listdir(images_folder) if f.lower().endswith(('.png', '.jpg', '.jpeg', '.gif', '.bmp', '.webp'))]
if not image_files:
print('|-------------------------------------->')
print(' No valid image is found in the folder.')
print('|-------------------------------------->')
return
random_image_file = random.choice(image_files)
image_url = f'attachment://{random_image_file}'
if welcome_channel:
embed = discord.Embed(
title=f'Welcome to the server, {member.name}!',
description='We are glad to have you here. nWe hope you will find this server enjoyable.',
color=0x3498db
)
embed.add_field(name = 'Rules', value = 'Strictly follow the rules mentioned in the Rules channel.', inline = False)
embed.add_field(name = 'What to do next', value = 'Example 1, nExample 2, nExample 3', inline = False)
embed.set_thumbnail(url = member.user.avatar_url)
embed.set_image(url = image_url)
await welcome_channel.send(embed=embed, file=discord.File(os.path.join(images_folder, random_image_file), filename=random_image_file))
In this code I tried to fetch some random images from a folder which contains images of dimension 320×320 (which is working fine). Then i want the user profile photo shows on top of the image, I tried bunch of different codes which is not working and then i thought i will add the user profile photo as thumbnail so I tried adding this
embed.set_thumbnail(url = member.user.avatar_url)
also I tried this too
embed.set_thumbnail(url = member.avatar_url)
But nothing seems to work, it always gives error similar to this (for the thumbnail part)
AttributeError: 'Member' object has no attribute 'user'. Did you mean: '_user'?
So, I appreciate the help on this topic. Either it give’s user profile photo on top of the image or as a thumbnail, both will work for me. And also I tried googling the image dimension required for embed message it says 320×320 which shows as a square image in the embed (I edited all my images to this dimension). Is it possible to show these image’s in rectangle.
Thanks in advance.
As the error explains there is no attribute user or avatar_url, the attribute is avatar.url as explained by the discordpy on_member_join documentation located at https://discordpy.readthedocs.io/en/latest/api.html?highlight=on_member_join#discord.on_member_join
The member object has an attribute avatar which has an attribute url however the member object does not have an attribute called ‘user’ or ‘avatar_url’ according to the docs.
The correct code is
embed.set_thumbnail(url = member.avatar.url)