Remove jokes
This commit is contained in:
parent
824548b04d
commit
a72ede0644
1 changed files with 0 additions and 122 deletions
|
@ -1,122 +0,0 @@
|
||||||
"""J.A.R.V.I.S. Jokes module."""
|
|
||||||
import html
|
|
||||||
import re
|
|
||||||
import traceback
|
|
||||||
from datetime import datetime
|
|
||||||
from random import randint
|
|
||||||
|
|
||||||
from dis_snek import InteractionContext, Scale, Snake
|
|
||||||
from dis_snek.models.discord.embed import EmbedField
|
|
||||||
from dis_snek.models.snek.application_commands import (
|
|
||||||
OptionTypes,
|
|
||||||
slash_command,
|
|
||||||
slash_option,
|
|
||||||
)
|
|
||||||
from dis_snek.models.snek.command import cooldown
|
|
||||||
from dis_snek.models.snek.cooldowns import Buckets
|
|
||||||
from jarvis_core.db import q
|
|
||||||
from jarvis_core.db.models import Joke
|
|
||||||
|
|
||||||
from jarvis.utils import build_embed
|
|
||||||
|
|
||||||
|
|
||||||
class JokeCog(Scale):
|
|
||||||
"""
|
|
||||||
Joke library for J.A.R.V.I.S.
|
|
||||||
|
|
||||||
May adapt over time to create jokes using machine learning
|
|
||||||
"""
|
|
||||||
|
|
||||||
def __init__(self, bot: Snake):
|
|
||||||
self.bot = bot
|
|
||||||
|
|
||||||
# TODO: Make this a command group with subcommands
|
|
||||||
@slash_command(
|
|
||||||
name="joke",
|
|
||||||
description="Hear a joke",
|
|
||||||
)
|
|
||||||
@slash_option(name="id", description="Joke ID", required=False, opt_type=OptionTypes.INTEGER)
|
|
||||||
@cooldown(bucket=Buckets.CHANNEL, rate=1, interval=10)
|
|
||||||
async def _joke(self, ctx: InteractionContext, id: str = None) -> None:
|
|
||||||
"""Get a joke from the database."""
|
|
||||||
try:
|
|
||||||
if randint(1, 100_000) == 5779 and id is None: # noqa: S311
|
|
||||||
await ctx.send(f"<@{ctx.message.author.id}>")
|
|
||||||
return
|
|
||||||
# TODO: Add this as a parameter that can be passed in
|
|
||||||
threshold = 500 # Minimum score
|
|
||||||
result = None
|
|
||||||
if id:
|
|
||||||
result = await Joke.find_one(q(rid=id))
|
|
||||||
else:
|
|
||||||
pipeline = [
|
|
||||||
{"$match": {"score": {"$gt": threshold}}},
|
|
||||||
{"$sample": {"size": 1}},
|
|
||||||
]
|
|
||||||
result = Joke.objects().aggregate(pipeline).next()
|
|
||||||
while result["body"] in ["[removed]", "[deleted]"]:
|
|
||||||
result = Joke.objects().aggregate(pipeline).next()
|
|
||||||
|
|
||||||
if result is None:
|
|
||||||
await ctx.send("Humor module failed. Please try again later.", ephemeral=True)
|
|
||||||
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))
|
|
||||||
body_chunks = []
|
|
||||||
|
|
||||||
body = ""
|
|
||||||
for word in result["body"].split(" "):
|
|
||||||
if len(body) + 1 + len(word) > 1024:
|
|
||||||
body_chunks.append(EmbedField("", body, False))
|
|
||||||
body = ""
|
|
||||||
if word == "\n" and body == "":
|
|
||||||
continue
|
|
||||||
elif word == "\n":
|
|
||||||
body += word
|
|
||||||
else:
|
|
||||||
body += " " + word
|
|
||||||
|
|
||||||
desc = ""
|
|
||||||
title = result["title"]
|
|
||||||
if len(title) > 256:
|
|
||||||
new_title = ""
|
|
||||||
limit = False
|
|
||||||
for word in title.split(" "):
|
|
||||||
if len(new_title) + len(word) + 1 > 253 and not limit:
|
|
||||||
new_title += "..."
|
|
||||||
desc = "..."
|
|
||||||
limit = True
|
|
||||||
if not limit:
|
|
||||||
new_title += word + " "
|
|
||||||
else:
|
|
||||||
desc += word + " "
|
|
||||||
|
|
||||||
body_chunks.append(EmbedField("", body, False))
|
|
||||||
|
|
||||||
fields = body_chunks
|
|
||||||
fields.append(EmbedField("Score", result["score"]))
|
|
||||||
# Field(
|
|
||||||
# "Created At",
|
|
||||||
# str(datetime.fromtimestamp(result["created_utc"])),
|
|
||||||
# ),
|
|
||||||
fields.append(EmbedField("ID", result["rid"]))
|
|
||||||
embed = build_embed(
|
|
||||||
title=title,
|
|
||||||
description=desc,
|
|
||||||
fields=fields,
|
|
||||||
url=f"https://reddit.com/r/jokes/comments/{result['rid']}",
|
|
||||||
timestamp=datetime.fromtimestamp(result["created_utc"]),
|
|
||||||
)
|
|
||||||
await ctx.send(embed=embed)
|
|
||||||
except Exception:
|
|
||||||
await ctx.send("Encountered error:\n```\n" + traceback.format_exc() + "\n```")
|
|
||||||
# await ctx.send(f"**{result['title']}**\n\n{result['body']}")
|
|
||||||
|
|
||||||
|
|
||||||
def setup(bot: Snake) -> None:
|
|
||||||
"""Add JokeCog to J.A.R.V.I.S."""
|
|
||||||
JokeCog(bot)
|
|
Loading…
Add table
Reference in a new issue