Correct way of displaying a member's mention in an embed
In my discord.py bot I have a function which lets each guild choose their desired welcome message to be displayed by the bot when a users join (using on_member_join). This message is introduced in the database using a command and when a member joins then it is then shown using an embed. The full code is quite complex, to sum up the on_member_join event piece of code to retrieve the message from the database and pass it to the embed is the following:
@commands.Cog.listener()
async def on_member_join(self, member):
# Here goes some code
# ...
result1 = await get_welcome_message(member.guild.id)
if result1[0] is None:
msg = 'Hello {mention}! Welcome to {guild}'
await channel.send(embed=embed_welcome(msg, member))
else:
await channel.send(embed=embed_welcome(str(result1[0]), member))
# Here goes more code
# ...
The embed_welcome function basically creates an embed passing some parameters that can be used in the message:
def embed_welcome(message, member):
'''Sets the embed message specifically for welcome messages.'''
guild = member.guild
user = member.name
mention = member.mention
members = len(list(member.guild.members))
embed = discord.Embed(color=discord.Colour.purple(), description=message.format(members=members, mention=mention, user=user, guild=guild))
embed.set_thumbnail(url=f'{member.avatar_url}')
embed.set_author(name=f'{member.name}', icon_url=f'{member.avatar_url}')
return set_style(embed)
My problem is that if the message tries to tag the member when he joins (I am well aware the user does not get a notification doing this, but it tags him at least), sometimes the tag is not done correctly and it displays the internal member ID instead. I'd like to emphasize the sometimes, as it a weird behavior. To illustrate what I mean, yes or no this is the behavior of the bot that I am getting sometimes:

Notice how it does the intended behavior for one user but not for the other... In the case of the picture, that guild is using the following message:
Bienvenido a la colmena de {guild}, {mention}!
My question is if this has something to do with the way I'm coding the function or maybe is this caused by the way Discord manages the embeds?
I have also thought about internal discord cache problems, as the avatars of the members are not displayed correctly in some cases as well (it just keeps loading infinitely)...
Please sign in to leave a comment.
Comments
0 comments