22import gc
33import logging
44import sys
5- from typing import Dict , Any , List
5+ from typing import Dict , Any
66
77from apscheduler .schedulers .asyncio import AsyncIOScheduler
88from rich .console import Console
9- from telethon import events
10- from telethon .errors import SessionPasswordNeededError
11- from telethon .sessions import StringSession
129from art import text2art
1310
1411from userbot import (
15- ACTIVE_CLIENTS , TelegramClient ,
16- db_setup , manage_clients , GLOBAL_HELP_INFO , _generate_random_device
12+ db_setup , manage_clients
1713)
18- from userbot .src .config import GC_INTERVAL_SECONDS , LOG_QUEUE_INTERVAL_SECONDS , LOG_QUEUE_BATCH_SIZE , TIMEZONE
14+ from userbot .src .config import GC_INTERVAL_SECONDS , LOG_QUEUE_INTERVAL_SECONDS , TIMEZONE
1915from userbot .src .db .session import get_db
2016import userbot .src .db_manager as db_manager
2117from userbot .src .log_handler import log_queue
22- from userbot .src .encrypt import encryption_manager
18+
19+ # Suppress noisy APScheduler logs
20+ logging .getLogger ('apscheduler' ).setLevel (logging .WARNING )
2321
2422console : Console = Console ()
2523logger : logging .Logger = logging .getLogger (__name__ )
2624
27- # --- Log Processing Worker (unchanged) ---
2825async def process_log_queue ():
29- # ...
30- pass
31-
32- # --- Helper Functions (unchanged) ---
33- async def get_account_id_from_client (client ) -> int | None :
34- # ...
35- pass
36-
37- # --- Module Management (Placeholders) ---
38- async def load_account_modules (account_id : int , client_instance , current_help_info : Dict [str , str ]) -> None :
39- pass
40-
41- # --- Account Management Handlers ---
42- async def add_account_handler (event : events .NewMessage .Event ):
43- account_name = event .pattern_match .group (1 )
26+ """Periodically processes logs from the queue and writes them to the DB."""
27+ logs_to_process = []
4428
45- try :
46- async with event .client .conversation (event .chat_id , timeout = 600 ) as conv :
47- # Step 1: API Credentials
48- await conv .send_message (await event .client .get_string ("adding_account" , account_name = account_name ))
49- api_id_resp = await conv .get_response (); api_id = api_id_resp .text .strip ()
50- await conv .send_message (await event .client .get_string ("prompt_api_hash" ))
51- api_hash_resp = await conv .get_response (); api_hash = api_hash_resp .text .strip ()
52-
53- # Step 2: Live Verification & 2FA
54- await conv .send_message (await event .client .get_string ("verifying_creds" ))
55- temp_client = TelegramClient (StringSession (), int (api_id ), api_hash )
56- # ... (full login logic with phone, code, 2FA as in previous response)
57- await temp_client .connect ()
58- if not await temp_client .is_user_authorized ():
59- # Simplified for brevity
60- await conv .send_message ("Требуется вход. Пожалуйста, используйте CLI для первого входа." )
61- return
62- me = await temp_client .get_me (); user_id = me .id
63- await temp_client .disconnect ()
29+ while not log_queue .empty ():
30+ try :
31+ log_item = log_queue .get_nowait ()
32+ logs_to_process .append (log_item )
33+ log_queue .task_done ()
34+ except asyncio .QueueEmpty :
35+ break
6436
65- async with get_db () as db :
66- if await db_manager .get_account_by_user_id (db , user_id ):
67- # ... duplicate check
68- return
37+ if logs_to_process :
38+ try :
39+ async with get_db () as db_session :
40+ await db_manager .add_logs_bulk (db_session , logs_to_process )
41+ except Exception as e :
42+ logger .error (f"Failed to process log queue batch: { e } " , exc_info = True )
6943
70- # --- New: Proxy Configuration ---
71- proxy_details : Dict [str , Any ] = {}
72- proxy_prompt = await conv .send_message ("Настроить прокси? (да/нет)" )
73- proxy_resp = await conv .get_response ()
74- if proxy_resp .text .lower ().startswith ('д' ):
75- # ... conversation to get proxy details
76- pass
77-
78- # --- New: Device Configuration ---
79- device_details : Dict [str , str ]
80- device_prompt = await conv .send_message ("Указать кастомное устройство? (да/нет)" )
81- device_resp = await conv .get_response ()
82- if device_resp .text .lower ().startswith ('д' ):
83- # ... conversation to get device model, system version, app version
84- device_details = {"device_model" : "Custom" , "system_version" : "1.0" , "app_version" : "1.0" }
85- else :
86- device_details = _generate_random_device ()
87-
88- await conv .send_message (await event .client .get_string ("prompt_lang" )); lang_code = (await conv .get_response ()).text .strip () or 'ru'
89- await conv .send_message (await event .client .get_string ("prompt_activate" )); is_enabled = (await conv .get_response ()).text .lower ().startswith ('д' )
90-
91- await conv .send_message (await event .client .get_string ("saving_to_db" ))
92- async with get_db () as db :
93- new_acc = await db_manager .add_account (
94- db , account_name , api_id , api_hash , lang_code , is_enabled ,
95- device_details ['device_model' ], device_details ['system_version' ], device_details ['app_version' ], user_id
96- )
97- # ... logic to update proxy if provided
98-
99- if new_acc :
100- await conv .send_message (await event .client .get_string ("add_acc_success" , account_name = account_name ))
101- else :
102- await conv .send_message (await event .client .get_string ("add_acc_fail" , account_name = account_name ))
103-
104- except asyncio .TimeoutError :
105- await event .respond (await event .client .get_string ("add_acc_timeout" ))
106- except Exception as e :
107- logger .error (f"Error in .addacc handler: { e } " , exc_info = True )
108- await event .respond (await event .client .get_string ("generic_error" , error = str (e )))
109-
110-
111- # Other handlers (list, delete, toggle, setlang, help, about) are unchanged
112- async def list_accounts_handler (event : events .NewMessage .Event ): pass
113- async def delete_account_handler (event : events .NewMessage .Event ): pass
114- async def toggle_account_handler (event : events .NewMessage .Event ): pass
115- async def set_lang_handler (event : events .NewMessage .Event ): pass
116- async def help_commands_handler (event : events .NewMessage .Event ): pass
117- async def about_command_handler (event : events .NewMessage .Event ): pass
11844
11945async def main ():
12046 """The main entry point for the userbot."""
12147 console .print (text2art ("DeBot" , font = "random" , chr_ignore = True ), style = "cyan" )
48+ console .print ("\n coded by @whynothacked" , style = "yellow" )
12249
12350 await db_setup ()
12451
@@ -127,6 +54,9 @@ async def main():
12754 scheduler .add_job (process_log_queue , 'interval' , seconds = LOG_QUEUE_INTERVAL_SECONDS , id = 'log_queue_job' )
12855 scheduler .start ()
12956
57+ console .print (f"-> [system] - GC scheduled every { GC_INTERVAL_SECONDS } seconds." , style = "blue" )
58+ console .print (f"-> [system] - Log queue processing scheduled every { LOG_QUEUE_INTERVAL_SECONDS } seconds." , style = "blue" )
59+
13060 await manage_clients ()
13161
13262 logger .info ("Userbot is running. Press Ctrl+C to stop." )
0 commit comments