diff --git a/utils/commands.py b/utils/commands.py index 68bb55a..30159c1 100644 --- a/utils/commands.py +++ b/utils/commands.py @@ -12,7 +12,7 @@ from discord import Embed, Intents from discord.ext import commands from settings.config import IMAGE_TYPES, OW_API_CONFIG, PERMISSIONS, QUOTE_STACK, RQ_LEADERBOARD, ROULETTE_LEADERBOARD -from utils.database import (count_quotes, count_quotes_user, get_by_id, get_quote_contains, +from utils.database import (count_quotes, count_quotes_user, get_by_id, get_by_user, get_quote_contains, get_quotes, remove_quote, set_quote) from utils.machine_monitor import Monitor from utils.news_paper import News @@ -226,6 +226,36 @@ async def by_id(bot: object, _id: int=None) -> str: except Exception as ex: return await bot.send(ex) +@client.command(aliases=['quser']) +async def by_user(bot: object, *user: str) -> str: + """ + Gets one random quote from a specific user. + """ + syntax = "`--quser `" + + if not user: + return await bot.send(f"The correct syntax is {syntax}") + + user = user[0] + + quote = get_by_user(user) + + if not quote: + return await bot.send(f"There are no quotes from {user}") + + try: + data = '' + # To image links. + if 'http' in quote.quote: + return await bot.send(f'{quote.quote}') + if quote.grabber == "#nograb": + data = f'{quote.quote}\n`By: {quote.user}`' + else: + data = f'{quote.quote}\n`By: {quote.user} and grabbed by {quote.grabber}`' + return await bot.send(data) + + except Exception as ex: + return await bot.send(ex) @client.command(aliases=['qinfo']) async def quote_info(bot: object, _id: str=None) -> str: diff --git a/utils/database.py b/utils/database.py index deb5cd0..71d6e79 100644 --- a/utils/database.py +++ b/utils/database.py @@ -102,6 +102,13 @@ def get_by_id(id: int) -> object: result = [s for s in session.query(Quotes).filter(Quotes.id==id)] return result[0] if result else None +def get_by_user(user: str) -> object: + """ + Get one quote by user. + """ + with Session(SQLACHEMY) as session: + result = session.query(Quotes).filter(Quotes.user==user).order_by(func.random()).first() + return result def remove_quote(_id: int) -> bool: """