Fixed handling of event dates (for #2)

This commit is contained in:
2025-05-02 12:20:24 +02:00
parent 80eae3f1b1
commit 5507295b1b
2 changed files with 29 additions and 11 deletions

View File

@@ -106,7 +106,7 @@ class CogEvent(Cog):
)
return
if not await validate_event_validity(ctx, name, start_date, end_date, guild_timezone):
if not await validate_event_validity(ctx, name, start_date, end_date, to_utc=True):
return
processed_media: List[Dict[str, Any]] = (
@@ -204,7 +204,12 @@ class CogEvent(Cog):
start_date: datetime = (
pycord_event.starts if start is None else datetime.strptime(start, "%d.%m.%Y %H:%M")
)
start_date = start_date.replace(tzinfo=guild_timezone)
start_date = (
start_date.replace(tzinfo=ZoneInfo("UTC"))
if start is None
else start_date.replace(tzinfo=guild_timezone)
)
except ValueError:
# TODO Make a nice message
await ctx.respond(
@@ -216,7 +221,12 @@ class CogEvent(Cog):
end_date: datetime = (
pycord_event.ends if end is None else datetime.strptime(end, "%d.%m.%Y %H:%M")
)
end_date = end_date.replace(tzinfo=guild_timezone)
end_date = (
end_date.replace(tzinfo=ZoneInfo("UTC"))
if end is None
else end_date.replace(tzinfo=guild_timezone)
)
except ValueError:
# TODO Make a nice message
await ctx.respond(
@@ -224,7 +234,7 @@ class CogEvent(Cog):
)
return
if not await validate_event_validity(ctx, name, start_date, end_date, guild_timezone):
if not await validate_event_validity(ctx, name, start_date, end_date, to_utc=True):
return
processed_media: List[Dict[str, Any]] = (

View File

@@ -14,20 +14,28 @@ async def validate_event_validity(
ctx: ApplicationContext,
name: str,
start_date: datetime | None,
finish_date: datetime | None,
guild_timezone: ZoneInfo,
end_date: datetime | None,
event_id: ObjectId | None = None,
to_utc: bool = False,
) -> bool:
if start_date >= finish_date:
# TODO Make a nice message
await ctx.respond("Start date must be before finish date")
return False
start_date_internal: datetime = start_date.astimezone(ZoneInfo("UTC")) if to_utc else start_date
end_date_internal: datetime = end_date.astimezone(ZoneInfo("UTC")) if to_utc else end_date
if start_date < datetime.now(tz=guild_timezone):
if start_date_internal < datetime.now(tz=ZoneInfo("UTC")):
# TODO Make a nice message
await ctx.respond("Start date must not be in the past")
return False
if end_date_internal < datetime.now(tz=ZoneInfo("UTC")):
# TODO Make a nice message
await ctx.respond("End date must not be in the past")
return False
if start_date_internal >= end_date_internal:
# TODO Make a nice message
await ctx.respond("Start date must be before end date")
return False
# TODO Add validation for concurrent events.
# Only one event can take place at the same time.
query: Dict[str, Any] = {