mirror of https://github.com/stijndcl/didier
Add dad jokes
parent
3d0f771f94
commit
3debd18d82
|
@ -0,0 +1,33 @@
|
|||
"""Add dad jokes
|
||||
|
||||
Revision ID: 581ae6511b98
|
||||
Revises: 632b69cdadde
|
||||
Create Date: 2022-07-15 23:37:08.147611
|
||||
|
||||
"""
|
||||
import sqlalchemy as sa
|
||||
|
||||
from alembic import op
|
||||
|
||||
# revision identifiers, used by Alembic.
|
||||
revision = "581ae6511b98"
|
||||
down_revision = "632b69cdadde"
|
||||
branch_labels = None
|
||||
depends_on = None
|
||||
|
||||
|
||||
def upgrade() -> None:
|
||||
# ### commands auto generated by Alembic - please adjust! ###
|
||||
op.create_table(
|
||||
"dad_jokes",
|
||||
sa.Column("dad_joke_id", sa.Integer(), nullable=False),
|
||||
sa.Column("joke", sa.Text(), nullable=False),
|
||||
sa.PrimaryKeyConstraint("dad_joke_id"),
|
||||
)
|
||||
# ### end Alembic commands ###
|
||||
|
||||
|
||||
def downgrade() -> None:
|
||||
# ### commands auto generated by Alembic - please adjust! ###
|
||||
op.drop_table("dad_jokes")
|
||||
# ### end Alembic commands ###
|
|
@ -33,6 +33,7 @@ async def create_command(session: AsyncSession, name: str, response: str) -> Cus
|
|||
command = CustomCommand(name=name, indexed_name=clean_name(name), response=response)
|
||||
session.add(command)
|
||||
await session.commit()
|
||||
|
||||
return command
|
||||
|
||||
|
||||
|
|
|
@ -0,0 +1,38 @@
|
|||
from typing import Optional
|
||||
|
||||
from sqlalchemy import func, select
|
||||
from sqlalchemy.ext.asyncio import AsyncSession
|
||||
|
||||
from database.exceptions.not_found import NoResultFoundException
|
||||
from database.models import DadJoke
|
||||
|
||||
__all__ = ["add_dad_joke", "edit_dad_joke", "get_random_dad_joke"]
|
||||
|
||||
|
||||
async def add_dad_joke(session: AsyncSession, joke: str) -> DadJoke:
|
||||
"""Add a new dad joke to the database"""
|
||||
dad_joke = DadJoke(joke=joke)
|
||||
session.add(dad_joke)
|
||||
await session.commit()
|
||||
|
||||
return dad_joke
|
||||
|
||||
|
||||
async def edit_dad_joke(session: AsyncSession, joke_id: int, new_joke: str) -> DadJoke:
|
||||
"""Edit an existing dad joke"""
|
||||
statement = select(DadJoke).where(DadJoke.dad_joke_id == joke_id)
|
||||
dad_joke: Optional[DadJoke] = (await session.execute(statement)).scalar_one_or_none()
|
||||
if dad_joke is None:
|
||||
raise NoResultFoundException
|
||||
|
||||
dad_joke.joke = new_joke
|
||||
session.add(dad_joke)
|
||||
await session.commit()
|
||||
|
||||
return dad_joke
|
||||
|
||||
|
||||
async def get_random_dad_joke(session: AsyncSession) -> DadJoke:
|
||||
"""Return a random database entry"""
|
||||
statement = select(DadJoke).order_by(func.random())
|
||||
return (await session.execute(statement)).first()
|
|
@ -14,6 +14,7 @@ __all__ = [
|
|||
"Bank",
|
||||
"CustomCommand",
|
||||
"CustomCommandAlias",
|
||||
"DadJoke",
|
||||
"NightlyData",
|
||||
"UforaAnnouncement",
|
||||
"UforaCourse",
|
||||
|
@ -73,6 +74,15 @@ class CustomCommandAlias(Base):
|
|||
command: CustomCommand = relationship("CustomCommand", back_populates="aliases", uselist=False, lazy="selectin")
|
||||
|
||||
|
||||
class DadJoke(Base):
|
||||
"""When I finally understood asymptotic notation, it was a big "oh" moment"""
|
||||
|
||||
__tablename__ = "dad_jokes"
|
||||
|
||||
dad_joke_id: int = Column(Integer, primary_key=True)
|
||||
joke: str = Column(Text, nullable=False)
|
||||
|
||||
|
||||
class NightlyData(Base):
|
||||
"""Data for a user's Nightly stats"""
|
||||
|
||||
|
|
|
@ -0,0 +1,29 @@
|
|||
from discord.ext import commands
|
||||
|
||||
from database.crud.dad_jokes import get_random_dad_joke
|
||||
from didier import Didier
|
||||
|
||||
|
||||
class Fun(commands.Cog):
|
||||
"""Cog with lots of random fun stuff"""
|
||||
|
||||
client: Didier
|
||||
|
||||
def __init__(self, client: Didier):
|
||||
self.client = client
|
||||
|
||||
@commands.hybrid_command(
|
||||
name="dadjoke",
|
||||
aliases=["Dad", "Dj"],
|
||||
description="Why does Yoda's code always crash? Because there is no try.",
|
||||
)
|
||||
async def dad_joke(self, ctx: commands.Context):
|
||||
"""Get a random dad joke"""
|
||||
async with self.client.db_session as session:
|
||||
row = await get_random_dad_joke(session)
|
||||
return await ctx.reply(row[0].joke, mention_author=False)
|
||||
|
||||
|
||||
async def setup(client: Didier):
|
||||
"""Load the cog"""
|
||||
await client.add_cog(Fun(client))
|
|
@ -9,7 +9,7 @@ from database.exceptions.constraints import DuplicateInsertException
|
|||
from database.exceptions.not_found import NoResultFoundException
|
||||
from didier import Didier
|
||||
from didier.data.flags.owner import EditCustomFlags
|
||||
from didier.data.modals.custom_commands import CreateCustomCommand, EditCustomCommand
|
||||
from didier.views.modals import AddDadJoke, CreateCustomCommand, EditCustomCommand
|
||||
|
||||
|
||||
class Owner(commands.Cog):
|
||||
|
@ -29,7 +29,6 @@ class Owner(commands.Cog):
|
|||
|
||||
This means that we don't have to add is_owner() to every single command separately
|
||||
"""
|
||||
# pylint: disable=W0236 # Pylint thinks this can't be async, but it can
|
||||
return await self.client.is_owner(ctx.author)
|
||||
|
||||
@commands.command(name="Error")
|
||||
|
@ -48,7 +47,7 @@ class Owner(commands.Cog):
|
|||
|
||||
await ctx.message.add_reaction("🔄")
|
||||
|
||||
@commands.group(name="Add", case_insensitive=True, invoke_without_command=False)
|
||||
@commands.group(name="Add", aliases=["Create"], case_insensitive=True, invoke_without_command=False)
|
||||
async def add_msg(self, ctx: commands.Context):
|
||||
"""Command group for [add X] message commands"""
|
||||
|
||||
|
@ -88,6 +87,17 @@ class Owner(commands.Cog):
|
|||
modal = CreateCustomCommand(self.client)
|
||||
await interaction.response.send_modal(modal)
|
||||
|
||||
@add_slash.command(name="dadjoke", description="Add a dad joke")
|
||||
async def add_dad_joke_slash(self, interaction: discord.Interaction):
|
||||
"""Slash command to add a dad joke"""
|
||||
if not await self.client.is_owner(interaction.user):
|
||||
return interaction.response.send_message(
|
||||
"Je hebt geen toestemming om dit commando uit te voeren.", ephemeral=True
|
||||
)
|
||||
|
||||
modal = AddDadJoke(self.client)
|
||||
await interaction.response.send_modal(modal)
|
||||
|
||||
@commands.group(name="Edit", case_insensitive=True, invoke_without_command=False)
|
||||
async def edit_msg(self, ctx: commands.Context):
|
||||
"""Command group for [edit X] commands"""
|
||||
|
|
|
@ -14,7 +14,6 @@ class Tasks(commands.Cog):
|
|||
client: Didier
|
||||
|
||||
def __init__(self, client: Didier):
|
||||
# pylint: disable=no-member
|
||||
self.client = client
|
||||
|
||||
# Only pull announcements if a token was provided
|
||||
|
|
|
@ -0,0 +1,4 @@
|
|||
from .custom_commands import CreateCustomCommand, EditCustomCommand
|
||||
from .dad_jokes import AddDadJoke
|
||||
|
||||
__all__ = ["AddDadJoke", "CreateCustomCommand", "EditCustomCommand"]
|
|
@ -0,0 +1,37 @@
|
|||
import traceback
|
||||
|
||||
import discord
|
||||
from overrides import overrides
|
||||
|
||||
from database.crud.dad_jokes import add_dad_joke
|
||||
from didier import Didier
|
||||
|
||||
__all__ = ["AddDadJoke"]
|
||||
|
||||
|
||||
class AddDadJoke(discord.ui.Modal, title="Add Dad Joke"):
|
||||
"""Modal to add a new dad joke"""
|
||||
|
||||
name: discord.ui.TextInput = discord.ui.TextInput(
|
||||
label="Joke",
|
||||
placeholder="I sold our vacuum cleaner, it was just gathering dust.",
|
||||
style=discord.TextStyle.long,
|
||||
)
|
||||
|
||||
client: Didier
|
||||
|
||||
def __init__(self, client: Didier, *args, **kwargs):
|
||||
super().__init__(*args, **kwargs)
|
||||
self.client = client
|
||||
|
||||
@overrides
|
||||
async def on_submit(self, interaction: discord.Interaction):
|
||||
async with self.client.db_session as session:
|
||||
joke = await add_dad_joke(session, self.name.value)
|
||||
|
||||
await interaction.response.send_message(f"Successfully added joke #{joke.dad_joke_id}", ephemeral=True)
|
||||
|
||||
@overrides
|
||||
async def on_error(self, interaction: discord.Interaction, error: Exception): # type: ignore
|
||||
await interaction.response.send_message("Something went wrong.", ephemeral=True)
|
||||
traceback.print_tb(error.__traceback__)
|
Loading…
Reference in New Issue