AttributeError: 'Member' object has no attribute 'avatar_url'
Question:
I am trying to make a Discord bot and one of the features is a welcoming from my bot using on_member_join. This is the event:
@bot.event
async def on_member_join(member, self):
embed = discord.Embed(colour=discord.Colour(0x95efcc), description=f"Welcome to Rebellions server! You are the{len(list(member.guild.members))}th member!")
embed.set_thumbnail(url=f"{member.avatar_url}")
embed.set_author(name=f"{member.name}", icon_url=f"{member.avatar_url}")
embed.set_footer(text=f"{member.guild}", icon_url=f"{member.guild.icon_url}")
embed.timestamp = datetime.datetime.utcnow()
await welcome_channel.send(embed=embed)
Although when the bot is working and running and someone joins my server I get this error:
[2022-11-07 19:38:10] [ERROR ] discord.client: Ignoring exception in on_member_join
Traceback (most recent call last):
File "C:UserssteneAppDataLocalProgramsPythonPython311Libsite-packagesdiscordclient.py", line 409, in _run_event
await coro(*args, **kwargs)
File "C:UserssteneOneDriveDocumentsGitHubbotmain.py", line 25, in on_member_join
embed.set_thumbnail(url=f"{member.avatar_url}")
^^^^^^^^^^^^^^^^^
AttributeError: 'Member' object has no attribute 'avatar_url'
I am running the latest version of discord.py and python.
Thanks!
welcome cog:
import discord
from discord.ext import commands
import asyncio
import datetime
class WelcomeCog(commands.Cog, name="Welcome"):
def __init__(self, bot):
self.bot = bot
@commands.Cog.listener()
async def on_member_join(self, member):
embed = discord.Embed(colour=discord.Colour(0x95efcc), description=f"Welcome to Rebellions server! You are the{len(list(member.guild.members))}th member!")
embed.set_thumbnail(url=member.avatar.url)
embed.set_author(name=member.name, icon_url=member.avatar.url)
embed.set_footer(text=member.guild)
embed.timestamp = datetime.datetime.utcnow()
channel = self.bot.get_channel(1038893507961692290)
await channel.send(embed=embed)
async def setup(bot):
await bot.add_cog(WelcomeCog(bot))
print("Welcome cog has been loaded successfully!")
Answers:
Following your code you have several errors, which can be corrected in the following code:
First: async def on_member_join(member, self):
is an incorrect code.
self
always comes before any other argument if this event is in a cog file, but still it isn’t a required argument so completely remove it. The correct code for this line is async def on_member_join(member):
Second: Make sure you’re using the correct event listener.
@client.event
or @bot.event
if this is in your Main file, and @commands.Cog.listener()
if this is in your cog file.
Third: Please change (member, self):
to (member:discord.Member)
Good luck! 😀
In discord.py 2.0 the attribute Member.avatar_url
got removed and replaced by Member.avatar
. To access the URL, you should use member.avatar.url
.
Check out the migration guide for similar instances. Using pip freeze
you can check which version of discord.py you have installed, but I assume it’s 2.x, and probably you followed a tutorial or copied a code example that used discord.py 1.x.
I am trying to make a Discord bot and one of the features is a welcoming from my bot using on_member_join. This is the event:
@bot.event
async def on_member_join(member, self):
embed = discord.Embed(colour=discord.Colour(0x95efcc), description=f"Welcome to Rebellions server! You are the{len(list(member.guild.members))}th member!")
embed.set_thumbnail(url=f"{member.avatar_url}")
embed.set_author(name=f"{member.name}", icon_url=f"{member.avatar_url}")
embed.set_footer(text=f"{member.guild}", icon_url=f"{member.guild.icon_url}")
embed.timestamp = datetime.datetime.utcnow()
await welcome_channel.send(embed=embed)
Although when the bot is working and running and someone joins my server I get this error:
[2022-11-07 19:38:10] [ERROR ] discord.client: Ignoring exception in on_member_join
Traceback (most recent call last):
File "C:UserssteneAppDataLocalProgramsPythonPython311Libsite-packagesdiscordclient.py", line 409, in _run_event
await coro(*args, **kwargs)
File "C:UserssteneOneDriveDocumentsGitHubbotmain.py", line 25, in on_member_join
embed.set_thumbnail(url=f"{member.avatar_url}")
^^^^^^^^^^^^^^^^^
AttributeError: 'Member' object has no attribute 'avatar_url'
I am running the latest version of discord.py and python.
Thanks!
welcome cog:
import discord
from discord.ext import commands
import asyncio
import datetime
class WelcomeCog(commands.Cog, name="Welcome"):
def __init__(self, bot):
self.bot = bot
@commands.Cog.listener()
async def on_member_join(self, member):
embed = discord.Embed(colour=discord.Colour(0x95efcc), description=f"Welcome to Rebellions server! You are the{len(list(member.guild.members))}th member!")
embed.set_thumbnail(url=member.avatar.url)
embed.set_author(name=member.name, icon_url=member.avatar.url)
embed.set_footer(text=member.guild)
embed.timestamp = datetime.datetime.utcnow()
channel = self.bot.get_channel(1038893507961692290)
await channel.send(embed=embed)
async def setup(bot):
await bot.add_cog(WelcomeCog(bot))
print("Welcome cog has been loaded successfully!")
Following your code you have several errors, which can be corrected in the following code:
First:
async def on_member_join(member, self):
is an incorrect code.
self
always comes before any other argument if this event is in a cog file, but still it isn’t a required argument so completely remove it. The correct code for this line is async def on_member_join(member):
Second: Make sure you’re using the correct event listener.
@client.event
or @bot.event
if this is in your Main file, and @commands.Cog.listener()
if this is in your cog file.
Third: Please change
(member, self):
to(member:discord.Member)
Good luck! 😀
In discord.py 2.0 the attribute Member.avatar_url
got removed and replaced by Member.avatar
. To access the URL, you should use member.avatar.url
.
Check out the migration guide for similar instances. Using pip freeze
you can check which version of discord.py you have installed, but I assume it’s 2.x, and probably you followed a tutorial or copied a code example that used discord.py 1.x.