Leaderboard cleanup

This commit is contained in:
Stijn De Clercq 2022-02-06 01:11:40 +01:00
parent ca687956f6
commit 81a0d90a12
6 changed files with 365 additions and 280 deletions

View file

@ -1,5 +1,5 @@
from data.embeds.xkcd import XKCDEmbed
from data.menus import paginated_leaderboard
from data.menus import leaderboards
from decorators import help
import discord
from discord.ext import commands

View file

@ -1,18 +1,12 @@
from typing import Callable, Optional
from data.menus import paginated_leaderboard
from decorators import help
import discord
from discord.ext import commands
from data.menus import leaderboards
from decorators import help
from enums.help_categories import Category
from enums.numbers import Numbers
from functions import checks, xp
from functions.database import currency, stats, poke, muttn
import math
import requests
from functions import checks
# TODO some sort of general leaderboard generation because all of them are the same
class Leaderboards(commands.Cog):
def __init__(self, client):
@ -20,29 +14,9 @@ class Leaderboards(commands.Cog):
self.utilsCog = self.client.get_cog("Utils")
# Don't allow any commands to work when locked
def cog_check(self, ctx):
def cog_check(self, _):
return not self.client.locked
def _generate_embed_data(self, entries: list,
key_f: Callable = lambda x: x[0],
data_f: Callable = lambda x: x[1],
ignore_non_pos: bool = True) -> Optional[list[tuple]]:
data = []
for i, v in enumerate(sorted(entries, key=data_f, reverse=True)):
entry_data = data_f(v)
# Leaderboard is empty
if i == 0 and entry_data == 0 and ignore_non_pos:
return None
# Ignore entries with no data
if ignore_non_pos and entry_data <= 0:
continue
data.append((key_f(v), f"{entry_data:,}", entry_data,))
return data
@commands.group(name="Leaderboard", aliases=["Lb", "Leaderboards"], case_insensitive=True, usage="[Categorie]*",
invoke_without_command=True)
@commands.check(checks.allowedChannels)
@ -56,154 +30,48 @@ class Leaderboards(commands.Cog):
@leaderboard.command(name="Dinks", aliases=["Cash"], hidden=True)
async def dinks(self, ctx):
entries = currency.getAllRows()
platDinks = currency.getAllPlatDinks()
# Take platinum dinks into account
for i, user in enumerate(entries):
if str(user[0]) in platDinks:
# Tuples don't support assignment, cast to list
user = list(user)
user[1] += platDinks[str(user[0])] * Numbers.q.value
entries[i] = user
data = self._generate_embed_data(entries, key_f=lambda x: x[0], data_f=lambda x: (float(x[1]) + float(x[3])))
if data is None:
return await self.empty_leaderboard(ctx, "Dinks Leaderboard",
"Er zijn nog geen personen met Didier Dinks.")
lb = paginated_leaderboard.Leaderboard(
ctx=ctx, title="Dinks Leaderboard", data=data, fetch_names=True
)
await lb.send(ctx)
lb = leaderboards.DinksLeaderboard(ctx=ctx)
await lb.send()
@leaderboard.command(name="Corona", hidden=True)
async def corona(self, ctx):
result = requests.get("https://disease.sh/v3/covid-19/countries").json()
result.sort(key=lambda x: int(x["cases"]), reverse=True)
data = []
for country in result:
data.append((country["country"], f"{country['cases']:,}",))
lb = paginated_leaderboard.Leaderboard(
ctx=ctx, title="Corona Leaderboard", data=data, highlight="Belgium",
colour=discord.Colour.red()
)
await lb.send(ctx)
lb = leaderboards.CoronaLeaderboard(ctx=ctx)
await lb.send()
@leaderboard.command(name="Bitcoin", aliases=["Bc"], hidden=True)
async def bitcoin(self, ctx):
users = currency.getAllRows()
data = self._generate_embed_data(users, data_f=lambda x: round(float(x[8]), 8))
if data is None:
return await self.empty_leaderboard(ctx, "Bitcoin Leaderboard",
"Er zijn nog geen personen met Bitcoins.")
lb = paginated_leaderboard.Leaderboard(
ctx=ctx, title="Bitcoin Leaderboard", data=data, fetch_names=True
)
await lb.send(ctx)
lb = leaderboards.BitcoinLeaderboard(ctx=ctx)
await lb.send()
@leaderboard.command(name="Rob", hidden=True)
async def rob(self, ctx):
users = list(stats.getAllRows())
data = self._generate_embed_data(users, data_f=lambda x: math.floor(float(x[4])))
if data is None:
return await self.empty_leaderboard(ctx, "Rob Leaderboard",
"Er heeft nog niemand Didier Dinks gestolen.")
lb = paginated_leaderboard.Leaderboard(
ctx=ctx, title="Rob Leaderboard", data=data, fetch_names=True
)
await lb.send(ctx)
lb = leaderboards.RobLeaderboard(ctx=ctx)
await lb.send()
@leaderboard.command(name="Poke", hidden=True)
async def poke(self, ctx):
entries = stats.getAllRows()
blacklist = poke.getAllBlacklistedUsers()
# Remove blacklisted users
entries = list(filter(lambda x: x[0] not in blacklist, entries))
data = self._generate_embed_data(entries, data_f=lambda x: round(int(x[1])))
if data is None:
return await self.empty_leaderboard(ctx, "Poke Leaderboard", "Er is nog niemand getikt.")
lb = paginated_leaderboard.Leaderboard(
ctx=ctx, title="Poke Leaderboard", data=data, fetch_names=True
)
await lb.send(ctx)
lb = leaderboards.PokeLeaderboard(ctx=ctx)
await lb.send()
@leaderboard.command(name="Xp", aliases=["Level"], hidden=True)
async def xp(self, ctx):
entries = stats.getAllRows()
data = self._generate_embed_data(entries, data_f=lambda x: round(int(x[12])))
def _format_entry(entry: int) -> str:
return f"Level {xp.calculate_level(entry):,} | {entry:,} XP"
lb = paginated_leaderboard.Leaderboard(
ctx=ctx, title="XP Leaderboard", data=data, fetch_names=True, format_f=_format_entry
)
await lb.send(ctx)
lb = leaderboards.XPLeaderboard(ctx=ctx)
await lb.send()
@leaderboard.command(name="Messages", aliases=["Mc", "Mess"], hidden=True)
async def messages(self, ctx):
entries = stats.getAllRows()
message_count = stats.getTotalMessageCount()
data = self._generate_embed_data(entries, data_f=lambda x: round(int(x[11])))
def _format_entry(entry: int) -> str:
perc = round(entry * 100 / message_count, 2)
return f"{entry:,} | {perc}%"
lb = paginated_leaderboard.Leaderboard(
ctx=ctx, title="Messages Leaderboard", data=data, fetch_names=True, format_f=_format_entry
)
await lb.send(ctx)
lb = leaderboards.MessageLeaderboard(ctx=ctx)
await lb.send()
@leaderboard.command(name="Muttn", aliases=["M", "Mutn", "Mutten"], hidden=True)
async def muttn(self, ctx):
entries = muttn.getAllRows()
data = self._generate_embed_data(entries, data_f=lambda x: round(float(x[1]), 2))
if data is None:
return await self.empty_leaderboard(ctx, "Muttn Leaderboard", "Der zittn nog geen muttns in de server.")
def _format_entry(entry: float) -> str:
return f"{entry}%"
lb = paginated_leaderboard.Leaderboard(
ctx=ctx, title="Muttn Leaderboard", data=data, fetch_names=True, format_f=_format_entry
)
await lb.send(ctx)
lb = leaderboards.MuttnLeaderboard(ctx=ctx)
await lb.send()
async def callLeaderboard(self, name, ctx):
command = [command for command in self.leaderboard.commands if command.name.lower() == name.lower()][0]
await command(ctx)
async def startPaginated(self, ctx, source, name, colour=discord.Colour.blue()):
pages = paginated_leaderboard.Pages(source=paginated_leaderboard.Source(source, name, colour),
clear_reactions_after=True)
await pages.start(ctx)
async def empty_leaderboard(self, ctx, name, message, colour=discord.Colour.blue()):
embed = discord.Embed(colour=colour)
embed.set_author(name=name)
embed.description = message
await ctx.send(embed=embed)
def setup(client):
client.add_cog(Leaderboards(client))

View file

@ -1,4 +1,4 @@
from data.menus import paginated_leaderboard
from data.menus import leaderboards
from decorators import help
import discord
from discord.ext import commands, menus