So I made a Discord.py leaderboard command with MongoDB, everything works but I can't send the users name.
Data is stored like this:
_id: 484363190168322049 #users id
experience: 964
level: 5
Python code:
@client.command(aliases = ["et", "leaderboard"])
async def lb(ctx, arg=10):
rankings = collection.find().sort("experience",-1)
i=1
embed = discord.Embed(title = f"***Top {arg} users***")
for x in rankings:
print(x["_id"])
user = client.get_user(x["_id"])
user_xp = x["experience"]
##########################################################################
# This is where I want to add the user username instead of <@{x['_id']}> #
##########################################################################
embed.add_field(name=f"{i}: <@{x['_id']}>", value=f"XP │ {user_xp}", inline=False)
if i == arg:
break
else:
i += 1
embed.set_footer(text=f"{ctx.guild}", icon_url=f"{ctx.guild.icon_url}")
embed.timestamp = datetime.datetime.utcnow()
await ctx.send(embed=embed)
This mentions the user but I don't want that, I want only want the user's username, I have tried:
user = client.get_user(x["_id"])
embed.add_field(name=f"{i}: {user.name}>", value=f"XP │ {user_xp}", inline=False)
Previously this has worked with my other commands, but it returns:
discord.ext.commands.errors.CommandInvokeError: Command raised an exception: AttributeError: 'NoneType' object has no attribute 'name'
Your problem is that the bot doesn't fetch the user (NoneType error). Try to do this:
user1 = int(x["_id"])
user = client.get_user(user1)
embed.add_field(name=f"{i}: {user.name}>", value=f"XP │ {user_xp}", inline=False)
Or check the database, the error could be the above or it can be a database find() error.