经济机器人 | discord.py 重写 |排行榜错误 - AttributeError: 'NoneType' 对象没有属性 'name'

时间:2021-03-17 23:09:10

标签: python python-3.x discord.py

我正在开发一个 discord.py 经济机器人,我刚收到这个错误。

我试试这个 -----> Discord.py get_user(id) 但它不起作用

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'

有什么办法可以解决这个问题吗?

代码:

@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)

2 个答案:

答案 0 :(得分:0)

查看 get_memberdocumentation,如果未找到该成员,则返回 None。因此,由于您得到的错误是 'NoneType' 没有属性 'name',这表明 get_member 返回 None 并且根据文档,如果未找到该成员,它仅返回 None 。

我建议添加


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

添加此 if 语句将自动引发错误,以便您可以看到 get_member 何时返回 None(即未找到结果),以便您可以适当地处理。

答案 1 :(得分:0)

抱歉所有的错误,但非常感谢!

感谢@mr_mooo_cow 和@user1558604

代码是

@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)