ADD: New 'grab' feature

pull/3/head
Kamal Curi 2 years ago
parent cbaf465aee
commit 1aeb5ea9c2

@ -28,6 +28,15 @@ stack_limit = int((count_quotes() * .25))
with open(QUOTE_STACK, mode='r') as f:
quote_id_stack = json.load(f)
quote_content = {}
@client.event
async def on_message(message):
content = str(message.content)
author = str(message.author).split('#')[0]
quote_content[author] = content
await client.process_commands(message)
@client.command(aliases=['q'])
async def quote(bot: object, *quote: str) -> str:
"""
@ -48,7 +57,7 @@ async def quote(bot: object, *quote: str) -> str:
try:
user = bot.author.name
date = datetime.now().replace(microsecond=0)
qtid = set_quote(user, quote, date)
qtid = set_quote(user, quote, date, "#notgrabbed")
except Exception as ex:
if ex.args[0].find("Duplicate") != -1:
return await bot.send("There's already a quote from that same person, with that "\
@ -59,6 +68,33 @@ async def quote(bot: object, *quote: str) -> str:
stack_limit = int((count_quotes() * .25))
return await bot.send(f"Done: `{quote}\n` ID: `{qtid}`")
@client.command(aliases=['g'])
async def grab_quote(bot: object, *author: str) -> str:
"""
Grabs the last thing someone said, and makes it into a quote.
"""
author = ' '.join(author)
if not author:
return await bot.send("You haven\'t told me whose sentence I'm supposed to grab!")
if author in quote_content.keys():
try:
grabber = bot.author.name
quote = quote_content[author]
date = datetime.now().replace(microsecond=0)
qtid = set_quote(author, quote, date, grabber)
except Exception as ex:
if ex.args[0].find("Duplicate") != -1:
return await bot.send("There's already a quote from that same person, with that "\
"exact match!")
return await bot.send(f'{ex.args}\n_What the fuck are you doing?_')
else:
global stack_limit
stack_limit = int((count_quotes() * .25))
return await bot.send(f"Done: `{quote}\n` ID: `{qtid}`")
else:
return await bot.send("No quotes from anyone with that name!")
@client.command(aliases=['rq'])
async def random_quote(bot: object) -> str:
@ -90,6 +126,10 @@ async def random_quote(bot: object) -> str:
# To image links.
if 'http' in chosen_one.quote:
return await bot.send(f'{chosen_one.quote}')
if chosen_one.grabber == "#nograb":
content = f'{chosen_one.quote}\n`By: {chosen_one.user}`'
else:
content = f'{chosen_one.quote}\n`By: {chosen_one.user} and grabbed by {chosen_one.grabber}`'
return await bot.send(f'{chosen_one.quote}\n`By: {chosen_one.user}`')
except Exception as ex:
@ -113,10 +153,15 @@ async def by_id(bot: object, _id: int=None) -> str:
return await bot.send(f"_Wrong ID, sucker!_\n(There's no such quote with id {_id})")
try:
data = ''
# To image links.
if 'http' in quote.quote:
return await bot.send(f'{quote.quote}')
return await bot.send(f'{quote.quote}\n`By: {quote.user}`')
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(content)
except Exception as ex:
return await bot.send(ex)
@ -143,7 +188,11 @@ async def quote_info(bot: object, _id: str=None) -> str:
user = quote.user
date = quote.date if quote.date else "Before datetimes were stored"
data = f"```\n ID: {_id}\n Quoted by: {user}\n Quoted datetime: {date}\n```"
grabber = quote.grabber
if grabber == "#nograb":
data = f"```\n ID: {_id}\n Quoted by: {user}\n Quoted datetime: {date}\n```"
else:
data = f"```\n ID: {_id}\n Quoted by: {user}\n Grabbed by: {grabber}\n Quoted datetime: {date}\n```"
return await bot.send(data)

@ -50,6 +50,7 @@ def migrate() -> None:
user varchar(200) not null,
quote varchar(500) not null unique,
date datetime not null,
grabber varchar(100) not null,
index quote_idx (quote)
) character set utf8mb4 collate utf8mb4_general_ci;
@ -68,11 +69,11 @@ def migrate() -> None:
logger.error(ex.args)
def set_quote(user: str, quote: str, date: str) -> int:
def set_quote(user: str, quote: str, date: str, grabber: str) -> int:
"""
Set a quote into database.
"""
qt = Quotes(quote=quote, user=user, date=date)
qt = Quotes(quote=quote, user=user, date=date, grabber=grabber)
qtid = 0
with Session(SQLACHEMY) as session:
session.add(qt)

Loading…
Cancel
Save