经济机器人discord.py重写|排行榜错误-AttributeError:"NoneType"对象没有属性“名称" [英] Economy Bot | discord.py rewrite | Leaderboard error - AttributeError: 'NoneType' object has no attribute 'name'

查看:21
本文介绍了经济机器人discord.py重写|排行榜错误-AttributeError:"NoneType"对象没有属性“名称"的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在开发一款discord.py经济型机器人,我刚遇到此错误.

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

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

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?

代码:

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

推荐答案

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

Sorry for all mistakes but thanks a lot!

感谢@mr_mooo_cow和@ user1558604

thanks to @mr_mooo_cow and @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)

这篇关于经济机器人discord.py重写|排行榜错误-AttributeError:"NoneType"对象没有属性“名称"的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆