0

I am working on a discord.py economy bot I just get this error.

I try this -----> Discord.py get_user(id) But It doesn't work

Ignoring exception in command leaderboard:
Traceback (most recent call last):
  File "C:\Users\Jerry\AppData\Local\Programs\Python\Python38\lib\site-packages\discord\ext\commands\core.py", line 85, in wrapped
    ret = await coro(*args, **kwargs)
  File "test.py", line 303, in leaderboard
    name = member.name
AttributeError: 'NoneType' object has no attribute 'name'

The above exception was the direct cause of the following exception:

Traceback (most recent call last):
  File "C:\Users\Jerry\AppData\Local\Programs\Python\Python38\lib\site-packages\discord\ext\commands\bot.py", line 902, in invoke
    await ctx.command.invoke(ctx)
  File "C:\Users\Jerry\AppData\Local\Programs\Python\Python38\lib\site-packages\discord\ext\commands\core.py", line 864, in invoke
    await injected(*ctx.args, **ctx.kwargs)
  File "C:\Users\Jerry\AppData\Local\Programs\Python\Python38\lib\site-packages\discord\ext\commands\core.py", line 94, in wrapped
    raise CommandInvokeError(exc) from exc
discord.ext.commands.errors.CommandInvokeError: Command raised an exception: AttributeError: 'NoneType' object has no attribute 'name'

Is there any way to fix this?

Code:

@client.command(aliases = ["lb"])
async def leaderboard(ctx,x: int = 10):
    users = await get_bank_data()
    leader_board = {}
    total = []
    for user in users:
        name = int(user)
        total_amount = users[user]["wallet"] + users[user]["bank"]
        leader_board[total_amount] = name
        total.append(total_amount)

    total = sorted(total, reverse=True)

    em = discord.Embed(title=f"Top {x} Richest People", color=random.randint(0, 0xffffff))
    index = 1
    for amt in total:
        id_ = leader_board[amt]
        member = ctx.guild.get_member(id_)
        name = member.name
        em.add_field(name=f"{index}. {name}", value=f"{amt}", inline=False)
        if index == x:
            break
        else:
            index += 1
    await ctx.send(embed=em)
4

2 回答 2

0

Looking at the documentation for get_member, if the member is not found it returns None. Thus, since the error you are getting is that 'NoneType' has no attribute 'name,' that suggests that get_member returned None and based on the docs it only returns None if the member is not found.

I'd recommend adding


member = ctx.guild.get_member(id_) #your existing line
if member is None:
    raise ValueError(f"Member with id {id_} not found")

Adding this if statement will automatically raise an error so that you can see when get_member returns None (ie doesn't find the result) so you can handle that appropriately.

于 2021-03-17T23:23:05.730 回答
0

Sorry for all mistakes but thanks a lot!

thanks to @mr_mooo_cow and @user1558604

The code is

@client.command(aliases = ["lb"])
async def leaderboard(ctx,x: int = 10):
    users = await get_bank_data()
    leader_board = {}
    total = []
    for user in users:
        name = int(user)
        total_amount = users[user]["wallet"] + users[user]["bank"]
        leader_board[total_amount] = name
        total.append(total_amount)

    total = sorted(total, reverse=True)

    em = discord.Embed(title=f"Top {x} Richest People", color=random.randint(0, 0xffffff))
    index = 1
    for amt in total:
        id_ = leader_board[amt]
        member = await ctx.guild.fetch_member(id_) #your existing line
        if member is None:
            raise ValueError(f"Member with id {id_} not found")
        name = member.name
        em.add_field(name=f"{index}. {name}", value=f"{amt}", inline=False)
        if index == x:
            break
        else:
            index += 1
    await ctx.send(embed=em)

于 2021-03-18T00:03:37.707 回答