排行榜中的名字重复discord.py

问题描述 投票:0回答:2

我制作了一个简单的积分机器人,用于与排行榜不一致。问题是,如果任意两个用户的积分相同,则该名字会在排行榜上重复两次。 我知道为什么会出现问题,但想不出办法来阻止它

@commands.has_permissions(manage_roles=True)
async def leaderboard(doc,x=5):
    with open("playerdb.json","r") as db:
        info=json.load(db)
    leader_board={}
    total=[]
    for mem in info:
        name=int(mem)
        amount=info[str(mem)]["points"]
        leader_board[amount]=name
        total.append(amount)
    total=sorted(total,reverse=True)
    embed=discord.Embed(title=f"Top {x} Members With Highest Points Are",color=0x7289da)
    index=1
    for points in total:
        id_=leader_board[points]
        mems=discord.utils.get(doc.guild.members,id=id_)
        name=mems.name
        embed.add_field(name=f"{index}. {name}",value=f"{points}",inline=False)
        if index==x:
            break
        else:
            index += 1
    await doc.send(embed=embed)```
python discord discord.py leaderboard
2个回答
0
投票

2年后的我

使用Python 2年后,问题是leader_boards是一个字典,并且字典不能有相同的键

正确的方法是这样

@commands.has_permissions(manage_roles=True)
async def leaderboard(doc,x=5):
    with open("playerdb.json","r") as db:
        info=json.load(db)
    leader_board={}
    total=[]
    for mem in info:
        name=int(mem)
        amount=info[str(mem)]["points"]
        leader_board[name]=amount
        total.append((name, amount))
    total=sorted(total,reverse=True)
    embed=discord.Embed(title=f"Top {x} Members With Highest Points Are",color=0x7289da)
    index=1
    for points in total:
        id_=leader_board[points[0]]
        mems=discord.utils.get(doc.guild.members,id=id_)
        name=mems.name
        embed.add_field(name=f"{index}. {name}",value=f"{points[1]}",inline=False)
        if index==x:
            break
        else:
            index += 1
    await doc.send(embed=embed)

-1
投票

我认为你没有全部意图?

<p><strong>NOTE: Once your bot reaches 100 or more servers, this will require verification and approval. <a href="https://support.discord.com/hc/en-us/articles/360040720412" target="_blank" rel="noreferrer">Read more here</a></strong></p>

© www.soinside.com 2019 - 2024. All rights reserved.