From 1aeb5ea9c25273793c8db1a05f0aad33fa0a2076 Mon Sep 17 00:00:00 2001 From: Kamal Curi Date: Wed, 19 Jul 2023 15:21:14 -0300 Subject: [PATCH] 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)