Merge pull request 'ADD: 2 new features' (#3) from newfeat_grab into develop

Reviewed-on: #3
ipm
Kamal Curi 2 years ago
commit 21c5363f40

@ -34,6 +34,9 @@ LOGLEVEL = os.environ.get('LOGLEVEL', 'info').upper()
# Define the path for the quote ID stack file
QUOTE_STACK = os.environ.get('NEEBLE_STACK_FILE', '/opt/neeble/id.list')
# Define the path for the --rq leaderboard file
RQ_LEADERBOARD = os.environ.get('RQ_LEADERBOARD_FILE', '/opt/neeble/rqlb.list')
# Logging custom config.
LOGGING_CONFIG = {
'version': 1,

@ -11,7 +11,7 @@ from random import randrange
from discord import Embed, Intents
from discord.ext import commands
from settings.config import IMAGE_TYPES, OW_API_CONFIG, PERMISSIONS, QUOTE_STACK
from settings.config import IMAGE_TYPES, OW_API_CONFIG, PERMISSIONS, QUOTE_STACK, RQ_LEADERBOARD
from utils.database import (count_quotes, count_quotes_user, get_by_id, get_quote_contains,
get_quotes, remove_quote, set_quote)
@ -28,6 +28,20 @@ stack_limit = int((count_quotes() * .25))
with open(QUOTE_STACK, mode='r') as f:
quote_id_stack = json.load(f)
with open(RQ_LEADERBOARD, mode='r') as f:
rq_leaderboard = json.load(f)
quote_content = {}
last_quote = 0
@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 +62,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 +73,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:
@ -68,6 +109,12 @@ async def random_quote(bot: object) -> str:
chosen_one = get_quotes(quote_id_stack)
stack_len = len(quote_id_stack)
#Adds to --rq leaderboard
if bot.author.name in rq_leaderboard.keys():
rq_leaderboard[bot.author.name] += 1
else:
rq_leaderboard[bot.author.name] = 1
if not chosen_one and stack_len > 0:
quote_id_stack.pop(0)
chosen_one = get_quotes(quote_id_stack)
@ -86,10 +133,18 @@ async def random_quote(bot: object) -> str:
with open(QUOTE_STACK, mode='w') as f:
json.dump(quote_id_stack, f)
# Writes to persistent rq leaderboard
with open(RQ_LEADERBOARD, mode='w') as f:
json.dump(rq_leaderboard, f)
try:
# 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 +168,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(data)
except Exception as ex:
return await bot.send(ex)
@ -143,7 +203,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"
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)
@ -419,6 +483,21 @@ async def count_leaderboard(bot:object) -> str:
return await bot.send(qlb)
@client.command(aliases=['rqlb'])
async def random_quote_leaderboard(bot:object) -> str:
"""
Returns a list of --rq invokers, sorted by amount
"""
data = "```\nLista de rqueiros\n"
lis = rq_leaderboard
lis = sorted(lis.items(), key=lambda x:x[1], reverse=True)
for people in lis:
data = data + people[0] + " - " + str(people[1]) + "\n"
data = data + "```"
return await bot.send(data)
@client.command(aliases=['dbg'])
async def neeble_debug(bot:object) -> str:
"""
@ -438,7 +517,8 @@ async def neeble_debug(bot:object) -> str:
qt_count = count_quotes()
st_size = len(quote_id_stack)
return await bot.send(f"```\nqt_count:{qt_count}\nst_size:{st_size}\nst_limit:{stack_limit}\nlst_qt:{last_quote}\n```")
ct_authors = str(quote_content.keys())
return await bot.send(f"```\nqt_count:{qt_count}\nst_size:{st_size}\nst_limit:{stack_limit}\nlst_qt:{last_quote}\nct_authors:{ct_authors}\n```")
@client.command(aliases=['dr', 'droll'])
async def dice_roll(bot:object, size:int=6) -> str:

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