From 6a946559e20260a27734452a2be08b2b6e7fb3e6 Mon Sep 17 00:00:00 2001 From: Stijn De Clercq Date: Wed, 23 Dec 2020 00:48:43 +0100 Subject: [PATCH] Add percentage to Message counter & leaderboard --- cogs/leaderboards.py | 10 +++++++--- cogs/xp.py | 6 +++++- functions/database/stats.py | 6 ++++++ 3 files changed, 18 insertions(+), 4 deletions(-) diff --git a/cogs/leaderboards.py b/cogs/leaderboards.py index b891442..1d39daf 100644 --- a/cogs/leaderboards.py +++ b/cogs/leaderboards.py @@ -10,7 +10,6 @@ import math import requests - # TODO some sort of general leaderboard because all of them are the same class Leaderboards(commands.Cog): @@ -156,15 +155,20 @@ class Leaderboards(commands.Cog): async def messages(self, ctx): s = stats.getAllRows() boardTop = [] + + message_count = stats.getTotalMessageCount() + for i, user in enumerate(sorted(s, key=lambda x: x[11], reverse=True)): if int(user[11]) == 0: break + perc = round(int(user[11]) * 100 / message_count, 2) + name = self.utilsCog.getDisplayName(ctx, user[0]) if int(user[0]) == int(ctx.author.id): - boardTop.append("**{} ({:,})**".format(name, round(int(user[11])))) + boardTop.append("**{} ({:,} | {}%)**".format(name, round(int(user[11])), perc)) else: - boardTop.append("{} ({:,})".format(name, round(int(user[11])))) + boardTop.append("{} ({:,} | {}%)".format(name, round(int(user[11])), perc)) await self.startPaginated(ctx, boardTop, "Messages Leaderboard") @leaderboard.command(name="Muttn", aliases=["M", "Mutn", "Mutten"], hidden=True) diff --git a/cogs/xp.py b/cogs/xp.py index 9d292cb..d805d65 100644 --- a/cogs/xp.py +++ b/cogs/xp.py @@ -22,9 +22,13 @@ class Xp(commands.Cog): target_stats = stats.getOrAddUser(target.id) + message_count = stats.getTotalMessageCount() + + perc = round(int(target_stats[11]) * 100/message_count, 2) + embed = discord.Embed(colour=discord.Colour.blue()) embed.set_author(name=target.display_name, icon_url=target.avatar_url) - embed.add_field(name="Aantal Berichten", value="{}".format(int(target_stats[11]))) + embed.add_field(name="Aantal Berichten", value="{} ({}%)".format(int(target_stats[11]), perc)) embed.add_field(name="Level", value=str(xp.calculate_level(target_stats[12]))) embed.add_field(name="XP", value="{:,}".format(int(target_stats[12]))) embed.set_footer(text="*Sinds Didier 2.0 Launch") diff --git a/functions/database/stats.py b/functions/database/stats.py index 43aac01..60d4685 100644 --- a/functions/database/stats.py +++ b/functions/database/stats.py @@ -107,6 +107,12 @@ def gainXp(user, user_db): update(user, "last_message", round(time.time())) +def getTotalMessageCount(): + r = getAllRows() + + return sum(user[11] for user in r) + + def getOrAddChannel(channelid: int): connection = utils.connect() cursor = connection.cursor()