Implemented /stage edit and /stage delete (#2)

This commit is contained in:
2025-04-22 22:36:43 +02:00
parent f2c81648fa
commit ec094f9a98
7 changed files with 183 additions and 37 deletions

View File

@@ -5,10 +5,11 @@ from typing import Any, Dict, List, Optional
from zoneinfo import ZoneInfo
from bson import ObjectId
from discord import Bot
from libbot.cache.classes import Cache
from pymongo.results import InsertOneResult
from modules.database import col_events, col_stages
from modules.database import col_events
from modules.logging_utils import get_logger
logger: Logger = get_logger(__name__)
@@ -266,14 +267,62 @@ class PycordEvent:
await self.__collection__.delete_one({"_id": self._id})
self._delete_cache(cache)
# TODO Add documentation
async def cancel(self, cache: Optional[Cache] = None):
await self._set(cache, cancelled=True)
async def insert_stage(
self, event_stage_id: ObjectId, sequence: int, cache: Optional[Cache] = None
async def _update_event_stage_order(
self,
bot: Any,
old_stage_ids: List[ObjectId],
cache: Optional[Cache] = None,
) -> None:
self.stage_ids.insert(sequence, event_stage_id)
await self._set(cache, stage_ids=self.stage_ids)
logger.info("Updating event stages order for %s...", self._id)
# TODO Check if this works
await col_stages.update_many({"_id": {"$eq": self.stage_ids[sequence:]}}, {"$inc": {"sequence": 1}})
logger.debug("Old stage IDs: %s", old_stage_ids)
logger.debug("New stage IDs: %s", self.stage_ids)
for event_stage_id in self.stage_ids:
if event_stage_id not in old_stage_ids:
continue
stage_index: int = self.stage_ids.index(event_stage_id)
old_stage_index: int = old_stage_ids.index(event_stage_id)
logger.debug(
"Indexes for %s: was %s and is now %s", event_stage_id, old_stage_index, stage_index
)
if stage_index != old_stage_index:
await (await bot.find_event_stage(event_stage_id)).update(cache, sequence=stage_index)
# TODO Add documentation
async def insert_stage(
self, bot: Bot, event_stage_id: ObjectId, index: int, cache: Optional[Cache] = None
) -> None:
old_stage_ids: List[ObjectId] = self.stage_ids.copy()
self.stage_ids.insert(index, event_stage_id)
await self._set(cache, stage_ids=self.stage_ids)
await self._update_event_stage_order(bot, old_stage_ids, cache=cache)
# TODO Add documentation
async def reorder_stage(
self, bot: Any, event_stage_id: ObjectId, index: int, cache: Optional[Cache] = None
) -> None:
old_stage_ids: List[ObjectId] = self.stage_ids.copy()
self.stage_ids.insert(index, self.stage_ids.pop(self.stage_ids.index(event_stage_id)))
await self._set(cache, stage_ids=self.stage_ids)
await self._update_event_stage_order(bot, old_stage_ids, cache=cache)
# TODO Add documentation
async def remove_stage(self, bot: Bot, event_stage_id: ObjectId, cache: Optional[Cache] = None) -> None:
old_stage_ids: List[ObjectId] = self.stage_ids.copy()
self.stage_ids.pop(self.stage_ids.index(event_stage_id))
await self._set(cache, stage_ids=self.stage_ids)
await self._update_event_stage_order(bot, old_stage_ids, cache=cache)