I have a leaderboard that I'm trying to sort from the member with the highest count to the lowest count but I cannot seem to do it. Leaderboard Code:
@bot.command()
@commands.cooldown(1, 20, commands.BucketType.user)
async def leaderboard(ctx):
embed = discord.Embed(
title="Leadboard",
description="Your Servers Count",
color=0xFF000
)
with open('messages.json') as file:
data = json.load(file)
for key, value in data[f"{ctx.guild.name}"].items():
try:
embed.add_field(name = key, value = value)
embed.set_footer(text="If you feel your message's are not being counted, please get the server owner to send me a dm!")
except: return
await ctx.respond(embed=embed)
Output: Gyazo.com
And here is a example of what I'm trying to accomplish. Gyazo.com
CodePudding user response:
final answer :
#your imports [...]
from discord.utils import get
from operator import itemgetter
from tabulate import tabulate
#your declarations [...]
@bot.command()
@commands.cooldown(1, 20, commands.BucketType.user)
listValues = []
sortedList = []
embed = discord.Embed(
title="Leadboard",
description="Your Servers Count",
color=0xFF000
)
with open('dat.json') as file:
data = json.load(file)
listValues = new_list = list(map(list, data[f"{ctx.guild.name}"].items()))
sortedList = sorted(listValues, key=itemgetter(1), reverse=True)
strToPrint = ""
for item in sortedList :
strToPrint = strToPrint "\n" item[0] " : " str(item[1])
embed.add_field(name="Scores", value=strToPrint )
embed.set_footer(text="If you feel your message's are not being counted, please get the server owner to send me a dm!")
await ctx.send(embed=embed)
After a few tries on my side, easiest was to make a list of json dictionary, map the list to get keys and values, and turn it into a new list. Then I convert the list into a string, adding "\n" as delimiter, and converting the score, after it has been sorted, into a string. Finally, I pass the resulting string as value for your embed, and.. voilà :)
CodePudding user response:
With the help of my new friend @pluck me and him have found a solution
JSON Example
{
"Slims Mod Bot": {
"Felix\u2122": 66,
"DustinFoes": 31,
"Slim Beatbox": 109,
"_noobzoid": 7,
"Slims Moderation Bot": 22,
"Mad": 4,
"AverageIndonesianYoutuber": 1,
"~Emmy~": 1,
"Lord_WASD_248": 1,
"pluck": 3
},
Python Code
@bot.command()
@commands.cooldown(1, 20, commands.BucketType.user)
async def leaderboard(ctx):
listValues = []
sortedList = []
embed = discord.Embed(
title="Leadboard",
description="Your Servers Count",
color=0xFF000
)
with open('messages.json') as file:
data = json.load(file)
listValues = new_list = list(map(list, data[f"{ctx.guild.name}"].items()))
sortedList = sorted(listValues, key=itemgetter(1), reverse=True)
strToPrint = ""
for item in sortedList :
strToPrint = strToPrint "\n" item[0] " : " str(item[1])
embed.add_field(name="Scores", value=f"**{strToPrint}**" )
embed.set_footer(text="If you feel your message's are not being counted, please get the server owner to send me a dm!")
await ctx.send(embed=embed)
This gives the nice, leaderboard from the JSON file, and it's exactly what I was looking for.