From 19f0f144cacf1c607a3765fbc854383dc798355a Mon Sep 17 00:00:00 2001 From: "kevin.caires" Date: Wed, 17 Aug 2022 18:28:27 -0300 Subject: [PATCH] ADD get by ID command. --- utils/commands.py | 34 ++++++++++++++++++++++++++++------ utils/database.py | 21 +++++++++++++++++++++ 2 files changed, 49 insertions(+), 6 deletions(-) diff --git a/utils/commands.py b/utils/commands.py index 94e22b4..ba1e384 100644 --- a/utils/commands.py +++ b/utils/commands.py @@ -6,7 +6,7 @@ from random import choice from discord.ext import commands -from utils.database import get_quotes, set_quote +from utils.database import get_by_id, get_quotes, set_quote client = commands.Bot(command_prefix='--') logger = logging.getLogger(__name__) @@ -19,7 +19,7 @@ async def quote(bot: object, *quote: str) -> str: Save a quote into database. """ if not quote: - return await bot.send('Insert a message to save.') + return await bot.send('Insert a message to save.\n_Your dumb ass!_') quote = ' '.join(quote) @@ -45,7 +45,7 @@ async def random_quote(bot: object) -> str: quote_id_stack.pop(0) quotes = get_quotes(quote_id_stack) elif not quotes: - return await bot.send('Have no one quote saved.') + return await bot.send('Have no one quote saved.\n_Hey jerk, coffee?_') chosen_one = choice(quotes) quote_id_stack.append(chosen_one.id) @@ -63,11 +63,33 @@ async def random_quote(bot: object) -> str: return await bot.send(ex) +@client.command(aliases=['bid']) +async def by_id(bot, _id: int=None) -> str: + """ + Get quote by ID. + """ + if not isinstance(_id, int) or not _id: + return await bot.send("_Don't fuck, you ass hole_.\nThe ID need to be a interger!") + + quote = get_by_id(_id) + + if not quote: + return await bot.send("_Got wrong, you socker!_\nThis ID doesn't exist in database!") + + try: + # 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}`') + + except Exception as ex: + return await bot.send(ex) + + @client.command(aliases=['qstack']) async def queue_stack(bot: object) -> str: """ Displays the 5 quote history stack """ - return await bot.send( - f'A list of the 5 latest message IDs follows: `{",".join(str(q) for q in quote_id_stack)}`' - ) + return await bot.send('A list of the 5 latest message IDs follows:'\ + f' `{",".join(str(q) for q in quote_id_stack[-5:])}`') diff --git a/utils/database.py b/utils/database.py index d8a3e29..d072cb7 100644 --- a/utils/database.py +++ b/utils/database.py @@ -86,3 +86,24 @@ def get_quotes(ids: list) -> tuple: response = cursor.fetchall() return tuple(obj(*r) for r in response) + + +def get_by_id(id: int) -> object: + """ + Get one quote by ID. + """ + obj = namedtuple('Quotes', ['quote', 'user', 'id']) + _sql = f''' + select quote, user, id + from neeble_quotes + where id={id}; + ''' + + with Cursor(MYSQL_CONFIG) as cursor: + cursor.execute(_sql) + quote = cursor.fetchone() + + if not quote: + return None + + return obj(*quote)