now using global app

This commit is contained in:
Profitroll 2022-12-11 23:31:36 +01:00
parent 5af5bea805
commit a8d17237e1

View File

@ -1,10 +1,11 @@
from app import isAnAdmin from app import app, isAnAdmin
from typing import Any, List, Union from typing import Any, List, Union
from pyrogram.types import User, ChatMember, ChatPrivileges, Chat from pyrogram.types import User, ChatMember, ChatPrivileges, Chat, Message
from pyrogram.client import Client from pyrogram.client import Client
from modules.database import col_users, col_context, col_warnings, col_applications, col_sponsorships from pyrogram.errors import bad_request_400
from modules.database import col_users, col_context, col_warnings, col_applications, col_sponsorships, col_messages
from modules.logging import logWrite from modules.logging import logWrite
from modules.utils import configGet from modules.utils import configGet, locale, should_quote
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"""
@ -21,7 +22,7 @@ class UserInvalidError(Exception):
class HoloUser(): class HoloUser():
def __init__(self, user: Union[List[User], User, ChatMember, int]) -> None: def __init__(self, user: Union[List[User], User, ChatMember, int, str]) -> None:
# Determine input object class and extract id # Determine input object class and extract id
if isinstance(user, list) and len(user) != 0: if isinstance(user, list) and len(user) != 0:
@ -32,6 +33,13 @@ class HoloUser():
self.id = user.user.id self.id = user.user.id
elif isinstance(user, int): elif isinstance(user, int):
self.id = user self.id = user
elif isinstance(user, str):
try:
self.id = (app.get_users(user)).id
except bad_request_400.UsernameNotOccupied:
raise UserInvalidError(user)
except bad_request_400.PeerIdInvalid:
raise UserInvalidError(user)
else: else:
raise UserInvalidError(user) raise UserInvalidError(user)
@ -50,31 +58,6 @@ class HoloUser():
self.locale = holo_user["tg_locale"] self.locale = holo_user["tg_locale"]
self.username = holo_user["tg_username"] self.username = holo_user["tg_username"]
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
))
def set(self, key: str, value: Any) -> None: def set(self, key: str, value: Any) -> None:
"""Set attribute data and save it into database """Set attribute data and save it into database
@ -87,3 +70,36 @@ class HoloUser():
setattr(self, key, value) setattr(self, key, value)
col_users.update_one(filter={"_id": self.db_id}, update={ "$set": { key: value } }, upsert=True) col_users.update_one(filter={"_id": self.db_id}, update={ "$set": { key: value } }, upsert=True)
logWrite(f"Set attribute {key} of user {self.id} to {value}") logWrite(f"Set attribute {key} of user {self.id} to {value}")
async def message(self, origin: Message, text: Union[str, None] = None, photo: Union[str, None] = None, video: Union[str, None] = None, file: Union[str, None] = None):
new_message = await app.send_message(self.id, text+locale("message_reply_notice", "message"))
await origin.reply_text(locale("message_sent", "message"), quote=should_quote(origin))
logWrite(f"Admin {origin.from_user.id} sent message '{' '.join(origin.command[2:])}' to {self.id}")
col_messages.insert_one({"origin": {"chat": origin.chat.id, "id": origin.id}, "destination": {"chat": new_message.chat.id, "id": new_message.id}})
async def set_label(self, chat: Chat, label: str):
"""Set label in destination group
### Args:
* app (`Client`): Pyrogram client
* label (`str`): Label you want to set
"""
self.label = label
self.set("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, chat: Chat):
"""Reset label in destination group
### Args:
* app (`Client`): Pyrogram client
"""
self.label = ""
self.set("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
))