mirror of https://github.com/stijndcl/didier
Use transformer
parent
e1af53cf44
commit
2de75fd168
|
@ -1,4 +1,4 @@
|
|||
from datetime import datetime
|
||||
from datetime import date
|
||||
from typing import Optional
|
||||
|
||||
import discord
|
||||
|
@ -12,6 +12,7 @@ from didier.data.apis.hydra import fetch_menu
|
|||
from didier.data.embeds.deadlines import Deadlines
|
||||
from didier.data.embeds.hydra import no_menu_found
|
||||
from didier.exceptions import HTTPException
|
||||
from didier.utils.discord.converters.time import DateTransformer
|
||||
from didier.utils.discord.flags.school import StudyGuideFlags
|
||||
|
||||
|
||||
|
@ -37,16 +38,13 @@ class School(commands.Cog):
|
|||
description="Show the menu in the Ghent University restaurants.",
|
||||
aliases=["Eten", "Food"],
|
||||
)
|
||||
async def menu(self, ctx: commands.Context, day: Optional[str] = None):
|
||||
@app_commands.rename(day_dt="date")
|
||||
async def menu(self, ctx: commands.Context, day_dt: Optional[app_commands.Transform[date, DateTransformer]] = None):
|
||||
"""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 autocompletion for [DAY]
|
||||
async with ctx.typing():
|
||||
day_dt = datetime(year=2022, month=8, day=29)
|
||||
|
||||
try:
|
||||
menu = await fetch_menu(self.client.http_session, day_dt)
|
||||
embed = menu.to_embed(day_dt=day_dt)
|
||||
|
|
|
@ -1,4 +1,4 @@
|
|||
from datetime import datetime
|
||||
from datetime import date
|
||||
|
||||
from aiohttp import ClientSession
|
||||
|
||||
|
@ -8,7 +8,7 @@ from didier.utils.http.requests import ensure_get
|
|||
__all__ = ["fetch_menu"]
|
||||
|
||||
|
||||
async def fetch_menu(http_session: ClientSession, day_dt: datetime) -> Menu:
|
||||
async def fetch_menu(http_session: ClientSession, day_dt: date) -> Menu:
|
||||
"""Fetch the menu for a given day"""
|
||||
endpoint = f"https://hydra.ugent.be/api/2.0/resto/menu/nl/{day_dt.year}/{day_dt.month}/{day_dt.day}.json"
|
||||
async with ensure_get(http_session, endpoint, log_exceptions=False) as response:
|
||||
|
|
|
@ -0,0 +1,31 @@
|
|||
from discord import app_commands
|
||||
|
||||
__all__ = ["autocomplete_day"]
|
||||
|
||||
|
||||
def autocomplete_day(argument: str) -> list[app_commands.Choice]:
|
||||
"""Autocompletion for day-arguments
|
||||
|
||||
This supports relative offsets ("tomorrow") as well as weekdays
|
||||
"""
|
||||
argument = argument.lower()
|
||||
values = [
|
||||
"Tomorrow",
|
||||
"Monday",
|
||||
"Tuesday",
|
||||
"Wednesday",
|
||||
"Thursday",
|
||||
"Friday",
|
||||
"Sunday",
|
||||
"Morgen",
|
||||
"Overmorgen",
|
||||
"Maandag",
|
||||
"Dinsdag",
|
||||
"Woensdag",
|
||||
"Donderdag",
|
||||
"Vrijdag",
|
||||
"Zaterdag",
|
||||
"Zondag",
|
||||
]
|
||||
|
||||
return [app_commands.Choice(name=value, value=value.lower()) for value in values if argument in value.lower()]
|
|
@ -1,9 +1,14 @@
|
|||
import contextlib
|
||||
import datetime
|
||||
from datetime import date, timedelta
|
||||
from typing import Optional
|
||||
from typing import Optional, Union
|
||||
|
||||
import discord
|
||||
from discord import app_commands
|
||||
from discord.ext.commands import ArgumentParsingError
|
||||
from overrides import overrides
|
||||
|
||||
from didier.utils.discord.autocompletion.time import autocomplete_day
|
||||
from didier.utils.types.datetime import (
|
||||
forward_to_next_weekday,
|
||||
parse_dm_string,
|
||||
|
@ -46,3 +51,16 @@ def date_converter(argument: Optional[str]) -> date:
|
|||
|
||||
# Unparseable
|
||||
raise ArgumentParsingError(f"Unable to interpret `{original_argument}` as a date.")
|
||||
|
||||
|
||||
class DateTransformer(app_commands.Transformer):
|
||||
"""Application commands transformer for dates"""
|
||||
|
||||
async def autocomplete(
|
||||
self, interaction: discord.Interaction, value: Union[int, float, str]
|
||||
) -> list[app_commands.Choice[Union[int, float, str]]]:
|
||||
return autocomplete_day(str(value))
|
||||
|
||||
@overrides
|
||||
async def transform(self, interaction: discord.Interaction, value: str) -> datetime.date:
|
||||
return date_converter(value)
|
||||
|
|
Loading…
Reference in New Issue