Implement /schedule

Allows matchers to schedule repeated weekly runs on a given weekday and hour

Can schedule multiple runs

Scheduled runs can be cancelled with cancel:True in the command

/list also shows any scheduled commands in that channel
This commit is contained in:
Marc Di Luzio 2024-08-12 23:00:49 +01:00
parent 522f89cff9
commit 07485ceb8d
4 changed files with 244 additions and 44 deletions

View file

@ -1,4 +1,4 @@
from datetime import datetime
from datetime import datetime, timedelta
def get_day_with_suffix(day):
@ -9,11 +9,15 @@ def get_day_with_suffix(day):
return str(day) + {1: 'st', 2: 'nd', 3: 'rd'}.get(day % 10, 'th')
def format_today():
def format_today() -> str:
"""Format the current datetime"""
now = datetime.now()
num = get_day_with_suffix(now.day)
day = now.strftime("%a")
return format_day(datetime.now())
def format_day(time: datetime) -> str:
"""Format the a given datetime"""
num = get_day_with_suffix(time.day)
day = time.strftime("%a")
return f"{day} {num}"
@ -22,3 +26,19 @@ def format_list(list) -> str:
if len(list) > 1:
return f"{', '.join(list[:-1])} and {list[-1]}"
return list[0] if list else ''
def get_next_datetime(weekday, hour) -> datetime:
"""Get the next datetime for the given weekday and hour"""
now = datetime.now()
days_until_next_week = (weekday - now.weekday() + 7) % 7
# Account for when we're already beyond the time now
if days_until_next_week == 0 and now.hour >= hour:
days_until_next_week = 7
# Calculate the next datetime
next_date = now + timedelta(days=days_until_next_week)
next_date.replace(hour=hour)
return next_date