CHANGE: Quotes are now taken randomly directly from the database, instead of a mass-query passed through a choice() function

pull/3/head
Kamal Curi 2 years ago
parent 3dd2cd0f8e
commit 49e903342d

@ -65,14 +65,13 @@ async def random_quote(bot: object) -> str:
"""
Get a random quote from the database.
"""
quotes = get_quotes(quote_id_stack)
stack_limit = int((len(quotes) * .25))
chosen_one = get_quotes(quote_id_stack)
stack_len = len(quote_id_stack)
if not quotes and stack_len > 0:
if not chosen_one and stack_len > 0:
quote_id_stack.pop(0)
quotes = get_quotes(quote_id_stack)
elif not quotes:
chosen_one = get_quotes(quote_id_stack)
elif not chosen_one:
return await bot.send('You\'ve got no quotes saved yet.\n(Save quotes by using '\
'`--q <quote`)')

@ -6,7 +6,7 @@ import logging
import MySQLdb
from models.quotes import Quotes
from settings.config import MYSQL_CONFIG, SQLACHEMY
from sqlalchemy import func
from sqlalchemy.sql import func
from sqlalchemy.orm import Session
logger = logging.getLogger(__name__)
@ -89,8 +89,8 @@ def get_quotes(ids: list) -> tuple:
ids: List of quote ID's
"""
with Session(SQLACHEMY) as session:
response = session.query(Quotes).filter(Quotes.id.not_in(ids))
return [item for item in response]
response = session.query(Quotes).filter(Quotes.id.not_in(ids)).order_by(func.random()).first()
return response
def get_by_id(id: int) -> object:

Loading…
Cancel
Save