Changed find_user behavior
This commit is contained in:
parent
8dad53409b
commit
4cbb51ca61
@ -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)
|
||||
|
@ -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)
|
||||
|
Reference in New Issue
Block a user