128 lines
4.7 KiB
Python
128 lines
4.7 KiB
Python
from logging import Logger
|
|
from typing import Any, override
|
|
|
|
from bson import ObjectId
|
|
from discord import Guild, User
|
|
from libbot.cache.classes import CacheMemcached, CacheRedis
|
|
from libbot.cache.manager import create_cache_client
|
|
from libbot.pycord.classes import PycordBot as LibPycordBot
|
|
|
|
from classes import PycordEvent, PycordGuild, PycordUser, PycordEventStage
|
|
from modules.logging_utils import get_logger
|
|
|
|
logger: Logger = get_logger(__name__)
|
|
|
|
|
|
class PycordBot(LibPycordBot):
|
|
cache: CacheMemcached | CacheRedis | None = None
|
|
|
|
def __init__(self, *args, **kwargs) -> None:
|
|
super().__init__(*args, **kwargs)
|
|
|
|
self._set_cache_engine()
|
|
|
|
if self.scheduler is None:
|
|
return
|
|
|
|
# This replacement exists because of the different
|
|
# i18n formats than provided by libbot
|
|
self._ = self._modified_string_getter
|
|
|
|
def _set_cache_engine(self) -> None:
|
|
if "cache" in self.config and self.config["cache"]["type"] is not None:
|
|
self.cache = create_cache_client(self.config, self.config["cache"]["type"])
|
|
|
|
def _modified_string_getter(self, key: str, *args: str, locale: str | None = None) -> Any:
|
|
"""This method exists because of the different i18n formats than provided by libbot.
|
|
It splits "-" and takes the first part of the provided locale to make complex language codes
|
|
compatible with an easy libbot approach to i18n.
|
|
"""
|
|
return self.bot_locale._(key, *args, locale=None if locale is None else locale.split("-")[0])
|
|
|
|
def _set_cache_engine(self) -> None:
|
|
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.
|
|
|
|
Args:
|
|
user (int | User): ID or User object to extract ID from
|
|
|
|
Returns:
|
|
PycordUser: User object
|
|
|
|
Raises:
|
|
UserNotFoundException: User was not found and creation was not allowed
|
|
"""
|
|
return (
|
|
await PycordUser.from_id(user, cache=self.cache)
|
|
if isinstance(user, int)
|
|
else await PycordUser.from_id(user.id, cache=self.cache)
|
|
)
|
|
|
|
async def find_guild(self, guild: int | Guild) -> PycordGuild:
|
|
"""Find Guild by its ID or Guild object.
|
|
|
|
Args:
|
|
guild (int | Guild): ID or User object to extract ID from
|
|
|
|
Returns:
|
|
PycordGuild: Guild object
|
|
|
|
Raises:
|
|
GuildNotFoundException: Guild was not found and creation was not allowed
|
|
"""
|
|
return (
|
|
await PycordGuild.from_id(guild, cache=self.cache)
|
|
if isinstance(guild, int)
|
|
else await PycordGuild.from_id(guild.id, cache=self.cache)
|
|
)
|
|
|
|
# TODO Document this method
|
|
async def create_event(self, **kwargs) -> PycordEvent:
|
|
return await PycordEvent.create(**kwargs, cache=self.cache)
|
|
|
|
# TODO Document this method
|
|
async def create_event_stage(self, event: PycordEvent, **kwargs) -> PycordEventStage:
|
|
# TODO Validation is handled by the caller for now, but
|
|
# ideally this should not be the case at all.
|
|
#
|
|
# if "event_id" not in kwargs:
|
|
# # TODO Create a nicer exception
|
|
# raise RuntimeError("Event ID must be provided while creating an event stage")
|
|
#
|
|
# event: PycordEvent = await self.find_event(event_id=kwargs["event_id"])
|
|
|
|
if "sequence" not in kwargs:
|
|
# TODO Create a nicer exception
|
|
raise RuntimeError("Stage must have a defined sequence")
|
|
|
|
event_stage: PycordEventStage = await PycordEventStage.create(**kwargs, cache=self.cache)
|
|
|
|
await event.insert_stage(self, event_stage._id, kwargs["sequence"], cache=self.cache)
|
|
|
|
return event_stage
|
|
|
|
# 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!")
|
|
|
|
if event_id is not None:
|
|
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)
|