CHANGE quote command to not get repetitive.

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

@ -6,7 +6,7 @@ from random import choice
from discord.ext import commands 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='~') client = commands.Bot(command_prefix='~')
logger = logging.getLogger(__name__) logger = logging.getLogger(__name__)
@ -37,17 +37,27 @@ async def random_quote(bot: object) -> str:
""" """
Get an random quote from database. Get an random quote from database.
""" """
while chosen_one[2] in quote_id_stack: quotes = get_quotes(quote_id_stack)
quotes = get_quote() stack_len = len(quote_id_stack)
chosen_one = choice(quotes)
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.')
quote_id_stack.add(chosen_one[2]) chosen_one = choice(quotes)
quote_id_stack.append(chosen_one.id)
if len(quote_id_stack) >= 5: if stack_len >= 5:
quote_id_stack[0].pop() quote_id_stack.pop(0)
try: 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: except Exception as ex:
return await bot.send(ex) return await bot.send(ex)

@ -68,14 +68,16 @@ def set_quote(user: str, quote: str) -> None:
with Cursor(MYSQL_CONFIG) as cursor: with Cursor(MYSQL_CONFIG) as cursor:
cursor.execute(_sql) cursor.execute(_sql)
def get_quotes() -> tuple: def get_quotes(ids: list) -> tuple:
""" """
Get the saved quotes. Get the saved quotes.
ids: List of quote ID's
""" """
_sql = f''' _sql = f'''
select quote, user, id 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 = [] response = []
obj = namedtuple('Quotes', ['quote', 'user', 'id']) obj = namedtuple('Quotes', ['quote', 'user', 'id'])

Loading…
Cancel
Save