Easter eggs

This commit is contained in:
stijndcl 2022-09-20 00:31:33 +02:00
parent 2638c5a3c4
commit 181118aa1d
7 changed files with 151 additions and 13 deletions

View file

@ -0,0 +1,12 @@
from sqlalchemy import select
from sqlalchemy.ext.asyncio import AsyncSession
from database.schemas import EasterEgg
__all__ = ["get_all_easter_eggs"]
async def get_all_easter_eggs(session: AsyncSession) -> list[EasterEgg]:
"""Return a list of all easter eggs"""
statement = select(EasterEgg)
return (await session.execute(statement)).scalars().all()

View file

@ -31,6 +31,7 @@ __all__ = [
"CustomCommandAlias",
"DadJoke",
"Deadline",
"EasterEgg",
"Link",
"MemeTemplate",
"NightlyData",
@ -144,6 +145,18 @@ class Deadline(Base):
course: UforaCourse = relationship("UforaCourse", back_populates="deadlines", uselist=False, lazy="selectin")
class EasterEgg(Base):
"""An easter egg response"""
__tablename__ = "easter_eggs"
easter_egg_id: int = Column(Integer, primary_key=True)
match: str = Column(Text, nullable=False)
response: str = Column(Text, nullable=False)
exact: bool = Column(Boolean, nullable=False, server_default="1")
startswith: bool = Column(Boolean, nullable=False, server_default="1")
class Link(Base):
"""Useful links that go useful places"""

View file

@ -4,11 +4,10 @@ from discord import app_commands
from overrides import overrides
from sqlalchemy.ext.asyncio import AsyncSession
from database.crud import links, memes, ufora_courses, wordle
from database.crud import easter_eggs, links, memes, ufora_courses, wordle
from database.schemas import EasterEgg, WordleWord
__all__ = ["CacheManager", "LinkCache", "UforaCourseCache"]
from database.schemas import WordleWord
__all__ = ["CacheManager", "EasterEggCache", "LinkCache", "UforaCourseCache"]
class DatabaseCache(ABC):
@ -46,6 +45,22 @@ class DatabaseCache(ABC):
return [app_commands.Choice(name=suggestion, value=suggestion.lower()) for suggestion in suggestions]
class EasterEggCache(DatabaseCache):
"""Cache to store easter eggs invoked by messages"""
easter_eggs: list[EasterEgg] = []
@overrides
async def clear(self):
self.easter_eggs.clear()
@overrides
async def invalidate(self, database_session: AsyncSession):
"""Invalidate the data stored in this cache"""
await self.clear()
self.easter_eggs = await easter_eggs.get_all_easter_eggs(database_session)
class LinkCache(DatabaseCache):
"""Cache to store the names of links"""
@ -131,12 +146,14 @@ class WordleCache(DatabaseCache):
class CacheManager:
"""Class that keeps track of all caches"""
easter_eggs: EasterEggCache
links: LinkCache
memes: MemeCache
ufora_courses: UforaCourseCache
wordle_word: WordleCache
def __init__(self):
self.easter_eggs = EasterEggCache()
self.links = LinkCache()
self.memes = MemeCache()
self.ufora_courses = UforaCourseCache()
@ -144,6 +161,7 @@ class CacheManager:
async def initialize_caches(self, postgres_session: AsyncSession):
"""Initialize the contents of all caches"""
await self.easter_eggs.invalidate(postgres_session)
await self.links.invalidate(postgres_session)
await self.memes.invalidate(postgres_session)
await self.ufora_courses.invalidate(postgres_session)