diff --git a/settings/config.py b/settings/config.py index 2b25b9d..0b9258f 100644 --- a/settings/config.py +++ b/settings/config.py @@ -19,7 +19,7 @@ MYSQL_CONFIG = { # Define the log level LOGLEVEL = os.environ.get('LOGLEVEL', 'info').upper() -# Configuração do logging. +# Logging custom config. LOGGING_CONFIG = { 'version': 1, 'disable_existing_loggers': False, diff --git a/utils/commands.py b/utils/commands.py index ab70e29..57ba87a 100644 --- a/utils/commands.py +++ b/utils/commands.py @@ -6,11 +6,12 @@ from random import choice from discord.ext import commands -from utils.database import get_quote, set_quote +from utils.database import get_quotes, set_quote client = commands.Bot(command_prefix='~') logger = logging.getLogger(__name__) +quote_id_stack = [] @client.command(aliases=['q']) async def quote(bot: object, *quote: str) -> str: @@ -36,10 +37,40 @@ async def random_quote(bot: object) -> str: """ Get an random quote from database. """ - quotes = get_quote() + quotes = get_quotes(quote_id_stack) + stack_len = len(quote_id_stack) + + if not quotes and stack_len > 0: + quote_id_stack.pop(0) + quotes = get_quotes(quote_id_stack) + elif not quotes: + return await bot.send('Have no one quote saved.') + chosen_one = choice(quotes) + quote_id_stack.append(chosen_one.id) + + if stack_len >= 5: + quote_id_stack.pop(0) try: - return await bot.send(f'{chosen_one[0]}\n`By: {chosen_one[1]}`') + # To image links. + if 'http' in chosen_one.quote: + return await bot.send(f'{chosen_one.quote}') + return await bot.send(f'{chosen_one.quote}\n`By: {chosen_one.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 + """ + id_stack = "" + for qid in quote_id_stack: + id_stack = id_stack + qid + + rmessage = "A list of the 5 latest message IDs follows: " + id_stack + + return await bot.send(rmessage) diff --git a/utils/database.py b/utils/database.py index 8567b7d..d8a3e29 100644 --- a/utils/database.py +++ b/utils/database.py @@ -2,6 +2,8 @@ Database utils module. """ import logging +from collections import namedtuple + import MySQLdb from settings.config import MYSQL_CONFIG @@ -66,16 +68,21 @@ def set_quote(user: str, quote: str) -> None: with Cursor(MYSQL_CONFIG) as cursor: cursor.execute(_sql) -def get_quote() -> tuple: +def get_quotes(ids: list) -> tuple: """ Get the saved quotes. + ids: List of quote ID's """ _sql = f''' - select quote, user - from neeble_quotes; + select quote, user, id + from neeble_quotes ''' - response = None + _sql = _sql + f' where id not in ({",".join([str(id) for id in ids])});' if ids else _sql + ';' + response = [] + obj = namedtuple('Quotes', ['quote', 'user', 'id']) + with Cursor(MYSQL_CONFIG) as cursor: cursor.execute(_sql) response = cursor.fetchall() - return response + + return tuple(obj(*r) for r in response)