Implemented find_group and find_user

This commit is contained in:
2023-08-17 16:37:42 +02:00
parent ab39c111eb
commit 9e00d38877
15 changed files with 37 additions and 43 deletions

View File

@@ -1,30 +1,37 @@
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.pyrogroup import PyroGroup
from classes.pyrouser import PyroUser
from modules.database import col_users
class PyroClient(PyroClient):
async def find_user(self, user: Union[int, User], group: int) -> PyroUser:
"""Find User by it's ID or User object
class PyroClient(LibPyroClient):
async def find_user(self, user: Union[int, User], *args, **kwargs) -> PyroUser:
"""Find User by it's ID or User object.
### Args:
* user (`Union[int, User]`): ID or User object to extract ID from
* group (`int`): ID of the group
### Returns:
* `PyroUser`: PyroUser object
"""
db_record = await col_users.find_one(
{"id": user.id if isinstance(user, User) else user, "group": group}
return (
await PyroUser.find(user, *args, **kwargs)
if isinstance(user, int)
else await PyroUser.find(user.id, *args, **kwargs)
)
if db_record is None:
raise KeyError(
f"User with ID {user.id if isinstance(user, User) else user} was not found in the database"
)
async def find_group(self, id: int, *args, **kwargs) -> PyroGroup:
"""Find Group by it's ID.
return PyroUser(**db_record)
### Args:
* id (`int`): Group ID
### Returns:
* `PyroGroup`: PyroGroup object
"""
return await PyroGroup.find(id, *args, **kwargs)

View File

@@ -35,7 +35,7 @@ class PyroGroup:
timeout_verify: int
@classmethod
async def create_if_not_exists(
async def find(
cls,
id: int,
locale: Union[str, None] = sync.config_get("locale", "defaults", "group"),

View File

@@ -34,7 +34,7 @@ class PyroUser:
mistakes: int
@classmethod
async def create_if_not_exists(
async def find(
cls,
id: int,
group: int,