Skip to content

Commit

Permalink
Disable breaks system in-between semesters
Browse files Browse the repository at this point in the history
  • Loading branch information
cbrxyz committed Dec 7, 2023
1 parent d721d9b commit 5fa192f
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 5 deletions.
14 changes: 12 additions & 2 deletions src/helper.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
import asyncio
import datetime
import logging
from collections.abc import Callable, Coroutine
from collections.abc import Awaitable, Callable, Coroutine
from functools import wraps
from typing import Any, TypeVar

from discord.utils import maybe_coroutine

T = TypeVar("T")
logger = logging.getLogger(__name__)

Expand All @@ -16,6 +18,7 @@ def run_on_weekday(
hour: int,
minute: int,
shift: datetime.timedelta | None = None,
check: Callable[[], bool | Awaitable[bool]] | None = None,
):
"""
Runs the decorated function on the next instance of the specified weekday.
Expand All @@ -32,7 +35,7 @@ def run_on_weekday(

def decorator(func: Callable[..., Coroutine[Any, Any, T]]): # type: ignore
@wraps(func)
async def wrapper(*args, **kwargs) -> T:
async def wrapper(*args, **kwargs) -> T | None:
# Sleep until the next instance of the weekday
# at the specified time
now = datetime.datetime.now()
Expand All @@ -53,6 +56,13 @@ async def wrapper(*args, **kwargs) -> T:
# Find next time to run the function (next week)
_tasks.add(asyncio.create_task(wrapper(*args, **kwargs)))

# Check if the function should be run again
if check and not await maybe_coroutine(check):
logger.info(
f"Skipping {func.__name__} until next week because check failed.",
)
return

# Run the function
try:
return await func(*args, **kwargs)
Expand Down
29 changes: 26 additions & 3 deletions src/reports.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,23 @@
logger = logging.getLogger(__name__)


def is_active() -> bool:
"""
Whether the reports system is active.
"""
semesters = [
(datetime.date(2023, 8, 23), datetime.date(2023, 12, 6)),
(datetime.date(2024, 1, 8), datetime.date(2024, 4, 24)),
(datetime.date(2024, 5, 13), datetime.date(2024, 8, 9)),
]
for semester in semesters:
if semester[0] <= datetime.date.today() <= semester[1]:
return True
if datetime.date.today() <= semester[0]:
return False
return False


class ReportsModal(discord.ui.Modal):
name = discord.ui.TextInput(label="Name", placeholder="Albert Gator")
ufid = discord.ui.TextInput(
Expand Down Expand Up @@ -152,6 +169,12 @@ async def submit(self, interaction: discord.Interaction, _: discord.ui.Button):
ephemeral=True,
)

if not is_active():
return await interaction.response.send_message(
"❌ The weekly reports system is currently inactive due to the interim period between semesters. Please wait until next semester to document any work you have completed in between semesters. Thank you!",
ephemeral=True,
)

# Send modal where user fills out report
await interaction.response.send_modal(ReportsModal(self.bot))

Expand All @@ -167,14 +190,14 @@ def __init__(self, bot: MILBot):
self._tasks.add(self.bot.loop.create_task(self.add_no()))
self._tasks.add(self.bot.loop.create_task(self.individual_reminder()))

@run_on_weekday(calendar.FRIDAY, 12, 0)
@run_on_weekday(calendar.FRIDAY, 12, 0, check=is_active)
async def post_reminder(self):
general_channel = self.bot.general_channel
return await general_channel.send(
f"{self.bot.egn4912_role.mention}\nHey everyone! Friendly reminder to submit your weekly progress reports by **tomorrow night at 11:59pm**. You can submit your reports in the {self.bot.reports_channel.mention} channel. If you have any questions, please contact your leader. Thank you!",
)

@run_on_weekday(calendar.SATURDAY, 12, 0)
@run_on_weekday(calendar.SATURDAY, 12, 0, check=is_active)
async def individual_reminder(self):
# Get all members who have not completed reports for the week
main_worksheet = await self.bot.sh.get_worksheet(0)
Expand Down Expand Up @@ -206,7 +229,7 @@ async def individual_reminder(self):
f"Could not send individual report reminder to {member}.",
)

@run_on_weekday(calendar.SUNDAY, 0, 0)
@run_on_weekday(calendar.SUNDAY, 0, 0, check=is_active)
async def add_no(self):
main_worksheet = await self.bot.sh.get_worksheet(0)

Expand Down

0 comments on commit 5fa192f

Please sign in to comment.