Rework last leaderboards

pull/104/head
Stijn De Clercq 2022-02-05 23:21:47 +01:00
parent 062d54722b
commit ca687956f6
2 changed files with 35 additions and 47 deletions

View File

@ -39,7 +39,7 @@ class Leaderboards(commands.Cog):
if ignore_non_pos and entry_data <= 0: if ignore_non_pos and entry_data <= 0:
continue continue
data.append((key_f(v), f"{entry_data:,}",)) data.append((key_f(v), f"{entry_data:,}", entry_data,))
return data return data
@ -144,60 +144,50 @@ class Leaderboards(commands.Cog):
@leaderboard.command(name="Xp", aliases=["Level"], hidden=True) @leaderboard.command(name="Xp", aliases=["Level"], hidden=True)
async def xp(self, ctx): async def xp(self, ctx):
s = stats.getAllRows() entries = stats.getAllRows()
boardTop = [] data = self._generate_embed_data(entries, data_f=lambda x: round(int(x[12])))
for i, user in enumerate(sorted(s, key=lambda x: x[12], reverse=True)):
if int(user[12]) == 0:
break
name = self.utilsCog.getDisplayName(ctx, user[0]) def _format_entry(entry: int) -> str:
if int(user[0]) == int(ctx.author.id): return f"Level {xp.calculate_level(entry):,} | {entry:,} XP"
boardTop.append("**{} (Level {:,} | {:,} XP)**".format(name,
xp.calculate_level(round(int(user[12]))), lb = paginated_leaderboard.Leaderboard(
round(int(user[12])))) ctx=ctx, title="XP Leaderboard", data=data, fetch_names=True, format_f=_format_entry
else: )
boardTop.append("{} (Level {:,} | {:,} XP)".format(name,
xp.calculate_level(round(int(user[12]))), await lb.send(ctx)
round(int(user[12]))))
await self.startPaginated(ctx, boardTop, "XP Leaderboard")
@leaderboard.command(name="Messages", aliases=["Mc", "Mess"], hidden=True) @leaderboard.command(name="Messages", aliases=["Mc", "Mess"], hidden=True)
async def messages(self, ctx): async def messages(self, ctx):
s = stats.getAllRows() entries = stats.getAllRows()
boardTop = []
message_count = stats.getTotalMessageCount() message_count = stats.getTotalMessageCount()
for i, user in enumerate(sorted(s, key=lambda x: x[11], reverse=True)): data = self._generate_embed_data(entries, data_f=lambda x: round(int(x[11])))
if int(user[11]) == 0:
break
perc = round(int(user[11]) * 100 / message_count, 2) def _format_entry(entry: int) -> str:
perc = round(entry * 100 / message_count, 2)
return f"{entry:,} | {perc}%"
name = self.utilsCog.getDisplayName(ctx, user[0]) lb = paginated_leaderboard.Leaderboard(
if int(user[0]) == int(ctx.author.id): ctx=ctx, title="Messages Leaderboard", data=data, fetch_names=True, format_f=_format_entry
boardTop.append("**{} ({:,} | {}%)**".format(name, round(int(user[11])), perc)) )
else:
boardTop.append("{} ({:,} | {}%)".format(name, round(int(user[11])), perc)) await lb.send(ctx)
await self.startPaginated(ctx, boardTop, "Messages Leaderboard")
@leaderboard.command(name="Muttn", aliases=["M", "Mutn", "Mutten"], hidden=True) @leaderboard.command(name="Muttn", aliases=["M", "Mutn", "Mutten"], hidden=True)
async def muttn(self, ctx): async def muttn(self, ctx):
users = muttn.getAllRows() entries = muttn.getAllRows()
boardTop = [] data = self._generate_embed_data(entries, data_f=lambda x: round(float(x[1]), 2))
for i, user in enumerate(sorted(users, key=lambda x: x[1], reverse=True)): if data is None:
if i == 0 and int(user[1]) == 0:
return await self.empty_leaderboard(ctx, "Muttn Leaderboard", "Der zittn nog geen muttns in de server.") return await self.empty_leaderboard(ctx, "Muttn Leaderboard", "Der zittn nog geen muttns in de server.")
if float(user[1]) == 0: def _format_entry(entry: float) -> str:
break return f"{entry}%"
name = self.utilsCog.getDisplayName(ctx, user[0]) lb = paginated_leaderboard.Leaderboard(
if int(user[0]) == int(ctx.author.id): ctx=ctx, title="Muttn Leaderboard", data=data, fetch_names=True, format_f=_format_entry
boardTop.append("**{} ({})%**".format(name, round(float(user[1]), 2))) )
else:
boardTop.append("{} ({}%)".format(name, round(float(user[1]), 2))) await lb.send(ctx)
await self.startPaginated(ctx, boardTop, "Muttn Leaderboard")
async def callLeaderboard(self, name, ctx): async def callLeaderboard(self, name, ctx):
command = [command for command in self.leaderboard.commands if command.name.lower() == name.lower()][0] command = [command for command in self.leaderboard.commands if command.name.lower() == name.lower()][0]

View File

@ -21,10 +21,6 @@ class Leaderboard:
colour: discord.Colour = discord.Colour.blue() colour: discord.Colour = discord.Colour.blue()
fetch_names: bool = False fetch_names: bool = False
def __post_init__(self):
if self.format_f is None:
self .format_f = lambda x: x
def _should_highlight(self, data) -> bool: def _should_highlight(self, data) -> bool:
"""Check if an entry should be highlighted""" """Check if an entry should be highlighted"""
if self.fetch_names: if self.fetch_names:
@ -38,7 +34,9 @@ class Leaderboard:
if self.fetch_names: if self.fetch_names:
name = get_display_name(self.ctx, int(data[0])) name = get_display_name(self.ctx, int(data[0]))
s = f"{index + 1}: {name} ({self.format_f(data[1])})" formatted_data = self.format_f(data[2]) if self.format_f is not None else data[1]
s = f"{index + 1}: {name} ({formatted_data})"
return s return s