mirror of https://github.com/stijndcl/didier
220 lines
8.7 KiB
Python
220 lines
8.7 KiB
Python
from typing import Callable, Optional
|
|
|
|
from data.menus import paginated_leaderboard
|
|
from decorators import help
|
|
import discord
|
|
from discord.ext import commands
|
|
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
|
|
|
|
|
|
# TODO some sort of general leaderboard generation because all of them are the same
|
|
class Leaderboards(commands.Cog):
|
|
|
|
def __init__(self, client):
|
|
self.client = client
|
|
self.utilsCog = self.client.get_cog("Utils")
|
|
|
|
# Don't allow any commands to work when locked
|
|
def cog_check(self, ctx):
|
|
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:,}",))
|
|
|
|
return data
|
|
|
|
@commands.group(name="Leaderboard", aliases=["Lb", "Leaderboards"], case_insensitive=True, usage="[Categorie]*",
|
|
invoke_without_command=True)
|
|
@commands.check(checks.allowedChannels)
|
|
@help.Category(category=Category.Other)
|
|
async def leaderboard(self, ctx):
|
|
categories = ["Bitcoin", "Corona", "Dinks", "Messages", "Poke", "Rob", "XP"]
|
|
embed = discord.Embed(colour=discord.Colour.blue())
|
|
embed.set_author(name="Leaderboard Categorieën")
|
|
embed.description = "\n".join(categories)
|
|
await ctx.channel.send(embed=embed)
|
|
|
|
@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)
|
|
|
|
@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)
|
|
|
|
@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)
|
|
|
|
@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)
|
|
|
|
@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)
|
|
|
|
@leaderboard.command(name="Xp", aliases=["Level"], hidden=True)
|
|
async def xp(self, ctx):
|
|
s = stats.getAllRows()
|
|
boardTop = []
|
|
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])
|
|
if int(user[0]) == int(ctx.author.id):
|
|
boardTop.append("**{} (Level {:,} | {:,} XP)**".format(name,
|
|
xp.calculate_level(round(int(user[12]))),
|
|
round(int(user[12]))))
|
|
else:
|
|
boardTop.append("{} (Level {:,} | {:,} XP)".format(name,
|
|
xp.calculate_level(round(int(user[12]))),
|
|
round(int(user[12]))))
|
|
await self.startPaginated(ctx, boardTop, "XP Leaderboard")
|
|
|
|
@leaderboard.command(name="Messages", aliases=["Mc", "Mess"], hidden=True)
|
|
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])), perc))
|
|
else:
|
|
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)
|
|
async def muttn(self, ctx):
|
|
users = muttn.getAllRows()
|
|
boardTop = []
|
|
for i, user in enumerate(sorted(users, key=lambda x: x[1], reverse=True)):
|
|
if i == 0 and int(user[1]) == 0:
|
|
return await self.empty_leaderboard(ctx, "Muttn Leaderboard", "Der zittn nog geen muttns in de server.")
|
|
|
|
if float(user[1]) == 0:
|
|
break
|
|
|
|
name = self.utilsCog.getDisplayName(ctx, user[0])
|
|
if int(user[0]) == int(ctx.author.id):
|
|
boardTop.append("**{} ({})%**".format(name, round(float(user[1]), 2)))
|
|
else:
|
|
boardTop.append("{} ({}%)".format(name, round(float(user[1]), 2)))
|
|
await self.startPaginated(ctx, boardTop, "Muttn Leaderboard")
|
|
|
|
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))
|