List memes

pull/134/head
stijndcl 2022-09-22 17:28:23 +02:00
parent 6cfe788df5
commit 00a146cb2b
5 changed files with 48 additions and 3 deletions

View File

@ -5,10 +5,12 @@ from discord import app_commands
from discord.ext import commands from discord.ext import commands
from database.crud.dad_jokes import get_random_dad_joke from database.crud.dad_jokes import get_random_dad_joke
from database.crud.memes import get_meme_by_name from database.crud.memes import get_all_memes, get_meme_by_name
from didier import Didier from didier import Didier
from didier.data.apis.imgflip import generate_meme from didier.data.apis.imgflip import generate_meme
from didier.exceptions.no_match import expect from didier.exceptions.no_match import expect
from didier.menus.common import Menu
from didier.menus.memes import MemeSource
from didier.views.modals import GenerateMeme from didier.views.modals import GenerateMeme
@ -58,6 +60,19 @@ class Fun(commands.Cog):
meme = await self._do_generate_meme(template, shlex.split(fields)) meme = await self._do_generate_meme(template, shlex.split(fields))
return await ctx.reply(meme, mention_author=False) return await ctx.reply(meme, mention_author=False)
@memegen_msg.command(name="list", aliases=["ls"])
async def memegen_ls_msg(self, ctx: commands.Context):
"""Get a list of all available meme templates.
This command does _not_ have a /slash variant, as the memegen /slash commands provide autocompletion.
"""
async with self.client.postgres_session as session:
results = await get_all_memes(session)
source = MemeSource(ctx, results)
menu = Menu(source)
await menu.start(ctx)
@memegen_msg.command(name="preview", aliases=["p"]) @memegen_msg.command(name="preview", aliases=["p"])
async def memegen_preview_msg(self, ctx: commands.Context, template: str): async def memegen_preview_msg(self, ctx: commands.Context, template: str):
"""Generate a preview for the meme template `template`, to see how the fields are structured.""" """Generate a preview for the meme template `template`, to see how the fields are structured."""

View File

@ -22,7 +22,7 @@ class BookmarkSource(PageSource[Bookmark]):
description = "" description = ""
for bookmark in self.dataset[page : page + self.per_page]: for bookmark in self.get_page_data(page):
description += f"`#{bookmark.bookmark_id}`: [{bookmark.label}]({bookmark.jump_url})\n" description += f"`#{bookmark.bookmark_id}`: [{bookmark.label}]({bookmark.jump_url})\n"
embed.description = description.strip() embed.description = description.strip()

View File

@ -52,6 +52,10 @@ class PageSource(ABC, Generic[T]):
"""Method that builds the list of embeds from the input data""" """Method that builds the list of embeds from the input data"""
raise NotImplementedError raise NotImplementedError
def get_page_data(self, page: int) -> list[T]:
"""Get the chunk of the dataset for page [page]"""
return self.dataset[page : page + self.per_page]
class Menu(discord.ui.View): class Menu(discord.ui.View):
"""Base class for a menu""" """Base class for a menu"""

View File

@ -0,0 +1,27 @@
import discord
from discord.ext import commands
from overrides import overrides
from database.schemas import MemeTemplate
from didier.menus.common import PageSource
__all__ = ["MemeSource"]
class MemeSource(PageSource[MemeTemplate]):
"""PageSource for meme templates"""
@overrides
def create_embeds(self, ctx: commands.Context):
for page in range(self.page_count):
# The colour of the embed is (69,4,20) with the values +100 because they were too dark
embed = discord.Embed(title="Meme Templates", colour=discord.Colour.from_rgb(169, 14, 120))
description_data = []
for template in self.get_page_data(page):
description_data.append(f"{template.name} ({template.field_count})")
embed.description = "\n".join(description_data)
embed.set_footer(text="Format: Template Name (Field Count)")
self.embeds.append(embed)

View File

@ -3,7 +3,6 @@ alembic==1.8.0
asyncpg==0.25.0 asyncpg==0.25.0
beautifulsoup4==4.11.1 beautifulsoup4==4.11.1
discord.py==2.0.1 discord.py==2.0.1
git+https://github.com/Rapptz/discord-ext-menus@8686b5d
environs==9.5.0 environs==9.5.0
feedparser==6.0.10 feedparser==6.0.10
ics==0.7.2 ics==0.7.2