From 1aeb5ea9c25273793c8db1a05f0aad33fa0a2076 Mon Sep 17 00:00:00 2001 From: Kamal Curi Date: Wed, 19 Jul 2023 15:21:14 -0300 Subject: [PATCH 1/5] ADD: New 'grab' feature --- utils/commands.py | 55 ++++++++++++++++++++++++++++++++++++++++++++--- utils/database.py | 5 +++-- 2 files changed, 55 insertions(+), 5 deletions(-) diff --git a/utils/commands.py b/utils/commands.py index b40babb..1bd71a8 100644 --- a/utils/commands.py +++ b/utils/commands.py @@ -28,6 +28,15 @@ stack_limit = int((count_quotes() * .25)) with open(QUOTE_STACK, mode='r') as f: quote_id_stack = json.load(f) +quote_content = {} + +@client.event +async def on_message(message): + content = str(message.content) + author = str(message.author).split('#')[0] + quote_content[author] = content + await client.process_commands(message) + @client.command(aliases=['q']) async def quote(bot: object, *quote: str) -> str: """ @@ -48,7 +57,7 @@ async def quote(bot: object, *quote: str) -> str: try: user = bot.author.name date = datetime.now().replace(microsecond=0) - qtid = set_quote(user, quote, date) + qtid = set_quote(user, quote, date, "#notgrabbed") except Exception as ex: if ex.args[0].find("Duplicate") != -1: return await bot.send("There's already a quote from that same person, with that "\ @@ -59,6 +68,33 @@ async def quote(bot: object, *quote: str) -> str: stack_limit = int((count_quotes() * .25)) return await bot.send(f"Done: `{quote}\n` ID: `{qtid}`") +@client.command(aliases=['g']) +async def grab_quote(bot: object, *author: str) -> str: + """ + Grabs the last thing someone said, and makes it into a quote. + """ + author = ' '.join(author) + if not author: + return await bot.send("You haven\'t told me whose sentence I'm supposed to grab!") + + if author in quote_content.keys(): + try: + grabber = bot.author.name + quote = quote_content[author] + date = datetime.now().replace(microsecond=0) + qtid = set_quote(author, quote, date, grabber) + except Exception as ex: + if ex.args[0].find("Duplicate") != -1: + return await bot.send("There's already a quote from that same person, with that "\ + "exact match!") + return await bot.send(f'{ex.args}\n_What the fuck are you doing?_') + else: + global stack_limit + stack_limit = int((count_quotes() * .25)) + return await bot.send(f"Done: `{quote}\n` ID: `{qtid}`") + else: + return await bot.send("No quotes from anyone with that name!") + @client.command(aliases=['rq']) async def random_quote(bot: object) -> str: @@ -90,6 +126,10 @@ async def random_quote(bot: object) -> str: # To image links. if 'http' in chosen_one.quote: return await bot.send(f'{chosen_one.quote}') + if chosen_one.grabber == "#nograb": + content = f'{chosen_one.quote}\n`By: {chosen_one.user}`' + else: + content = f'{chosen_one.quote}\n`By: {chosen_one.user} and grabbed by {chosen_one.grabber}`' return await bot.send(f'{chosen_one.quote}\n`By: {chosen_one.user}`') except Exception as ex: @@ -113,10 +153,15 @@ async def by_id(bot: object, _id: int=None) -> str: return await bot.send(f"_Wrong ID, sucker!_\n(There's no such quote with id {_id})") try: + data = '' # To image links. if 'http' in quote.quote: return await bot.send(f'{quote.quote}') - return await bot.send(f'{quote.quote}\n`By: {quote.user}`') + if quote.grabber == "#nograb": + data = f'{quote.quote}\n`By: {quote.user}`' + else: + data = f'{quote.quote}\n`By: {quote.user} and grabbed by {quote.grabber}`' + return await bot.send(content) except Exception as ex: return await bot.send(ex) @@ -143,7 +188,11 @@ async def quote_info(bot: object, _id: str=None) -> str: user = quote.user date = quote.date if quote.date else "Before datetimes were stored" - data = f"```\n ID: {_id}\n Quoted by: {user}\n Quoted datetime: {date}\n```" + grabber = quote.grabber + if grabber == "#nograb": + data = f"```\n ID: {_id}\n Quoted by: {user}\n Quoted datetime: {date}\n```" + else: + data = f"```\n ID: {_id}\n Quoted by: {user}\n Grabbed by: {grabber}\n Quoted datetime: {date}\n```" return await bot.send(data) diff --git a/utils/database.py b/utils/database.py index 49d3418..deb5cd0 100644 --- a/utils/database.py +++ b/utils/database.py @@ -50,6 +50,7 @@ def migrate() -> None: user varchar(200) not null, quote varchar(500) not null unique, date datetime not null, + grabber varchar(100) not null, index quote_idx (quote) ) character set utf8mb4 collate utf8mb4_general_ci; @@ -68,11 +69,11 @@ def migrate() -> None: logger.error(ex.args) -def set_quote(user: str, quote: str, date: str) -> int: +def set_quote(user: str, quote: str, date: str, grabber: str) -> int: """ Set a quote into database. """ - qt = Quotes(quote=quote, user=user, date=date) + qt = Quotes(quote=quote, user=user, date=date, grabber=grabber) qtid = 0 with Session(SQLACHEMY) as session: session.add(qt) From 6f0bbb2adef45ad5216f3098d4596ec34db04cb5 Mon Sep 17 00:00:00 2001 From: Kamal Curi Date: Wed, 19 Jul 2023 15:38:43 -0300 Subject: [PATCH 2/5] FIX: Without this declaration, '--dbg' fails if '--rq' is never invoked --- utils/commands.py | 1 + 1 file changed, 1 insertion(+) diff --git a/utils/commands.py b/utils/commands.py index 1bd71a8..67cf0ba 100644 --- a/utils/commands.py +++ b/utils/commands.py @@ -30,6 +30,7 @@ with open(QUOTE_STACK, mode='r') as f: quote_content = {} +last_quote = 0 @client.event async def on_message(message): content = str(message.content) From a3b1951f4c90909a98628fe9b95a07f7701280cb Mon Sep 17 00:00:00 2001 From: Kamal Curi Date: Wed, 19 Jul 2023 15:39:37 -0300 Subject: [PATCH 3/5] ADD: authors list in '--dbg' --- utils/commands.py | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/utils/commands.py b/utils/commands.py index 67cf0ba..1fe48e2 100644 --- a/utils/commands.py +++ b/utils/commands.py @@ -31,6 +31,7 @@ with open(QUOTE_STACK, mode='r') as f: quote_content = {} last_quote = 0 + @client.event async def on_message(message): content = str(message.content) @@ -488,7 +489,8 @@ async def neeble_debug(bot:object) -> str: qt_count = count_quotes() st_size = len(quote_id_stack) - return await bot.send(f"```\nqt_count:{qt_count}\nst_size:{st_size}\nst_limit:{stack_limit}\nlst_qt:{last_quote}\n```") + ct_authors = str(quote_content.keys()) + return await bot.send(f"```\nqt_count:{qt_count}\nst_size:{st_size}\nst_limit:{stack_limit}\nlst_qt:{last_quote}\nct_authors:{ct_authors}\n```") @client.command(aliases=['dr', 'droll']) async def dice_roll(bot:object, size:int=6) -> str: From 7d662ee3c3d5f0a52fe2113eafd46ded83f7a49a Mon Sep 17 00:00:00 2001 From: Kamal Curi Date: Wed, 19 Jul 2023 16:20:01 -0300 Subject: [PATCH 4/5] ADD: '--rqlb' feature --- settings/config.py | 3 +++ utils/commands.py | 30 +++++++++++++++++++++++++++++- 2 files changed, 32 insertions(+), 1 deletion(-) diff --git a/settings/config.py b/settings/config.py index 9eb6d5d..cfcb9d1 100644 --- a/settings/config.py +++ b/settings/config.py @@ -34,6 +34,9 @@ LOGLEVEL = os.environ.get('LOGLEVEL', 'info').upper() # Define the path for the quote ID stack file QUOTE_STACK = os.environ.get('NEEBLE_STACK_FILE', '/opt/neeble/id.list') +# Define the path for the --rq leaderboard file +RQ_LEADERBOARD = os.environ.get('RQ_LEADERBOARD_FILE', '/opt/neeble/rqlb.list') + # Logging custom config. LOGGING_CONFIG = { 'version': 1, diff --git a/utils/commands.py b/utils/commands.py index 1fe48e2..42135f4 100644 --- a/utils/commands.py +++ b/utils/commands.py @@ -11,7 +11,7 @@ from random import randrange from discord import Embed, Intents from discord.ext import commands -from settings.config import IMAGE_TYPES, OW_API_CONFIG, PERMISSIONS, QUOTE_STACK +from settings.config import IMAGE_TYPES, OW_API_CONFIG, PERMISSIONS, QUOTE_STACK, RQ_LEADERBOARD from utils.database import (count_quotes, count_quotes_user, get_by_id, get_quote_contains, get_quotes, remove_quote, set_quote) @@ -28,6 +28,9 @@ stack_limit = int((count_quotes() * .25)) with open(QUOTE_STACK, mode='r') as f: quote_id_stack = json.load(f) +with open(RQ_LEADERBOARD, mode='r') as f: + rq_leaderboard = json.load(f) + quote_content = {} last_quote = 0 @@ -106,6 +109,12 @@ async def random_quote(bot: object) -> str: chosen_one = get_quotes(quote_id_stack) stack_len = len(quote_id_stack) + #Adds to --rq leaderboard + if bot.author.name in rq_leaderboard.keys(): + rq_leaderboard[bot.author.name] += 1 + else: + rq_leaderboard[bot.author.name] = 1 + if not chosen_one and stack_len > 0: quote_id_stack.pop(0) chosen_one = get_quotes(quote_id_stack) @@ -124,6 +133,10 @@ async def random_quote(bot: object) -> str: with open(QUOTE_STACK, mode='w') as f: json.dump(quote_id_stack, f) + # Writes to persistent rq leaderboard + with open(RQ_LEADERBOARD, mode='w') as f: + json.dump(rq_leaderboard, f) + try: # To image links. if 'http' in chosen_one.quote: @@ -470,6 +483,21 @@ async def count_leaderboard(bot:object) -> str: return await bot.send(qlb) +@client.command(aliases=['rqlb']) +async def random_quote_leaderboard(bot:object) -> str: + """ + Returns a list of --rq invokers, sorted by amount + """ + data = "```\nLista de rqueiros\n" + lis = rq_leaderboard + lis = sorted(lis.items(), key=lambda x:x[1], reverse=True) + for people in lis: + data = data + people[0] + " - " + str(people[1]) + "\n" + data = data + "```" + + return await bot.send(data) + + @client.command(aliases=['dbg']) async def neeble_debug(bot:object) -> str: """ From 874501c9efc777c3c2b1b98ef6e5c84785b7765c Mon Sep 17 00:00:00 2001 From: Kamal Curi Date: Wed, 19 Jul 2023 16:26:04 -0300 Subject: [PATCH 5/5] FIX: Wrong variable --- utils/commands.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/utils/commands.py b/utils/commands.py index 42135f4..bf9f10f 100644 --- a/utils/commands.py +++ b/utils/commands.py @@ -176,7 +176,7 @@ async def by_id(bot: object, _id: int=None) -> str: data = f'{quote.quote}\n`By: {quote.user}`' else: data = f'{quote.quote}\n`By: {quote.user} and grabbed by {quote.grabber}`' - return await bot.send(content) + return await bot.send(data) except Exception as ex: return await bot.send(ex)