diakonos/cherubim/quotes.py

80 lines
2.8 KiB
Python
Raw Normal View History

2024-01-17 00:20:44 -05:00
import asyncio
import os
import random
from datetime import datetime
from re import split
from typing import final
import discord
from redbot.core import commands, checks, Config
class Quotes(commands.Cog):
def __init__(self, bot):
self.bot = bot
self.config = Config.get_conf(self, identifier=(3322665 + 55673))
default_guild = {
"quotes": {},
}
self.config.register_guild(**default_guild)
@commands.command(name="quote", pass_context=True, no_pm=True)
async def quote(self, ctx, user: discord.Member=None, value: int=None):
guild_group = self.config.guild(ctx.guild)
if user is None:
user = ctx.author
async with guild_group.quotes() as quotes:
if str(user.id) in quotes:
quote = None
if value is None:
quote = random.choice(quotes[str(user.id)])
else:
value = value - 1
if -1 < value < len(quotes[str(user.id)]):
quote = quotes[str(user.id)][value]
if quote is not None:
split_paragraphs = quote["text"].split("\n")
for idx in range(len(split_paragraphs)):
split_paragraphs[idx] = f"> {split_paragraphs[idx]}"
final_quote = "\n".join(split_paragraphs)
link = quote["link"]
await ctx.send(f"{user.display_name}:\n\n{final_quote}\n\nContext: <{link}>")
else:
await ctx.send("There are no quotes associated with this user.")
@commands.Cog.listener()
async def on_reaction_add(self, reaction, user):
guild_group = self.config.guild(reaction.message.guild)
reacts_needed = 4
async with guild_group.quotes() as quotes:
if reaction.emoji == "":
users_reacted_count = 0
users_reacted = await reaction.users().flatten()
for user in users_reacted:
if user.id != reaction.message.author.id:
users_reacted_count += 1
if users_reacted_count >= reacts_needed:
msg = reaction.message
final_obj = {
"text": msg.content,
"link": msg.jump_url
}
if str(msg.author.id) in quotes:
if final_obj not in quotes[str(msg.author.id)]:
quotes[str(msg.author.id)].append(final_obj)
else:
quotes[str(msg.author.id)] = [final_obj]