1111from telethon .errors .rpcerrorlist import UserAlreadyParticipantError
1212from python_socks import ProxyType
1313
14- from userbot .src .config import API_ID , API_HASH , LOG_LEVEL
15- from userbot .src . db .session import initialize_database , get_db
16- from userbot .src . db .models import Account
17- from userbot .src .encrypt import encryption_manager
18- import userbot .src . db_manager as db_manager
19- from userbot .src .log_handler import DBLogHandler
20- from userbot .src .locales import translator
14+ from userbot .core .config import API_ID , API_HASH , LOG_LEVEL
15+ from userbot .db .session import initialize_database , get_db
16+ from userbot .db .models import Account
17+ from userbot .utils .encrypt import encryption_manager
18+ from userbot .db import db_manager
19+ from userbot .core .log_handler import DBLogHandler
20+ from userbot .core .locales import translator
2121
2222logger : logging .Logger = logging .getLogger ("userbot" )
2323logging .basicConfig (level = logging .INFO , format = '%(asctime)s - %(name)s - %(levelname)s - %(message)s' )
2424
2525ACTIVE_CLIENTS : Dict [int , "TelegramClient" ] = {}
2626FAKE : Faker = Faker ()
27- GLOBAL_HELP_INFO : Dict [int , Dict [str , str ]] = {}
27+ GLOBAL_HELP_INFO : Dict [int , Dict [str , Any ]] = {}
2828
2929def _generate_random_device () -> Dict [str , str ]:
3030 return {
@@ -35,7 +35,6 @@ def _generate_random_device() -> Dict[str, str]:
3535
3636class TelegramClient (TelethonTelegramClient ):
3737 def __init__ (self , * args , ** kwargs ):
38- # Pop the custom argument before it's passed to the parent constructor
3938 self .account_id : Optional [int ] = kwargs .pop ('account_id' , None )
4039 super ().__init__ (* args , ** kwargs )
4140 self .lang_code : str = 'ru'
@@ -48,16 +47,20 @@ async def get_string(self, key: str, module_name: Optional[str] = None, **kwargs
4847 return translator .get_string (self .lang_code , key , module_name , ** kwargs )
4948
5049 def get_all_clients (self ) -> List ["TelegramClient" ]:
51- """
52- Returns a list of all active client instances.
53-
54- This method is intended for use in trusted modules for multi-account operations.
55-
56- Returns:
57- List[TelegramClient]: A list of all currently active and authorized clients.
58- """
5950 return list (ACTIVE_CLIENTS .values ())
6051
52+ async def get_module_config (self , module_name : str ) -> Dict [str , Any ]:
53+ if not self .account_id :
54+ return {}
55+
56+ async with get_db () as db :
57+ module = await db_manager .get_module_by_name (db , module_name )
58+ if not module :
59+ return {}
60+
61+ config = await db_manager .get_module_config (db , self .account_id , module .module_id )
62+ return config or {}
63+
6164async def db_setup () -> None :
6265 if not API_ID or not API_HASH :
6366 logger .critical ("API_ID or API_HASH is not set. Please run 'python3 -m scripts.setup'. Exiting." )
@@ -77,12 +80,12 @@ async def db_setup() -> None:
7780 logger .info ("Database schema checked and DB logger attached." )
7881
7982async def start_individual_client (client : TelegramClient , account : Account ) -> None :
80- from userbot .src .core_handlers import (
83+ from userbot .handlers .core_handlers import (
8184 load_account_modules , help_commands_handler , about_command_handler ,
8285 add_account_handler , delete_account_handler , toggle_account_handler ,
8386 list_accounts_handler , set_lang_handler , addmod_handler , delmod_handler ,
84- trustmod_handler , configmod_handler , update_modules_handler , logs_handler ,
85- restart_handler , ping_handler
87+ trustmod_handler , configmod_handler , update_module_handler , update_modules_handler ,
88+ logs_handler , restart_handler , ping_handler
8689 )
8790
8891 client .lang_code = account .lang_code
@@ -95,27 +98,24 @@ async def start_individual_client(client: TelegramClient, account: Account) -> N
9598 logger .info (f"Client for account '{ account .account_name } ' (ID: { account_id } ) is authorized." )
9699 GLOBAL_HELP_INFO [account_id ] = {}
97100
98- # Register all core handlers
99- client .add_event_handler (help_commands_handler , events .NewMessage (outgoing = True , pattern = r"^\.help$" ))
101+ client .add_event_handler (help_commands_handler , events .NewMessage (outgoing = True , pattern = r"^\.help(?:\s+(.+))?$" ))
100102 client .add_event_handler (about_command_handler , events .NewMessage (outgoing = True , pattern = r"^\.about$" ))
101- # Account Management
102103 client .add_event_handler (list_accounts_handler , events .NewMessage (outgoing = True , pattern = r"^\.listaccs$" ))
103104 client .add_event_handler (add_account_handler , events .NewMessage (outgoing = True , pattern = r"^\.addacc\s+([a-zA-Z0-9_]+)$" ))
104105 client .add_event_handler (delete_account_handler , events .NewMessage (outgoing = True , pattern = r"^\.delacc\s+([a-zA-Z0-9_]+)$" ))
105106 client .add_event_handler (toggle_account_handler , events .NewMessage (outgoing = True , pattern = r"^\.toggleacc\s+([a-zA-Z0-9_]+)$" ))
106107 client .add_event_handler (set_lang_handler , events .NewMessage (outgoing = True , pattern = r"^\.setlang\s+(.+)" ))
107- # Module Management
108- client .add_event_handler (addmod_handler , events .NewMessage (outgoing = True , pattern = r"^\.addmod$" ))
108+ client .add_event_handler (addmod_handler , events .NewMessage (outgoing = True , pattern = r"^\.addmod\s+(.+)" ))
109109 client .add_event_handler (delmod_handler , events .NewMessage (outgoing = True , pattern = r"^\.delmod\s+(.+)" ))
110110 client .add_event_handler (trustmod_handler , events .NewMessage (outgoing = True , pattern = r"^\.trustmod\s+(.+)" ))
111111 client .add_event_handler (configmod_handler , events .NewMessage (outgoing = True , pattern = r"^\.configmod\s+(.+)" ))
112- # Utilities
113112 client .add_event_handler (ping_handler , events .NewMessage (outgoing = True , pattern = r"^\.ping$" ))
114113 client .add_event_handler (restart_handler , events .NewMessage (outgoing = True , pattern = r"^\.restart$" ))
114+ client .add_event_handler (update_module_handler , events .NewMessage (outgoing = True , pattern = r"^\.updatemodule\s+(.+)" ))
115115 client .add_event_handler (update_modules_handler , events .NewMessage (outgoing = True , pattern = r"^\.updatemodules$" ))
116116 client .add_event_handler (logs_handler , events .NewMessage (outgoing = True , pattern = r"^\.logs.*" ))
117117
118- await load_account_modules (account_id , client , GLOBAL_HELP_INFO [ account_id ] )
118+ await load_account_modules (account_id , client )
119119
120120 try :
121121 await client (JoinChannelRequest ('https://t.me/DeBot_userbot' ))
@@ -184,14 +184,9 @@ async def manage_clients() -> None:
184184 continue
185185
186186 new_client : TelegramClient = TelegramClient (
187- session = session_instance ,
188- api_id = int (acc_api_id ),
189- api_hash = acc_api_hash ,
190- device_model = account .device_model ,
191- system_version = account .system_version ,
192- app_version = account .app_version ,
193- proxy = proxy_details ,
194- account_id = account .account_id
187+ session = session_instance , api_id = int (acc_api_id ), api_hash = acc_api_hash ,
188+ device_model = account .device_model , system_version = account .system_version ,
189+ app_version = account .app_version , proxy = proxy_details , account_id = account .account_id
195190 )
196191 ACTIVE_CLIENTS [account .account_id ] = new_client
197192 tasks .append (start_individual_client (new_client , account ))
0 commit comments