Added errors and fixed pycord's bot

This commit is contained in:
Profitroll 2024-05-19 14:48:01 +02:00
parent 1f464ef624
commit c3d3e43216
Signed by: profitroll
GPG Key ID: FA35CAB49DACD3B2
4 changed files with 47 additions and 6 deletions

View File

@ -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 *

View File

@ -0,0 +1 @@
from .config import ConfigKeyError, ConfigValueError

View File

@ -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."

View File

@ -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