【问题标题】:Leaderboard code doesnt wok properly discord.py排行榜代码无法正常工作 discord.py
【发布时间】:2021-01-19 09:22:13
【问题描述】:

只要我的 JSON 文件中只有 1 个人,我的代码就可以正常工作。一旦另一个用户在 json 文件中注册。我收到此错误:命令引发异常:TypeError:“dict”和“dict”实例之间不支持“

知道我的错误是什么吗?

我的代码



@client.command()
async def leaderboard(ctx):

    with open('users.json', 'r') as f:
        data = json.load(f)

    top_users = {k: v for k, v in sorted(data.items(), key=lambda item: item[1], reverse=True)}

    names = ''
    for postion, user in enumerate(top_users):
        names += f'{postion+1} - <@!{user}> mit {top_users[user]}\n'

    embed = discord.Embed(title="Rangliste")
    embed.add_field(name="Spieler", value=names, inline=False)
    await ctx.send(embed=embed)

【问题讨论】:

  • 您不应该使用 JSON 作为数据库。推荐使用 MySQL、MongoDB 等。
  • 分享json文件。

标签: python discord.py


【解决方案1】:

您正在尝试比较字典,默认情况下不支持。

要解决此问题,您需要更改您的关键功能,因为它目前提供dict

# The following line gives an error because '<' is not applicable for item[1]
sorted(data.items(), key=lambda item: item[1], reverse=True)

请注意,您使用的属性应该具有可比性。

sorted(data.items(), key=lambda item: item[1]["someattribute"], reverse=True)

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2021-08-29
    • 2022-06-13
    • 2017-02-28
    • 2017-08-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多