From 4cbb51ca6170e5ce8ca2ccaad21ecd24db30782b Mon Sep 17 00:00:00 2001 From: profitroll Date: Thu, 17 Aug 2023 15:21:08 +0200 Subject: [PATCH] Changed find_user behavior --- classes/pyroclient.py | 33 ++++++--------------------------- plugins/language.py | 9 ++------- 2 files changed, 8 insertions(+), 34 deletions(-) diff --git a/classes/pyroclient.py b/classes/pyroclient.py index e1ffa47..01888db 100644 --- a/classes/pyroclient.py +++ b/classes/pyroclient.py @@ -1,45 +1,24 @@ from typing import Union -from libbot.pyrogram.classes import PyroClient +from libbot.pyrogram.classes import PyroClient as LibPyroClient from pyrogram.types import User from classes.pyrouser import PyroUser -from modules.database import col_users -class PyroClient(PyroClient): +class PyroClient(LibPyroClient): async def find_user(self, user: Union[int, User]) -> PyroUser: """Find User by it's ID or User object. - ### ⚠️ WARNING - Method is deprecated, `PyroUser.find()` is a preferred way to find users - ### Args: * user (`Union[int, User]`): ID or User object to extract ID from ### Returns: * `PyroUser`: PyroUser object """ - if ( - await col_users.find_one( - {"id": user.id if isinstance(user, User) else user} - ) - is None - ): - await col_users.insert_one( - { - "id": user.id if isinstance(user, User) else user, - "locale": user.language_code if isinstance(user, User) else None, - } - ) - db_record = await col_users.find_one( - {"id": user.id if isinstance(user, User) else user} + return ( + await PyroUser.find(user) + if isinstance(user, int) + else await PyroUser.find(user.id, locale=user.language_code) ) - - if db_record is None: - raise TypeError( - f"User with ID {user.id if isinstance(user, User) else user} was not found in the database" - ) - - return PyroUser(**db_record) diff --git a/plugins/language.py b/plugins/language.py index 3c02d02..72c59b5 100644 --- a/plugins/language.py +++ b/plugins/language.py @@ -4,16 +4,13 @@ from pyrogram.types import CallbackQuery, Message from classes.callbacks import CallbackLanguage from classes.pyroclient import PyroClient -from classes.pyrouser import PyroUser @PyroClient.on_message( ~filters.scheduled & filters.private & filters.command(["language"], prefixes=["/"]) # type: ignore ) async def command_language(app: PyroClient, message: Message): - user = await PyroUser.find( - message.from_user.id, locale=message.from_user.language_code - ) + user = await app.find_user(message.from_user) keyboard = InlineKeyboard(row_width=2) buttons = [] @@ -33,9 +30,7 @@ async def command_language(app: PyroClient, message: Message): @PyroClient.on_callback_query(filters.regex(r"language:[\s\S]*")) # type: ignore async def callback_language(app: PyroClient, callback: CallbackQuery): - user = await PyroUser.find( - callback.from_user.id, locale=callback.from_user.language_code - ) + user = await app.find_user(callback.from_user) parsed = CallbackLanguage.from_callback(callback) await user.update_locale(parsed.language)