From 3aba3e19b28f094c13b6598e0289e10ebb678b23 Mon Sep 17 00:00:00 2001 From: Kamal Curi Date: Thu, 30 Mar 2023 16:58:06 -0300 Subject: [PATCH] ADD: Quote leaderboard feature --- utils/commands.py | 18 ++++++++++++++++-- utils/database.py | 8 ++++++++ 2 files changed, 24 insertions(+), 2 deletions(-) diff --git a/utils/commands.py b/utils/commands.py index 9f76032..aaa536a 100644 --- a/utils/commands.py +++ b/utils/commands.py @@ -11,7 +11,7 @@ from discord import Embed, Intents from discord.ext import commands from settings.config import IMAGE_TYPES, OW_API_CONFIG, PERMISSIONS -from utils.database import (count_quotes, get_by_id, get_quote_contains, +from utils.database import (count_quotes, count_quotes_user, get_by_id, get_quote_contains, get_quotes, remove_quote, set_quote) from utils.machine_monitor import Monitor from utils.news_paper import News @@ -363,4 +363,18 @@ async def neofetch(bot: object) -> str: os.system('neofetch --stdout --disable gpu --disable shell --disable packages --disable resolution --cpu_temp C > /tmp/neofetch') nfetch = open('/tmp/neofetch', mode='r') nfetch = nfetch.read() - return await bot.send("```\n" + nfetch + "\n```") \ No newline at end of file + return await bot.send("```\n" + nfetch + "\n```") + +@client.command(aliases=['qlb']) +async def count_leaderboard(bot:object): + """ + Returns a list of quoters, sorted by amount of quotes. + """ + qlb = "```\nLista de quoteiros\n" + lis = count_quotes_user() + lis = sorted(lis, key=lambda lis: lis[1], reverse=True) + for data in lis: + qlb = qlb + data[0] + " - " + str(data[1]) + "\n" + qlb = qlb + "```" + + return await bot.send(qlb) \ No newline at end of file diff --git a/utils/database.py b/utils/database.py index 8e7ebc9..ebb1a24 100644 --- a/utils/database.py +++ b/utils/database.py @@ -6,6 +6,7 @@ import logging import MySQLdb from models.quotes import Quotes from settings.config import MYSQL_CONFIG, SQLACHEMY +from sqlalchemy import func from sqlalchemy.orm import Session logger = logging.getLogger(__name__) @@ -122,6 +123,13 @@ def count_quotes() -> int: response = session.query(Quotes.id).count() return response +def count_quotes_user(): + """ + Quote "leaderboard" + """ + with Session(SQLACHEMY) as session: + response = session.query(Quotes.user, func.count(Quotes.user)).group_by(Quotes.user).all() + return response def get_quote_contains(part: str) -> tuple: """