Added joke cog
This commit is contained in:
parent
05fda00d99
commit
ae56590bb9
6 changed files with 106 additions and 9 deletions
|
@ -7,3 +7,8 @@
|
||||||
- user
|
- user
|
||||||
- ids
|
- ids
|
||||||
logo: alligator2
|
logo: alligator2
|
||||||
|
mongo:
|
||||||
|
username: user
|
||||||
|
password: pass
|
||||||
|
host: localhost
|
||||||
|
port: 27017
|
||||||
|
|
|
@ -6,8 +6,8 @@ from psutil import Process
|
||||||
import asyncio
|
import asyncio
|
||||||
|
|
||||||
from jarvis.config import get_config
|
from jarvis.config import get_config
|
||||||
from jarvis import utils
|
from jarvis import utils, logo
|
||||||
from jarvis import logo
|
|
||||||
|
|
||||||
if asyncio.get_event_loop().is_closed():
|
if asyncio.get_event_loop().is_closed():
|
||||||
asyncio.set_event_loop(asyncio.new_event_loop())
|
asyncio.set_event_loop(asyncio.new_event_loop())
|
||||||
|
@ -16,6 +16,7 @@ intents = Intents.default()
|
||||||
intents.members = True
|
intents.members = True
|
||||||
restart_ctx = None
|
restart_ctx = None
|
||||||
|
|
||||||
|
|
||||||
jarvis = commands.Bot(command_prefix=utils.get_prefix, intents=intents)
|
jarvis = commands.Bot(command_prefix=utils.get_prefix, intents=intents)
|
||||||
jarvis_self = Process()
|
jarvis_self = Process()
|
||||||
__version__ = "0.1.0"
|
__version__ = "0.1.0"
|
||||||
|
|
81
jarvis/cogs/jokes.py
Normal file
81
jarvis/cogs/jokes.py
Normal file
|
@ -0,0 +1,81 @@
|
||||||
|
import jarvis
|
||||||
|
import discord
|
||||||
|
from random import randint
|
||||||
|
import html
|
||||||
|
import re
|
||||||
|
from jarvis.utils import build_embed, db
|
||||||
|
from jarvis.utils.field import Field
|
||||||
|
from discord.ext import commands
|
||||||
|
from datetime import datetime
|
||||||
|
|
||||||
|
|
||||||
|
class JokeCog(commands.Cog):
|
||||||
|
def __init__(self, bot):
|
||||||
|
self.bot = bot
|
||||||
|
self.db = db.create_connection()
|
||||||
|
|
||||||
|
# TODO: Make this a command group with subcommands
|
||||||
|
@commands.command(name="joke", help="Hear a joke")
|
||||||
|
async def _joke(self, ctx, id: str = None):
|
||||||
|
if randint(1, 100_000) == 5779 and id is None:
|
||||||
|
await ctx.send(f"<@{ctx.message.author.id}>")
|
||||||
|
return
|
||||||
|
# TODO: Add this as a parameter that can be passed in
|
||||||
|
threshold = 500 # Minimum score
|
||||||
|
coll = self.db.jokes.reddit
|
||||||
|
result = None
|
||||||
|
if id:
|
||||||
|
result = coll.find_one({"id": id})
|
||||||
|
else:
|
||||||
|
result = list(
|
||||||
|
coll.aggregate(
|
||||||
|
[
|
||||||
|
{"$match": {"score": {"$gt": threshold}}},
|
||||||
|
{"$sample": {"size": 1}},
|
||||||
|
]
|
||||||
|
)
|
||||||
|
)[0]
|
||||||
|
while result["body"] in ["[removed]", "[deleted]"]:
|
||||||
|
result = list(
|
||||||
|
coll.aggregate(
|
||||||
|
[
|
||||||
|
{"$match": {"score": {"$gt": threshold}}},
|
||||||
|
{"$sample": {"size": 1}},
|
||||||
|
]
|
||||||
|
)
|
||||||
|
)[0]
|
||||||
|
# TODO: Build a custom embed to show the joke
|
||||||
|
if result is None:
|
||||||
|
await ctx.send("Humor module failed. Please try again later.")
|
||||||
|
return
|
||||||
|
emotes = re.findall(r"(&#x[a-fA-F0-9]*;)", result["body"])
|
||||||
|
for match in emotes:
|
||||||
|
result["body"] = result["body"].replace(
|
||||||
|
match, html.unescape(match)
|
||||||
|
)
|
||||||
|
emotes = re.findall(r"(&#x[a-fA-F0-9]*;)", result["title"])
|
||||||
|
for match in emotes:
|
||||||
|
result["title"] = result["title"].replace(
|
||||||
|
match, html.unescape(match)
|
||||||
|
)
|
||||||
|
fields = [
|
||||||
|
Field("", result["body"], False),
|
||||||
|
Field("Score", result["score"]),
|
||||||
|
Field(
|
||||||
|
"Created At",
|
||||||
|
str(datetime.fromtimestamp(result["created_utc"])),
|
||||||
|
),
|
||||||
|
Field("ID", result["id"]),
|
||||||
|
]
|
||||||
|
embed = build_embed(
|
||||||
|
title=result["title"],
|
||||||
|
desc=None,
|
||||||
|
fields=fields,
|
||||||
|
url=f"https://reddit.com/r/jokes/comments/{result['id']}",
|
||||||
|
)
|
||||||
|
await ctx.send(embed=embed)
|
||||||
|
# await ctx.send(f"**{result['title']}**\n\n{result['body']}")
|
||||||
|
|
||||||
|
|
||||||
|
def setup(bot):
|
||||||
|
bot.add_cog(JokeCog(bot))
|
|
@ -13,15 +13,11 @@ class Config:
|
||||||
client_id: str
|
client_id: str
|
||||||
admins: list
|
admins: list
|
||||||
logo: str
|
logo: str
|
||||||
|
mongo: dict
|
||||||
|
|
||||||
@classmethod
|
@classmethod
|
||||||
def from_yaml(cls, y):
|
def from_yaml(cls, y):
|
||||||
instance = cls(
|
instance = cls(**y)
|
||||||
token=y["token"],
|
|
||||||
client_id=y["client_id"],
|
|
||||||
admins=y["admins"],
|
|
||||||
logo=y["logo"],
|
|
||||||
)
|
|
||||||
return instance
|
return instance
|
||||||
|
|
||||||
|
|
||||||
|
|
|
@ -2,8 +2,11 @@ from discord.ext import commands
|
||||||
from discord import Embed, Color
|
from discord import Embed, Color
|
||||||
from pkgutil import iter_modules
|
from pkgutil import iter_modules
|
||||||
import jarvis.cogs
|
import jarvis.cogs
|
||||||
|
import jarvis.utils.db
|
||||||
import git
|
import git
|
||||||
|
|
||||||
|
__all__ = ["field", "db"]
|
||||||
|
|
||||||
|
|
||||||
def convert_bytesize(bytes: int) -> str:
|
def convert_bytesize(bytes: int) -> str:
|
||||||
bytes = float(bytes)
|
bytes = float(bytes)
|
||||||
|
@ -34,7 +37,11 @@ def parse_color_hex(hex: str) -> Color:
|
||||||
|
|
||||||
|
|
||||||
def build_embed(
|
def build_embed(
|
||||||
title: str, desc: str, fields: list, color: str = "#FF0000", **kwargs
|
title: str,
|
||||||
|
desc: str,
|
||||||
|
fields: list,
|
||||||
|
color: str = "#FF0000",
|
||||||
|
**kwargs,
|
||||||
) -> Embed:
|
) -> Embed:
|
||||||
embed = Embed(
|
embed = Embed(
|
||||||
title=title, desc=desc, color=parse_color_hex(color), **kwargs
|
title=title, desc=desc, color=parse_color_hex(color), **kwargs
|
||||||
|
|
7
jarvis/utils/db.py
Normal file
7
jarvis/utils/db.py
Normal file
|
@ -0,0 +1,7 @@
|
||||||
|
from pymongo import MongoClient
|
||||||
|
from jarvis.config import get_config
|
||||||
|
|
||||||
|
|
||||||
|
def create_connection() -> MongoClient:
|
||||||
|
config = get_config()
|
||||||
|
return MongoClient(**config.mongo)
|
Loading…
Add table
Reference in a new issue