Cleanups and bugfixes for (#2 and #8)

This commit is contained in:
2025-04-24 00:16:53 +02:00
parent 57c4ff3bf9
commit c1d8620478
11 changed files with 222 additions and 72 deletions

View File

@@ -1,8 +1,7 @@
from datetime import datetime
from typing import Any, Dict
from zoneinfo import ZoneInfo
from bson import ObjectId
from bson.errors import InvalidId
from discord import (
ApplicationContext,
Attachment,
@@ -14,42 +13,7 @@ from discord.utils import basic_autocomplete
from classes import PycordEvent, PycordGuild
from classes.pycord_bot import PycordBot
from modules.database import col_events
from modules.utils import autocomplete_active_events
# TODO Move to staticmethod or to a separate module
async def validate_event_validity(
ctx: ApplicationContext,
name: str,
start_date: datetime | None,
finish_date: datetime | None,
guild_timezone: ZoneInfo,
event_id: ObjectId | None = None,
) -> None:
if start_date > finish_date:
# TODO Make a nice message
await ctx.respond("Start date must be before finish date")
return
elif start_date < datetime.now(tz=guild_timezone):
# TODO Make a nice message
await ctx.respond("Start date must not be in the past")
return
query: Dict[str, Any] = {
"name": name,
"ended": None,
"ends": {"$gt": datetime.now(tz=ZoneInfo("UTC"))},
"cancelled": {"$ne": True},
}
if event_id is not None:
query["_id"] = {"$ne": event_id}
if (await col_events.find_one(query)) is not None:
# TODO Make a nice message
await ctx.respond("There can only be one active event with the same name")
return
from modules.utils import autocomplete_active_events, validate_event_validity
class Event(Cog):
@@ -137,9 +101,10 @@ class Event(Cog):
thumbnail: Attachment = None,
) -> None:
guild: PycordGuild = await self.bot.find_guild(ctx.guild.id)
pycord_event: PycordEvent = await self.bot.find_event(event_id=event)
if pycord_event is None:
try:
pycord_event: PycordEvent = await self.bot.find_event(event_id=event)
except (InvalidId, RuntimeError):
# TODO Make a nice message
await ctx.respond("Event was not found.")
return
@@ -208,9 +173,10 @@ class Event(Cog):
return
guild: PycordGuild = await self.bot.find_guild(ctx.guild.id)
pycord_event: PycordEvent = await self.bot.find_event(event_id=event)
if pycord_event is None:
try:
pycord_event: PycordEvent = await self.bot.find_event(event_id=event)
except (InvalidId, RuntimeError):
# TODO Make a nice message
await ctx.respond("Event was not found.")
return
@@ -251,9 +217,10 @@ class Event(Cog):
)
async def command_event_show(self, ctx: ApplicationContext, event: str) -> None:
guild: PycordGuild = await self.bot.find_guild(ctx.guild.id)
pycord_event: PycordEvent = await self.bot.find_event(event_id=event)
if pycord_event is None:
try:
pycord_event: PycordEvent = await self.bot.find_event(event_id=event)
except (InvalidId, RuntimeError):
# TODO Make a nice message
await ctx.respond("Event was not found.")
return

View File

@@ -1,6 +1,7 @@
from datetime import datetime
from zoneinfo import ZoneInfo
from bson.errors import InvalidId
from discord import ApplicationContext, Attachment, SlashCommandGroup, option
from discord.ext.commands import Cog
from discord.utils import basic_autocomplete
@@ -14,7 +15,7 @@ async def validate_event_status(
ctx: ApplicationContext,
event: PycordEvent,
) -> None:
if event.cancelled:
if event.is_cancelled:
# TODO Make a nice message
await ctx.respond("This event was cancelled.")
return
@@ -63,7 +64,12 @@ class Stage(Cog):
await ctx.respond("Guild is not configured.")
return
pycord_event: PycordEvent = await self.bot.find_event(event)
try:
pycord_event: PycordEvent = await self.bot.find_event(event_id=event)
except (InvalidId, RuntimeError):
# TODO Make a nice message
await ctx.respond("Event was not found.")
return
await validate_event_status(ctx, pycord_event)
@@ -123,11 +129,21 @@ class Stage(Cog):
await ctx.respond("Guild is not configured.")
return
pycord_event: PycordEvent = await self.bot.find_event(event)
try:
pycord_event: PycordEvent = await self.bot.find_event(event_id=event)
except (InvalidId, RuntimeError):
# TODO Make a nice message
await ctx.respond("Event was not found.")
return
await validate_event_status(ctx, pycord_event)
event_stage: PycordEventStage = await self.bot.find_event_stage(stage)
try:
event_stage: PycordEventStage = await self.bot.find_event_stage(stage)
except (InvalidId, RuntimeError):
# TODO Make a nice message
await ctx.respond("Event stage was not found.")
return
if order is not None and order > len(pycord_event.stage_ids):
# TODO Make a nice message
@@ -180,11 +196,21 @@ class Stage(Cog):
await ctx.respond("Guild is not configured.")
return
pycord_event: PycordEvent = await self.bot.find_event(event)
try:
pycord_event: PycordEvent = await self.bot.find_event(event_id=event)
except (InvalidId, RuntimeError):
# TODO Make a nice message
await ctx.respond("Event was not found.")
return
await validate_event_status(ctx, pycord_event)
event_stage: PycordEventStage = await self.bot.find_event_stage(stage)
try:
event_stage: PycordEventStage = await self.bot.find_event_stage(stage)
except (InvalidId, RuntimeError):
# TODO Make a nice message
await ctx.respond("Event stage was not found.")
return
await pycord_event.remove_stage(self.bot, event_stage._id, cache=self.bot.cache)
await event_stage.purge(cache=self.bot.cache)