Implemented /stage edit and /stage delete (#2)
This commit is contained in:
@@ -1,5 +1,5 @@
|
||||
from logging import Logger
|
||||
from typing import Any
|
||||
from typing import Any, override
|
||||
|
||||
from bson import ObjectId
|
||||
from discord import Guild, User
|
||||
@@ -43,6 +43,14 @@ class PycordBot(LibPycordBot):
|
||||
if "cache" in self.config and self.config["cache"]["type"] is not None:
|
||||
self.cache = create_cache_client(self.config, self.config["cache"]["type"])
|
||||
|
||||
@override
|
||||
async def start(self, *args: Any, **kwargs: Any) -> None:
|
||||
await super().start(*args, **kwargs)
|
||||
|
||||
@override
|
||||
async def close(self, **kwargs) -> None:
|
||||
await super().close(**kwargs)
|
||||
|
||||
async def find_user(self, user: int | User) -> PycordUser:
|
||||
"""Find User by its ID or User object.
|
||||
|
||||
@@ -100,16 +108,11 @@ class PycordBot(LibPycordBot):
|
||||
|
||||
event_stage: PycordEventStage = await PycordEventStage.create(**kwargs, cache=self.cache)
|
||||
|
||||
await event.insert_stage(event_stage._id, kwargs["sequence"], cache=self.cache)
|
||||
await event.insert_stage(self, event_stage._id, kwargs["sequence"], cache=self.cache)
|
||||
|
||||
return event_stage
|
||||
|
||||
async def start(self, *args: Any, **kwargs: Any) -> None:
|
||||
await super().start(*args, **kwargs)
|
||||
|
||||
async def close(self, **kwargs) -> None:
|
||||
await super().close(**kwargs)
|
||||
|
||||
# TODO Document this method
|
||||
async def find_event(self, event_id: str | ObjectId | None = None, event_name: str | None = None):
|
||||
if event_id is None and event_name is None:
|
||||
raise AttributeError("Either event's ID or name must be provided!")
|
||||
@@ -118,3 +121,7 @@ class PycordBot(LibPycordBot):
|
||||
return await PycordEvent.from_id(event_id, cache=self.cache)
|
||||
else:
|
||||
return await PycordEvent.from_name(event_name, cache=self.cache)
|
||||
|
||||
# TODO Document this method
|
||||
async def find_event_stage(self, stage_id: str | ObjectId) -> PycordEventStage:
|
||||
return await PycordEventStage.from_id(stage_id, cache=self.cache)
|
||||
|
@@ -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)
|
||||
|
@@ -38,7 +38,7 @@ class PycordEventStage:
|
||||
creator_id: int
|
||||
question: str
|
||||
answer: str
|
||||
media: List[str]
|
||||
media: List[int]
|
||||
|
||||
@classmethod
|
||||
async def from_id(cls, stage_id: str | ObjectId, cache: Optional[Cache] = None) -> "PycordEventStage":
|
||||
|
Reference in New Issue
Block a user