HoloUser WIP
This commit is contained in:
parent
94bb52ad62
commit
9c611b436c
@ -1,6 +1,9 @@
|
|||||||
from typing import Union
|
from app import isAnAdmin
|
||||||
from pyrogram.types import User, ChatMember
|
from typing import List, Union
|
||||||
|
from pyrogram.types import User, ChatMember, ChatPrivileges, Chat
|
||||||
|
from pyrogram.client import Client
|
||||||
from modules.database import col_users, col_context, col_warnings, col_applications, col_sponsorships
|
from modules.database import col_users, col_context, col_warnings, col_applications, col_sponsorships
|
||||||
|
from modules.utils import configGet
|
||||||
|
|
||||||
class UserNotFoundError(Exception):
|
class UserNotFoundError(Exception):
|
||||||
"""HoloUser could not find user with such an ID in database"""
|
"""HoloUser could not find user with such an ID in database"""
|
||||||
@ -9,6 +12,59 @@ class UserNotFoundError(Exception):
|
|||||||
self.user_id = user_id
|
self.user_id = user_id
|
||||||
super().__init__(f"User of type {type(self.user)} with id {self.user_id} was not found")
|
super().__init__(f"User of type {type(self.user)} with id {self.user_id} was not found")
|
||||||
|
|
||||||
|
class UserInvalidError(Exception):
|
||||||
|
"""Provided to HoloUser object is not supported"""
|
||||||
|
def __init__(self, user):
|
||||||
|
self.user = user
|
||||||
|
super().__init__(f"Could not find HoloUser by using {type(self.user)} as an input type")
|
||||||
|
|
||||||
class HoloUser():
|
class HoloUser():
|
||||||
def __init__(self, user: Union[User, ChatMember, int]) -> None:
|
|
||||||
pass
|
def __init__(self, user: Union[List[User], User, ChatMember, int]) -> None:
|
||||||
|
|
||||||
|
# Determine input object class and extract id
|
||||||
|
if isinstance(user, list) and len(user) != 0:
|
||||||
|
self.id = user[0].id
|
||||||
|
elif isinstance(user, User):
|
||||||
|
self.id = user.id
|
||||||
|
elif isinstance(user, ChatMember):
|
||||||
|
self.id = user.user.id
|
||||||
|
elif isinstance(user, int):
|
||||||
|
self.id = user
|
||||||
|
else:
|
||||||
|
raise UserInvalidError(user)
|
||||||
|
|
||||||
|
# Find user record in DB
|
||||||
|
holo_user = col_users.find_one({"user": self.id})
|
||||||
|
|
||||||
|
if holo_user is None:
|
||||||
|
raise UserNotFoundError(user=user, user_id=self.id)
|
||||||
|
|
||||||
|
self.db_id = holo_user["_id"]
|
||||||
|
|
||||||
|
self.label = holo_user["label"]
|
||||||
|
|
||||||
|
async def set_label(self, app: Client, chat: Chat, label: str):
|
||||||
|
"""Set label in destination group
|
||||||
|
|
||||||
|
### Args:
|
||||||
|
* app (`Client`): Pyrogram client
|
||||||
|
* label (`str`): Label you want to set
|
||||||
|
"""
|
||||||
|
self.label = label
|
||||||
|
await app.promote_chat_member(configGet("destination_group"), self.id)
|
||||||
|
if (not await isAnAdmin(self.id)) and (chat.id == configGet("admin_group")):
|
||||||
|
await app.set_administrator_title(configGet("destination_group"), self.id, label)
|
||||||
|
|
||||||
|
async def reset_label(self, app: Client, chat: Chat):
|
||||||
|
"""Reset label in destination group
|
||||||
|
|
||||||
|
### Args:
|
||||||
|
* app (`Client`): Pyrogram client
|
||||||
|
"""
|
||||||
|
self.label = ""
|
||||||
|
await app.set_administrator_title(configGet("destination_group"), self.id, "")
|
||||||
|
if (not await isAnAdmin(self.id)) and (chat.id == configGet("admin_group")):
|
||||||
|
await app.promote_chat_member(configGet("destination_group"), self.id, privileges=ChatPrivileges(
|
||||||
|
can_manage_chat=False
|
||||||
|
))
|
Reference in New Issue
Block a user