From c3d3e4321643f58f452c1bbf95f61be696355ecb Mon Sep 17 00:00:00 2001 From: profitroll Date: Sun, 19 May 2024 14:48:01 +0200 Subject: [PATCH] Added errors and fixed pycord's bot --- src/libbot/__init__.py | 4 ++-- src/libbot/errors/__init__.py | 1 + src/libbot/errors/config.py | 37 ++++++++++++++++++++++++++++++++ src/libbot/pycord/classes/bot.py | 11 ++++++---- 4 files changed, 47 insertions(+), 6 deletions(-) create mode 100644 src/libbot/errors/__init__.py create mode 100644 src/libbot/errors/config.py diff --git a/src/libbot/__init__.py b/src/libbot/__init__.py index 74b0290..ae01807 100644 --- a/src/libbot/__init__.py +++ b/src/libbot/__init__.py @@ -1,6 +1,6 @@ -__version__ = "3.0.1" +__version__ = "3.1.0" __license__ = "GPL3" __author__ = "Profitroll" -from . import i18n, pycord, pyrogram, sync +from . import errors, i18n, pycord, pyrogram, sync from .__main__ import * diff --git a/src/libbot/errors/__init__.py b/src/libbot/errors/__init__.py new file mode 100644 index 0000000..1b0549f --- /dev/null +++ b/src/libbot/errors/__init__.py @@ -0,0 +1 @@ +from .config import ConfigKeyError, ConfigValueError diff --git a/src/libbot/errors/config.py b/src/libbot/errors/config.py new file mode 100644 index 0000000..64f8a4c --- /dev/null +++ b/src/libbot/errors/config.py @@ -0,0 +1,37 @@ +from typing import Any, List, Optional, Union + + +class ConfigKeyError(Exception): + """Raised when config key is not found. + + ### Attributes: + * key (`Union[str, List[str]]`): Missing config key. + """ + + def __init__(self, key: Union[str, List[str]]) -> None: + self.key: Union[str, List[str]] = key + super().__init__( + f"Config key {'.'.join(key) if isinstance(key, list) else key} is missing. Please set in your config file." + ) + + def __str__(self): + return f"Config key {'.'.join(self.key) if isinstance(self.key, list) else self.key} is missing. Please set in your config file." + + +class ConfigValueError(Exception): + """Raised when config key's value is invalid. + + ### Attributes: + * key (`Union[str, List[str]]`): Invalid config key. + * value (`Optional[Any]`): Key's correct value. + """ + + def __init__(self, key: Union[str, List[str]], value: Optional[Any] = None) -> None: + self.key: Union[str, List[str]] = key + self.value: Optional[Any] = value + super().__init__( + f"Config key {'.'.join(key) if isinstance(key, list) else key} has invalid value. {f'Must be {value}. ' if value else ''}Please set in your config file." + ) + + def __str__(self): + return f"Config key {'.'.join(self.key) if isinstance(self.key, list) else self.key} has invalid value. {f'Must be {self.value}. ' if self.value else ''}Please set in your config file." diff --git a/src/libbot/pycord/classes/bot.py b/src/libbot/pycord/classes/bot.py index e59dad5..d521532 100644 --- a/src/libbot/pycord/classes/bot.py +++ b/src/libbot/pycord/classes/bot.py @@ -1,6 +1,6 @@ import logging from pathlib import Path -from typing import Any, Dict, Union +from typing import Any, Dict, List, Union try: from apscheduler.schedulers.asyncio import AsyncIOScheduler @@ -29,6 +29,7 @@ class PycordBot(Bot): config_path: Union[str, Path] = Path("config.json"), locales_root: Union[str, Path, None] = None, scheduler: Union[AsyncIOScheduler, BackgroundScheduler, None] = None, + *args, **kwargs, ): if config is None: @@ -38,17 +39,19 @@ class PycordBot(Bot): self.config = config super().__init__( - debug_guilds=self.config["bot"]["debug_guilds"], - owner_ids=self.config["bot"]["owners"], + *args, **kwargs, ) + self.owner_ids: List[int] = self.config["bot"]["owners"] + self.debug_guilds: List[int] = self.config["bot"]["debug_guilds"] + self.bot_locale: BotLocale = BotLocale( default_locale=self.config["locale"], locales_root=(Path("locale") if locales_root is None else locales_root), ) self.default_locale: str = self.bot_locale.default - self.locales: dict = self.bot_locale.locales + self.locales: Dict[str, Any] = self.bot_locale.locales self._ = self.bot_locale._ self.in_all_locales = self.bot_locale.in_all_locales