109 lines
3.0 KiB
Python
109 lines
3.0 KiB
Python
from dataclasses import dataclass
|
|
from datetime import datetime
|
|
from typing import Any, Dict, Optional
|
|
|
|
from bson import ObjectId
|
|
from libbot.cache.classes import Cache
|
|
|
|
from javelina.classes.base import BaseCacheable
|
|
from javelina.modules.database import col_custom_channels
|
|
|
|
|
|
@dataclass
|
|
class CustomChannel(BaseCacheable):
|
|
"""Dataclass of DB entry of a custom channel"""
|
|
|
|
__slots__ = (
|
|
"_id",
|
|
"owner_id",
|
|
"guild_id",
|
|
"channel_id",
|
|
"allow_comments",
|
|
"allow_reactions",
|
|
"created",
|
|
"deleted",
|
|
)
|
|
__short_name__ = "channel"
|
|
__collection__ = col_custom_channels
|
|
|
|
_id: ObjectId
|
|
owner_id: int
|
|
guild_id: int
|
|
channel_id: int
|
|
allow_comments: bool
|
|
allow_reactions: bool
|
|
created: datetime
|
|
deleted: datetime | None
|
|
|
|
@classmethod
|
|
async def from_id(
|
|
cls,
|
|
user_id: int,
|
|
guild_id: int,
|
|
channel_id: Optional[int] = None,
|
|
cache: Optional[Cache] = None,
|
|
) -> "CustomChannel":
|
|
raise NotImplementedError()
|
|
|
|
def to_dict(self, json_compatible: bool = False) -> Dict[str, Any]:
|
|
"""Convert PycordGuild object to a JSON representation.
|
|
|
|
Args:
|
|
json_compatible (bool): Whether the JSON-incompatible objects like ObjectId need to be converted
|
|
|
|
Returns:
|
|
Dict[str, Any]: JSON representation of PycordGuild
|
|
"""
|
|
return {
|
|
"_id": self._id if not json_compatible else str(self._id),
|
|
"id": self.id,
|
|
"locale": self.locale,
|
|
}
|
|
|
|
async def _set(self, cache: Optional[Cache] = None, **kwargs: Any) -> None:
|
|
await super()._set(cache, **kwargs)
|
|
|
|
async def _remove(self, *args: str, cache: Optional[Cache] = None) -> None:
|
|
await super()._remove(*args, cache=cache)
|
|
|
|
def _get_cache_key(self) -> str:
|
|
return f"{self.__short_name__}_{self.id}"
|
|
|
|
def _update_cache(self, cache: Optional[Cache] = None) -> None:
|
|
super()._update_cache(cache)
|
|
|
|
def _delete_cache(self, cache: Optional[Cache] = None) -> None:
|
|
super()._delete_cache(cache)
|
|
|
|
@staticmethod
|
|
def _entry_to_cache(db_entry: Dict[str, Any]) -> Dict[str, Any]:
|
|
cache_entry: Dict[str, Any] = db_entry.copy()
|
|
|
|
cache_entry["_id"] = str(cache_entry["_id"])
|
|
|
|
return cache_entry
|
|
|
|
@staticmethod
|
|
def _entry_from_cache(cache_entry: Dict[str, Any]) -> Dict[str, Any]:
|
|
db_entry: Dict[str, Any] = cache_entry.copy()
|
|
|
|
db_entry["_id"] = ObjectId(db_entry["_id"])
|
|
|
|
return db_entry
|
|
|
|
# TODO Add documentation
|
|
@staticmethod
|
|
def get_defaults(guild_id: Optional[int] = None) -> Dict[str, Any]:
|
|
return {
|
|
"id": guild_id,
|
|
"locale": None,
|
|
}
|
|
|
|
# TODO Add documentation
|
|
@staticmethod
|
|
def get_default_value(key: str) -> Any:
|
|
if key not in CustomChannel.get_defaults():
|
|
raise KeyError(f"There's no default value for key '{key}' in CustomChannel")
|
|
|
|
return CustomChannel.get_defaults()[key]
|