From e78a82dbda08321ed7403fcbeb7b93b7995356ab Mon Sep 17 00:00:00 2001 From: Kamal Curi Date: Sat, 13 Aug 2022 15:04:33 -0300 Subject: [PATCH 1/8] CHANGE: rq's command no will no longer repeat between 5 quotes --- utils/commands.py | 7 +++++++ utils/database.py | 2 +- 2 files changed, 8 insertions(+), 1 deletion(-) diff --git a/utils/commands.py b/utils/commands.py index ab70e29..e7aa1f0 100644 --- a/utils/commands.py +++ b/utils/commands.py @@ -11,6 +11,7 @@ from utils.database import get_quote, 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,9 +37,15 @@ async def random_quote(bot: object) -> str: """ Get an random quote from database. """ + while chosen_one[2] in quote_id_stack: quotes = get_quote() chosen_one = choice(quotes) + quote_id_stack.add(chosen_one[2]) + + if len(quote_id_stack) >= 5: + quote_id_stack[0].pop() + try: return await bot.send(f'{chosen_one[0]}\n`By: {chosen_one[1]}`') except Exception as ex: diff --git a/utils/database.py b/utils/database.py index 8567b7d..baf51db 100644 --- a/utils/database.py +++ b/utils/database.py @@ -71,7 +71,7 @@ def get_quote() -> tuple: Get the saved quotes. """ _sql = f''' - select quote, user + select quote, user, id from neeble_quotes; ''' response = None From ba6a328c1c188d1a2b64ed2c3f8f677be8c17fcb Mon Sep 17 00:00:00 2001 From: Kamal Curi Date: Sat, 13 Aug 2022 15:04:54 -0300 Subject: [PATCH 2/8] ADD: Returns the id quote stack for testing and checking --- utils/commands.py | 14 ++++++++++++++ 1 file changed, 14 insertions(+) diff --git a/utils/commands.py b/utils/commands.py index e7aa1f0..848f9e9 100644 --- a/utils/commands.py +++ b/utils/commands.py @@ -50,3 +50,17 @@ async def random_quote(bot: object) -> str: return await bot.send(f'{chosen_one[0]}\n`By: {chosen_one[1]}`') except Exception as ex: return await bot.send(ex) + +@client.command(aliases=['qstack']) +async def random_quote(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.sent(rmessage) + From 7a3306ee173a1e2f6c605c775e07453904f9576d Mon Sep 17 00:00:00 2001 From: kevincaires Date: Sun, 14 Aug 2022 11:18:38 -0300 Subject: [PATCH 3/8] FIX function name, and bo send method. --- utils/commands.py | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/utils/commands.py b/utils/commands.py index 848f9e9..6d6d2b2 100644 --- a/utils/commands.py +++ b/utils/commands.py @@ -38,10 +38,10 @@ async def random_quote(bot: object) -> str: Get an random quote from database. """ while chosen_one[2] in quote_id_stack: - quotes = get_quote() - chosen_one = choice(quotes) + quotes = get_quote() + chosen_one = choice(quotes) - quote_id_stack.add(chosen_one[2]) + quote_id_stack.add(chosen_one[2]) if len(quote_id_stack) >= 5: quote_id_stack[0].pop() @@ -52,7 +52,7 @@ async def random_quote(bot: object) -> str: return await bot.send(ex) @client.command(aliases=['qstack']) -async def random_quote(bot: object) -> str: +async def queue_stack(bot: object) -> str: """ Displays the 5 quote history stack """ @@ -62,5 +62,5 @@ async def random_quote(bot: object) -> str: rmessage = "A list of the 5 latest message IDs follows: " + id_stack - return await bot.sent(rmessage) + return await bot.send(rmessage) From d519acd53856d615570ecda83a05d66718667aca Mon Sep 17 00:00:00 2001 From: kevincaires Date: Sun, 14 Aug 2022 11:35:00 -0300 Subject: [PATCH 4/8] FIX lint stuf. --- utils/commands.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/utils/commands.py b/utils/commands.py index 6d6d2b2..0c9e104 100644 --- a/utils/commands.py +++ b/utils/commands.py @@ -51,6 +51,7 @@ async def random_quote(bot: object) -> str: except Exception as ex: return await bot.send(ex) + @client.command(aliases=['qstack']) async def queue_stack(bot: object) -> str: """ @@ -63,4 +64,3 @@ async def queue_stack(bot: object) -> str: rmessage = "A list of the 5 latest message IDs follows: " + id_stack return await bot.send(rmessage) - From 151881617b7d5a720b7e1c4780394826ec535af9 Mon Sep 17 00:00:00 2001 From: kevincaires Date: Sun, 14 Aug 2022 11:46:04 -0300 Subject: [PATCH 5/8] CHANGE content response. --- utils/database.py | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) diff --git a/utils/database.py b/utils/database.py index baf51db..2edb9d6 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 @@ -74,8 +76,11 @@ def get_quote() -> tuple: select quote, user, id from neeble_quotes; ''' - response = None + 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) From 3bcb64efd88a91eb1bcc706075e4429a150acec8 Mon Sep 17 00:00:00 2001 From: kevincaires Date: Sun, 14 Aug 2022 13:19:10 -0300 Subject: [PATCH 6/8] FIX function name. --- utils/database.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/utils/database.py b/utils/database.py index 2edb9d6..792db48 100644 --- a/utils/database.py +++ b/utils/database.py @@ -68,7 +68,7 @@ def set_quote(user: str, quote: str) -> None: with Cursor(MYSQL_CONFIG) as cursor: cursor.execute(_sql) -def get_quote() -> tuple: +def get_quotes() -> tuple: """ Get the saved quotes. """ From 1b039f1692abc1fa15b0e8473e638e80888be42c Mon Sep 17 00:00:00 2001 From: kevincaires Date: Sun, 14 Aug 2022 13:19:52 -0300 Subject: [PATCH 7/8] FIX comment to satisfy my OCD. --- settings/config.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) 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, From b082a67528d40fa98d0962f384cfb0929eead8a8 Mon Sep 17 00:00:00 2001 From: kevincaires Date: Sun, 14 Aug 2022 13:52:48 -0300 Subject: [PATCH 8/8] CHANGE quote command to not get repetitive. --- utils/commands.py | 26 ++++++++++++++++++-------- utils/database.py | 6 ++++-- 2 files changed, 22 insertions(+), 10 deletions(-) diff --git a/utils/commands.py b/utils/commands.py index 0c9e104..57ba87a 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_quote, set_quote +from utils.database import get_quotes, set_quote client = commands.Bot(command_prefix='~') logger = logging.getLogger(__name__) @@ -37,17 +37,27 @@ async def random_quote(bot: object) -> str: """ Get an random quote from database. """ - while chosen_one[2] in quote_id_stack: - quotes = get_quote() - chosen_one = choice(quotes) + quotes = get_quotes(quote_id_stack) + stack_len = len(quote_id_stack) - quote_id_stack.add(chosen_one[2]) + 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.') - if len(quote_id_stack) >= 5: - quote_id_stack[0].pop() + 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) diff --git a/utils/database.py b/utils/database.py index 792db48..d8a3e29 100644 --- a/utils/database.py +++ b/utils/database.py @@ -68,14 +68,16 @@ def set_quote(user: str, quote: str) -> None: with Cursor(MYSQL_CONFIG) as cursor: cursor.execute(_sql) -def get_quotes() -> tuple: +def get_quotes(ids: list) -> tuple: """ Get the saved quotes. + ids: List of quote ID's """ _sql = f''' select quote, user, id - from neeble_quotes; + from neeble_quotes ''' + _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'])