This repository has been archived on 2024-10-15. You can view files and clone it, but cannot push or open issues or pull requests.
PyrogramBotBase/classes/pyroclient.py

46 lines
1.3 KiB
Python
Raw Normal View History

2023-08-03 22:29:47 +03:00
from typing import Union
from libbot.pyrogram.classes import PyroClient
from pyrogram.types import User
from classes.pyrouser import PyroUser
from modules.database import col_users
class PyroClient(PyroClient):
async def find_user(self, user: Union[int, User]) -> PyroUser:
2023-08-17 16:15:53 +03:00
"""Find User by it's ID or User object.
### ⚠️ WARNING
Method is deprecated, `PyroUser.find()` is a preferred way to find users
2023-08-03 22:29:47 +03:00
### Args:
* user (`Union[int, User]`): ID or User object to extract ID from
### Returns:
* `PyroUser`: PyroUser object
"""
if (
2023-08-14 16:16:30 +03:00
await col_users.find_one(
{"id": user.id if isinstance(user, User) else user}
)
2023-08-03 22:29:47 +03:00
is None
):
2023-08-14 16:16:30 +03:00
await col_users.insert_one(
2023-08-03 22:29:47 +03:00
{
"id": user.id if isinstance(user, User) else user,
"locale": user.language_code if isinstance(user, User) else None,
}
)
2023-08-14 16:16:30 +03:00
db_record = await col_users.find_one(
2023-08-03 22:29:47 +03:00
{"id": user.id if isinstance(user, User) else user}
)
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)