Menu message & slash commands

pull/128/head
stijndcl 2022-08-28 22:44:53 +02:00
parent 0186a0793a
commit b581c3e5dc
2 changed files with 90 additions and 7 deletions

View File

@ -33,14 +33,19 @@ class School(commands.Cog):
await ctx.reply(embed=embed, mention_author=False, ephemeral=False) await ctx.reply(embed=embed, mention_author=False, ephemeral=False)
@commands.hybrid_command( @commands.hybrid_command(
name="menu", description="Show the menu in the Ghent University restaurants", aliases=["Eten", "Food"] name="menu",
description="Show the menu in the Ghent University restaurants.",
aliases=["Eten", "Food"],
) )
async def menu(self, ctx: commands.Context, day: Optional[str] = None): async def menu(self, ctx: commands.Context, day: Optional[str] = None):
"""Get the menu for a given day in the restaurants""" """Show the menu in the Ghent University restaurants.
Menus are Dutch, as a lot of dishes have very weird translations
"""
# TODO time converter (transformer) for [DAY] # TODO time converter (transformer) for [DAY]
# TODO autocompletion for [DAY] # TODO autocompletion for [DAY]
async with ctx.typing(): async with ctx.typing():
day_dt = datetime.now() day_dt = datetime(year=2022, month=8, day=29)
try: try:
menu = await fetch_menu(self.client.http_session, day_dt) menu = await fetch_menu(self.client.http_session, day_dt)

View File

@ -1,25 +1,40 @@
from datetime import datetime from datetime import datetime
from typing import Literal, Optional, cast from enum import Enum
from typing import Optional, cast
import discord import discord
from overrides import overrides from overrides import overrides
from pydantic import BaseModel from pydantic import BaseModel
from didier.data.embeds.base import EmbedPydantic from didier.data.embeds.base import EmbedPydantic
from didier.utils.discord.colours import ghent_university_blue from didier.utils.discord.colours import ghent_university_blue, ghent_university_yellow
from didier.utils.types.datetime import int_to_weekday from didier.utils.types.datetime import int_to_weekday
from didier.utils.types.string import leading from didier.utils.types.string import leading
__all__ = ["Menu", "no_menu_found"] __all__ = ["Menu", "no_menu_found"]
class _MealKind(str, Enum):
FISH = "fish"
MEAT = "meat"
SOUP = "soup"
VEGAN = "vegan"
VEGETARIAN = "vegetarian"
class _MealType(str, Enum):
COLD = "cold"
MAIN = "main"
SIDE = "side"
class _Meal(BaseModel): class _Meal(BaseModel):
"""Model for an item on the menu""" """Model for an item on the menu"""
kind: Literal["meat", "fish", "soup", "vegetarian", "vegan"] kind: _MealKind
name: str name: str
price: str price: str
type: Literal["cold", "main", "side"] type: _MealType
class Menu(EmbedPydantic): class Menu(EmbedPydantic):
@ -30,6 +45,64 @@ class Menu(EmbedPydantic):
vegetables: list[str] = [] vegetables: list[str] = []
message: Optional[str] = None message: Optional[str] = None
def _get_dutch_meal_prefix(self, meal: _Meal) -> str:
if meal.kind == _MealKind.MEAT:
prefix = "Vlees"
elif meal.kind == _MealKind.FISH:
prefix = "Vis"
elif meal.kind == _MealKind.VEGETARIAN:
prefix = "Vegetarisch"
else:
prefix = "Vegan"
return prefix
def _get_soups(self) -> str:
acc = ""
for meal in self.meals:
if meal.kind == _MealKind.SOUP:
acc += f"{meal.name} ({meal.price})\n"
return acc.strip()
def _get_main_courses(self) -> str:
acc = ""
for meal in self.meals:
if meal.type != _MealType.MAIN:
continue
prefix = self._get_dutch_meal_prefix(meal)
acc += f"* {prefix}: {meal.name} ({meal.price})\n"
return acc.strip()
def _get_cold_meals(self) -> str:
acc = ""
for meal in self.meals:
if meal.type == _MealType.COLD:
acc += f"* {self._get_dutch_meal_prefix(meal)}: {meal.name} ({meal.price})\n"
return acc.strip()
def _closed_embed(self, embed: discord.Embed) -> discord.Embed:
embed.colour = ghent_university_yellow()
embed.description = "The restaurants are closed today."
return embed
def _regular_embed(self, embed: discord.Embed) -> discord.Embed:
embed.add_field(name="🥣 Soep", value=self._get_soups(), inline=False)
embed.add_field(name="🍴 Hoofdgerechten", value=self._get_main_courses(), inline=False)
embed.add_field(name="Koud", value=self._get_cold_meals(), inline=False)
vegetables = "\n".join(list(sorted(self.vegetables)))
embed.add_field(name="🥦 Groenten", value=vegetables, inline=False)
return embed
@overrides @overrides
def to_embed(self, **kwargs) -> discord.Embed: def to_embed(self, **kwargs) -> discord.Embed:
day_dt: datetime = cast(datetime, kwargs.get("day_dt")) day_dt: datetime = cast(datetime, kwargs.get("day_dt"))
@ -38,6 +111,11 @@ class Menu(EmbedPydantic):
embed = discord.Embed(title=f"Menu - {weekday} {formatted_date}", colour=ghent_university_blue()) embed = discord.Embed(title=f"Menu - {weekday} {formatted_date}", colour=ghent_university_blue())
embed = self._regular_embed(embed) if self.open else self._closed_embed(embed)
if self.message:
embed.add_field(name="📣 Extra Mededeling", value=self.message, inline=False)
return embed return embed