'NoneType'对象没有属性'display_name'

时间:2020-11-10 17:44:19

标签: python discord.py

@bot.command()
async def servers(ctx):
    servers = bot.guilds
    servers.sort(key=lambda x: x.member_count, reverse=True)
    await ctx.send("Servers")
    for x in servers[:20]:
        await ctx.send("**{}** | **{}** Members | Owned by {} | Created at {}".format(x.name, x.member_count, x.owner.display_name, x.created_at))
    y = 0
    for x in bot.guilds:
        y += x.member_count
    await ctx.send("**Count:**\n**Users:** **{}** | **Server Count:** | **{}**".format(y, len(bot.guilds)))

我不断收到错误“ NoneType”对象没有属性“ display_name”的信息。我不完全明白为什么要这么说。

2 个答案:

答案 0 :(得分:1)

在尝试获取名称时检查是否有所有者。

for x in servers[:20]:
    owner_name = x.owner.display_name if x.owner else "unknown"
    await ctx.send("**{}** | **{}** Members | Owned by {} | Created at {}".format(x.name, x.member_count, owner_name, x.created_at))

答案 1 :(得分:1)

确保您使用的是Discord 1.5 ,并正确设置了intents。 API文档说明了如何执行此操作:https://discordpy.readthedocs.io/en/latest/api.html