Migrate jokes, closes #98

This commit is contained in:
Zeva Rose 2022-02-03 06:27:24 -07:00
parent a23c28e551
commit 3489fb1ec3

View file

@ -5,31 +5,38 @@ import traceback
from datetime import datetime from datetime import datetime
from random import randint from random import randint
from discord.ext import commands from dis_snek import InteractionContext, Scale, Snake
from discord_slash import SlashContext, cog_ext 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.db.models import Joke from jarvis.db.models import Joke
from jarvis.utils import build_embed from jarvis.utils import build_embed
from jarvis.utils.field import Field
class JokeCog(commands.Cog): class JokeCog(Scale):
""" """
Joke library for J.A.R.V.I.S. Joke library for J.A.R.V.I.S.
May adapt over time to create jokes using machine learning May adapt over time to create jokes using machine learning
""" """
def __init__(self, bot: commands.Bot): def __init__(self, bot: Snake):
self.bot = bot self.bot = bot
# TODO: Make this a command group with subcommands # TODO: Make this a command group with subcommands
@cog_ext.cog_slash( @slash_command(
name="joke", name="joke",
description="Hear a joke", description="Hear a joke",
) )
@commands.cooldown(1, 10, commands.BucketType.channel) @slash_option(name="id", description="Joke ID", required=False, option_type=OptionTypes.INTEGER)
async def _joke(self, ctx: SlashContext, id: str = None) -> None: @cooldown(bucket=Buckets.CHANNEL, rate=1, interval=10)
async def _joke(self, ctx: InteractionContext, id: str = None) -> None:
"""Get a joke from the database.""" """Get a joke from the database."""
try: try:
if randint(1, 100_000) == 5779 and id is None: # noqa: S311 if randint(1, 100_000) == 5779 and id is None: # noqa: S311
@ -63,7 +70,7 @@ class JokeCog(commands.Cog):
body = "" body = ""
for word in result["body"].split(" "): for word in result["body"].split(" "):
if len(body) + 1 + len(word) > 1024: if len(body) + 1 + len(word) > 1024:
body_chunks.append(Field("", body, False)) body_chunks.append(EmbedField("", body, False))
body = "" body = ""
if word == "\n" and body == "": if word == "\n" and body == "":
continue continue
@ -87,15 +94,15 @@ class JokeCog(commands.Cog):
else: else:
desc += word + " " desc += word + " "
body_chunks.append(Field("", body, False)) body_chunks.append(EmbedField("", body, False))
fields = body_chunks fields = body_chunks
fields.append(Field("Score", result["score"])) fields.append(EmbedField("Score", result["score"]))
# Field( # Field(
# "Created At", # "Created At",
# str(datetime.fromtimestamp(result["created_utc"])), # str(datetime.fromtimestamp(result["created_utc"])),
# ), # ),
fields.append(Field("ID", result["rid"])) fields.append(EmbedField("ID", result["rid"]))
embed = build_embed( embed = build_embed(
title=title, title=title,
description=desc, description=desc,
@ -109,6 +116,6 @@ class JokeCog(commands.Cog):
# await ctx.send(f"**{result['title']}**\n\n{result['body']}") # await ctx.send(f"**{result['title']}**\n\n{result['body']}")
def setup(bot: commands.Bot) -> None: def setup(bot: Snake) -> None:
"""Add JokeCog to J.A.R.V.I.S.""" """Add JokeCog to J.A.R.V.I.S."""
bot.add_cog(JokeCog(bot)) bot.add_cog(JokeCog(bot))