CHANGE quote command to not get repetitive.

pull/3/head
kevincaires 3 years ago
parent 1b039f1692
commit b082a67528

@ -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)

@ -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'])

Loading…
Cancel
Save