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 random import randint
from discord.ext import commands
from discord_slash import SlashContext, cog_ext
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.db.models import Joke
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.
May adapt over time to create jokes using machine learning
"""
def __init__(self, bot: commands.Bot):
def __init__(self, bot: Snake):
self.bot = bot
# TODO: Make this a command group with subcommands
@cog_ext.cog_slash(
@slash_command(
name="joke",
description="Hear a joke",
)
@commands.cooldown(1, 10, commands.BucketType.channel)
async def _joke(self, ctx: SlashContext, id: str = None) -> None:
@slash_option(name="id", description="Joke ID", required=False, option_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
@ -63,7 +70,7 @@ class JokeCog(commands.Cog):
body = ""
for word in result["body"].split(" "):
if len(body) + 1 + len(word) > 1024:
body_chunks.append(Field("", body, False))
body_chunks.append(EmbedField("", body, False))
body = ""
if word == "\n" and body == "":
continue
@ -87,15 +94,15 @@ class JokeCog(commands.Cog):
else:
desc += word + " "
body_chunks.append(Field("", body, False))
body_chunks.append(EmbedField("", body, False))
fields = body_chunks
fields.append(Field("Score", result["score"]))
fields.append(EmbedField("Score", result["score"]))
# Field(
# "Created At",
# str(datetime.fromtimestamp(result["created_utc"])),
# ),
fields.append(Field("ID", result["rid"]))
fields.append(EmbedField("ID", result["rid"]))
embed = build_embed(
title=title,
description=desc,
@ -109,6 +116,6 @@ class JokeCog(commands.Cog):
# 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."""
bot.add_cog(JokeCog(bot))