diff --git a/classes/callbacks.py b/classes/callbacks.py index b05b3d3..af6fc52 100644 --- a/classes/callbacks.py +++ b/classes/callbacks.py @@ -9,7 +9,20 @@ class CallbackLanguage: @classmethod def from_callback(cls, callback: CallbackQuery): + """Parse callback query and extract language data from it. + + ### Args: + * callback (`CallbackQuery`): Callback query got from user interaction. + + ### Raises: + * `ValueError`: Raised when callback provided is not a language one. + + ### Returns: + * `CallbackLanguage`: Parsed callback query. + """ action, language = str(callback.data).split(":") + if action.lower() != "language": raise ValueError("Callback provided is not a language callback") + return cls(language) diff --git a/classes/pyroclient.py b/classes/pyroclient.py index 01888db..e7800ad 100644 --- a/classes/pyroclient.py +++ b/classes/pyroclient.py @@ -11,10 +11,10 @@ class PyroClient(LibPyroClient): """Find User by it's ID or User object. ### Args: - * user (`Union[int, User]`): ID or User object to extract ID from + * user (`Union[int, User]`): ID or User object to extract ID from. ### Returns: - * `PyroUser`: PyroUser object + * `PyroUser`: User in database representation. """ return ( diff --git a/classes/pyrouser.py b/classes/pyrouser.py index 527d72a..2fb9b73 100644 --- a/classes/pyrouser.py +++ b/classes/pyrouser.py @@ -21,6 +21,18 @@ class PyroUser: @classmethod async def find(cls, id: int, locale: Union[str, None] = None): + """Find user in database and create new record if user does not exist. + + ### Args: + * id (`int`): User's Telegram ID + * locale (`Union[str, None]`, *optional*): User's locale. Defaults to `None`. + + ### Raises: + * `RuntimeError`: Raised when user entry after insertion could not be found. + + ### Returns: + * `PyroUser`: User with its database data. + """ db_entry = await col_users.find_one({"id": id}) if db_entry is None: @@ -33,10 +45,10 @@ class PyroUser: return cls(**db_entry) async def update_locale(self, locale: Union[str, None]) -> None: - """Change user's locale stored in the database + """Change user's locale stored in the database. ### Args: - * locale (`Union[str, None]`): New locale to be set + * locale (`Union[str, None]`): New locale to be set. """ logger.debug("%s's locale has been set to %s", self.id, locale) await col_users.update_one({"_id": self._id}, {"$set": {"locale": locale}}) diff --git a/modules/database.py b/modules/database.py index 07f32e0..bdfacdb 100644 --- a/modules/database.py +++ b/modules/database.py @@ -1,9 +1,11 @@ """Module that provides all database collections""" -from async_pymongo import AsyncClient +from typing import Any, Mapping + +from async_pymongo import AsyncClient, AsyncCollection, AsyncDatabase from libbot.sync import config_get -db_config = config_get("database") +db_config: Mapping[str, Any] = config_get("database") if db_config["user"] is not None and db_config["password"] is not None: con_string = "mongodb://{0}:{1}@{2}:{3}/{4}".format( @@ -19,6 +21,6 @@ else: ) db_client = AsyncClient(con_string) -db = db_client.get_database(name=db_config["name"]) +db: AsyncDatabase = db_client.get_database(name=db_config["name"]) -col_users = db.get_collection("users") +col_users: AsyncCollection = db.get_collection("users") diff --git a/plugins/language.py b/plugins/language.py index 72c59b5..673b72b 100644 --- a/plugins/language.py +++ b/plugins/language.py @@ -1,3 +1,5 @@ +from typing import List + from pykeyboard import InlineButton, InlineKeyboard from pyrogram import filters from pyrogram.types import CallbackQuery, Message @@ -13,7 +15,7 @@ async def command_language(app: PyroClient, message: Message): user = await app.find_user(message.from_user) keyboard = InlineKeyboard(row_width=2) - buttons = [] + buttons: List[InlineButton] = [] for locale, data in app.in_every_locale("metadata").items(): buttons.append(