diff --git a/cogs/school.py b/cogs/school.py index 3768a74..b22482d 100644 --- a/cogs/school.py +++ b/cogs/school.py @@ -4,7 +4,8 @@ import discord from discord.ext import commands from enums.help_categories import Category from functions import config, eten, les -from functions.timeFormatters import intToWeekday +from functions.stringFormatters import capitalize +from functions.timeFormatters import intToWeekday, skip_weekends class School(commands.Cog): @@ -21,6 +22,7 @@ class School(commands.Cog): @help.Category(category=Category.School) async def eten(self, ctx, *day): day_dt = les.find_target_date(day if day else None) + day_dt = skip_weekends(day_dt) day = intToWeekday(day_dt.weekday()) # Create embed @@ -44,6 +46,13 @@ class School(commands.Cog): @help.Category(category=Category.School) async def les(self, ctx, day=None): date = les.find_target_date(day) + + # Person explicitly requested a weekend-day + if day is not None and day.lower() in ("morgen", "overmorgen") and date.weekday() > 4: + return await ctx.send(f"{capitalize(day)} is het weekend.") + + date = skip_weekends(date) + s = schedule.Schedule(date, int(config.get("year")), int(config.get("semester")), day is not None) return await ctx.send(embed=s.create_schedule().to_embed()) diff --git a/functions/les.py b/functions/les.py new file mode 100644 index 0000000..c3a7cfb --- /dev/null +++ b/functions/les.py @@ -0,0 +1,27 @@ +from datetime import datetime, timedelta +from functions.timeFormatters import dateTimeNow, weekdayToInt, forward_to_weekday +from typing import Optional + + +def find_target_date(arg: Optional[str]) -> datetime: + """ + Find the requested date out of the user's arguments + """ + # Start at current date + day: datetime = dateTimeNow() + + # If no offset was provided, check the time + # otherwise the argument overrides it + if arg is None: + # When the command is used after 6 pm, show schedule + # for the next day instead + if day.hour > 18: + day += timedelta(days=1) + elif 0 <= (weekday := weekdayToInt(arg)) <= 4: # Weekday provided + day = forward_to_weekday(day, weekday) + elif arg.lower() == "morgen": # Tomorrow's schedule + day += timedelta(days=1) + elif arg.lower() == "overmorgen": # Day after tomorrow's schedule + day += timedelta(days=2) + + return day