【问题标题】:Command raised an exception: AttributeError: 'NoneType' object has no attribute 'name'命令引发异常:AttributeError:“NoneType”对象没有属性“名称”
【发布时间】:2021-08-04 18:09:40
【问题描述】:
@client.command(aliases=["lb"])
async def leaderboardie(ctx, x=1):
    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",
                       description="This is decided on the basis of raw money in the bank and wallet", color=discord.Color(0xfa43ee))
    index = 1
    for amt in total:
        id_ = leader_board[amt]
        member = client.get_user(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)

我不断收到错误: Command raised an exception: AttributeError: 'NoneType' object has no attribute 'name'

它基本上是关于基于用户钱包的排行榜,但每次我出于某种原因收到此错误,如果您知道解决方案,请告诉我。

【问题讨论】:

  • client.get_user(id_) 正在返回 None。
  • 如何解决它
  • 要么跳过该成员,要么输入替换值,或者以其他方式获取名称。想想你希望你的程序在那种情况下做的事情,然后编写执行此操作的代码。

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


【解决方案1】:

如果你在做client.get_user(_id),你需要在开发者门户中开启服务器成员网关意图。如果你不想开启这个 Intent,你可以使用:

await client.fetch_user(id)

【讨论】:

    猜你喜欢
    • 2022-01-25
    • 1970-01-01
    • 2021-02-05
    • 2021-01-30
    • 2021-12-07
    • 1970-01-01
    • 1970-01-01
    • 2022-01-05
    • 2021-12-04
    相关资源
    最近更新 更多