Reputation: 17
I've made a simple points bot for discord with a leaderboard. The problem is that if any two users have the same number of points, the name is repeated twice on the leaderboard. I know why the problem is occurring but cannot think up of a way to stop it
@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)```
Upvotes: 0
Views: 107
Reputation: 22
Me after 2 years
after 2 years of using python, the problem is that leader_boards is a dictionary and dictionarys cant have the same key
heres the correct way
@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)
Upvotes: 0
Reputation: 22
i think you don't have all the intents on?
<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>
Upvotes: -1