Initial commit with code
This commit is contained in:
71
classes/pycord_bot.py
Normal file
71
classes/pycord_bot.py
Normal file
@@ -0,0 +1,71 @@
|
||||
import logging
|
||||
from logging import Logger
|
||||
from typing import Any
|
||||
|
||||
from aiohttp import ClientSession
|
||||
from discord import User
|
||||
from libbot.cache.manager import create_cache_client
|
||||
from libbot.pycord.classes import PycordBot as LibPycordBot
|
||||
|
||||
from classes import PycordUser
|
||||
|
||||
logger: Logger = logging.getLogger(__name__)
|
||||
|
||||
|
||||
# from modules.tracking.dhl import update_tracks_dhl
|
||||
|
||||
|
||||
class PycordBot(LibPycordBot):
|
||||
def __init__(self, *args, **kwargs) -> None:
|
||||
super().__init__(*args, **kwargs)
|
||||
|
||||
self._set_cache_engine()
|
||||
|
||||
self.client_session = ClientSession()
|
||||
|
||||
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 _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"])
|
||||
|
||||
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 start(self, *args: Any, **kwargs: Any) -> None:
|
||||
await super().start(*args, **kwargs)
|
||||
|
||||
async def close(self, **kwargs) -> None:
|
||||
await self.client_session.close()
|
||||
|
||||
if self.scheduler is not None:
|
||||
self.scheduler.shutdown()
|
||||
|
||||
await super().close(**kwargs)
|
Reference in New Issue
Block a user