From 1553abf55277d7285ce1db5bcaab9a9b67a4a3f9 Mon Sep 17 00:00:00 2001 From: kevincaires Date: Sat, 20 Aug 2022 12:49:26 -0300 Subject: [PATCH] ADD command to get quote by a part of text. --- utils/commands.py | 27 +++++++++++++++++++++++++-- utils/database.py | 9 +++++++++ 2 files changed, 34 insertions(+), 2 deletions(-) diff --git a/utils/commands.py b/utils/commands.py index 440c36f..e664ee7 100644 --- a/utils/commands.py +++ b/utils/commands.py @@ -7,8 +7,8 @@ from random import choice from discord.ext import commands from settings.config import IMAGE_TYPES, PERMISSIONS -from utils.database import (count_quotes, get_by_id, get_quotes, remove_quote, - set_quote) +from utils.database import (count_quotes, get_by_id, get_quote_contains, + get_quotes, remove_quote, set_quote) client = commands.Bot(command_prefix='--') logger = logging.getLogger(__name__) @@ -182,3 +182,26 @@ async def info(bot: object) -> str: msg = f'''```\n{fullbanner}\n```''' return await bot.send(msg) + + +@client.command(aliases=['qcontains', 'qsearch']) +async def quote_contains(bot: object, part: str) -> str: + """ + Filter quote by part of saved message. + """ + syntax = '--qcontains ' + + if not part: + return await bot.send("_If you don't tell me the part, how the fuck do you expect me to "\ + f"find it to you!?_\n(The correct syntax is {syntax})") + + quotes = get_quote_contains(part) + + if not quotes: + return await bot.send(f"_Wrong text, sucker!_\n(There's no such quote with text `{part}`)") + + for quote in quotes: + await bot.send(f'```\nID: {quote.id}\nMessage: {quote.quote[:10]} ... '\ + f'{quote.quote[-10:]}\nUser: {quote.user}\n```') + + return diff --git a/utils/database.py b/utils/database.py index 40e57d8..ec34037 100644 --- a/utils/database.py +++ b/utils/database.py @@ -114,3 +114,12 @@ def count_quotes() -> int: with Session(SQLACHEMY) as session: response = session.query(Quotes.id).count() return response + + +def get_quote_contains(part: str) -> tuple: + """ + Get quotes by part of message. + """ + with Session(SQLACHEMY) as session: + response = session.query(Quotes).filter(Quotes.quote.contains(part)) + return (r for r in response)