didier/didier/cogs/fun.py

75 lines
2.8 KiB
Python
Raw Normal View History

import shlex
2022-08-25 02:07:02 +02:00
import discord
from discord import app_commands
2022-07-16 00:14:02 +02:00
from discord.ext import commands
from database.crud.dad_jokes import get_random_dad_joke
2022-08-25 11:04:25 +02:00
from database.crud.memes import get_meme_by_name
2022-07-16 00:14:02 +02:00
from didier import Didier
from didier.data.apis.imgflip import generate_meme
2022-08-25 11:04:25 +02:00
from didier.views.modals import GenerateMeme
2022-07-16 00:14:02 +02:00
class Fun(commands.Cog):
"""Cog with lots of random fun stuff"""
client: Didier
2022-08-25 11:04:25 +02:00
# Slash groups
memes_slash = app_commands.Group(name="meme", description="Commands to generate memes", guild_only=False)
2022-08-25 02:07:02 +02:00
2022-07-16 00:14:02 +02:00
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"""
2022-07-25 20:33:20 +02:00
async with self.client.postgres_session as session:
2022-07-16 00:19:05 +02:00
joke = await get_random_dad_joke(session)
return await ctx.reply(joke.joke, mention_author=False)
2022-07-16 00:14:02 +02:00
2022-08-25 02:07:02 +02:00
@commands.group(name="Memegen", aliases=["Meme", "Memes"], invoke_without_command=True, case_insensitive=True)
2022-08-26 18:51:34 +02:00
async def memegen_msg(self, ctx: commands.Context, meme_name: str, *, fields: str):
2022-08-25 02:07:02 +02:00
"""Command group for meme-related commands"""
async with ctx.typing():
async with self.client.postgres_session as session:
result = await get_meme_by_name(session, meme_name)
if result is None:
return await ctx.reply(f"Found no meme matching `{meme_name}`.", mention_author=False)
meme = await generate_meme(self.client.http_session, result, shlex.split(fields))
if meme is None:
return await ctx.reply("Something went wrong.", mention_author=False)
return await ctx.reply(meme)
2022-08-25 02:07:02 +02:00
2022-08-25 11:04:25 +02:00
@memes_slash.command(name="generate", description="Generate a meme")
async def memegen_slash(self, interaction: discord.Interaction, meme: str):
2022-08-25 02:07:02 +02:00
"""Slash command to generate a meme"""
2022-08-25 11:04:25 +02:00
async with self.client.postgres_session as session:
result = await get_meme_by_name(session, meme)
if result is None:
return await interaction.response.send_message(f"Found no meme matching `{meme}`.", ephemeral=True)
2022-08-25 11:04:25 +02:00
modal = GenerateMeme(self.client, result)
await interaction.response.send_modal(modal)
2022-08-25 02:07:02 +02:00
@memegen_slash.autocomplete("meme")
async def _memegen_slash_autocomplete_meme(
self, _: discord.Interaction, current: str
) -> list[app_commands.Choice[str]]:
"""Autocompletion for the 'meme'-parameter"""
return self.client.database_caches.memes.get_autocomplete_suggestions(current)
2022-07-16 00:14:02 +02:00
async def setup(client: Didier):
"""Load the cog"""
await client.add_cog(Fun(client))