|
10 | 10 | from telethon import events, helpers |
11 | 11 | from telethon.errors import SessionPasswordNeededError |
12 | 12 | from telethon.sessions import StringSession |
| 13 | +from art import text2art |
13 | 14 |
|
14 | 15 | from userbot import ACTIVE_CLIENTS, LOOP, GLOBAL_HELP_INFO, FAKE, TelegramClient |
15 | 16 | from userbot.src.config import GC_INTERVAL_SECONDS |
|
22 | 23 |
|
23 | 24 | # --- Helper Functions --- |
24 | 25 | async def get_account_id_from_client(client) -> int | None: |
| 26 | + """Gets the account_id associated with a client instance.""" |
25 | 27 | return next((acc_id for acc_id, c in ACTIVE_CLIENTS.items() if c == client), None) |
26 | 28 |
|
27 | | -# --- Module Management --- |
| 29 | +# --- Module Management (Placeholders) --- |
| 30 | +async def load_account_modules(account_id: int, client_instance, current_help_info: Dict[str, str]) -> None: |
| 31 | + """Loads all active and trusted modules for a given account.""" |
| 32 | + console.print(f"[MODULES] - Loading modules for account_id: {account_id}...", style="yellow") |
| 33 | + # In a real scenario, this would populate current_help_info based on loaded modules |
| 34 | + pass |
| 35 | + |
28 | 36 | async def delmod_handler(event: events.NewMessage.Event): |
29 | 37 | """Handles unlinking a module and safely uninstalling its dependencies.""" |
30 | | - # Simplified handler logic for brevity |
31 | | - module_name_to_delete = event.pattern_match.group(1) |
32 | | - account_id = await get_account_id_from_client(event.client) |
33 | | - if not account_id: return |
34 | | - |
35 | | - uninstalled_deps: List[str] = [] |
36 | | - async with get_db() as db: |
37 | | - module_to_delete = await db_manager.get_module(db, module_name_to_delete) |
38 | | - if not module_to_delete: |
39 | | - await event.edit(f"Модуль '{module_name_to_delete}' не найден.") |
40 | | - return |
41 | | - |
42 | | - # 1. Get module dependencies |
43 | | - dependencies_to_check: List[str] = [] |
44 | | - try: |
45 | | - with open(module_to_delete.module_path, 'r', encoding='utf-8') as f: |
46 | | - metadata, _ = parse_module_metadata(f.read()) |
47 | | - dependencies_to_check = metadata.get("requires", []) |
48 | | - except FileNotFoundError: |
49 | | - logger.warning(f"Could not find file for module '{module_name_to_delete}' to check dependencies.") |
50 | | - |
51 | | - # 2. Unlink module from account |
52 | | - await db_manager.unlink_module_from_account(db, account_id, module_to_delete.module_id) |
53 | | - |
54 | | - # 3. Check if dependencies are used by other modules |
55 | | - if dependencies_to_check: |
56 | | - all_other_deps: Set[str] = set() |
57 | | - all_active_modules = await db_manager.get_all_active_modules(db) # Needs implementation |
58 | | - for mod in all_active_modules: |
59 | | - if mod.module_id == module_to_delete.module_id: continue |
60 | | - try: |
61 | | - with open(mod.module_path, 'r', encoding='utf-8') as f: |
62 | | - meta, _ = parse_module_metadata(f.read()) |
63 | | - all_other_deps.update(meta.get("requires", [])) |
64 | | - except FileNotFoundError: |
65 | | - continue |
66 | | - |
67 | | - # 4. Uninstall unused dependencies |
68 | | - for dep in dependencies_to_check: |
69 | | - if dep not in all_other_deps: |
70 | | - try: |
71 | | - subprocess.run([sys.executable, "-m", "pip", "uninstall", "-y", dep], check=True) |
72 | | - uninstalled_deps.append(dep) |
73 | | - logger.info(f"Uninstalled unused dependency: {dep}") |
74 | | - except subprocess.CalledProcessError: |
75 | | - logger.error(f"Failed to uninstall dependency: {dep}") |
76 | | - |
77 | | - response = f"Модуль '{module_name_to_delete}' отвязан." |
78 | | - if uninstalled_deps: |
79 | | - response += f"\nУдалены зависимости: `{' '.join(uninstalled_deps)}`" |
80 | | - |
81 | | - await event.edit(response) |
82 | | - |
| 38 | + await event.edit("Команда `.delmod` в разработке.") |
83 | 39 |
|
84 | 40 | # --- Account Management Handlers --- |
85 | 41 | async def list_accounts_handler(event: events.NewMessage.Event): |
@@ -122,11 +78,15 @@ async def add_account_handler(event: events.NewMessage.Event): |
122 | 78 |
|
123 | 79 | await conv.send_message(await event.client.get_string("verifying_creds")) |
124 | 80 | temp_client = TelegramClient(StringSession(), int(api_id), api_hash) |
125 | | - user_id, two_fa_pass = None, None |
| 81 | + user_id = None |
126 | 82 | try: |
127 | 83 | await temp_client.connect() |
128 | 84 | if not await temp_client.is_user_authorized(): |
129 | | - raise ValueError("Failed to authorize, manual sign-in flow is complex for this example.") |
| 85 | + # This part is highly simplified. A real sign-in flow is more complex. |
| 86 | + phone_number = await temp_client.send_code_request(api_id) |
| 87 | + await conv.send_message(f"Код отправлен. Введите его:") |
| 88 | + code = (await conv.get_response()).text.strip() |
| 89 | + await temp_client.sign_in(password=code) |
130 | 90 | except SessionPasswordNeededError: |
131 | 91 | await conv.send_message(await event.client.get_string("prompt_2fa")) |
132 | 92 | two_fa_pass_resp = await conv.get_response(); two_fa_pass = two_fa_pass_resp.text.strip() |
@@ -167,18 +127,22 @@ async def add_account_handler(event: events.NewMessage.Event): |
167 | 127 | async def delete_account_handler(event: events.NewMessage.Event): |
168 | 128 | """Deletes an account from the database.""" |
169 | 129 | account_name = event.pattern_match.group(1) |
170 | | - async with event.client.conversation(event.chat_id, timeout=60) as conv: |
171 | | - await conv.send_message(await event.client.get_string("confirm_delete", account_name=account_name)) |
172 | | - confirmation = await conv.get_response() |
173 | | - if confirmation.text.lower() in ['да', 'yes']: |
174 | | - async with get_db() as db: |
175 | | - success = await db_manager.delete_account(db, account_name) |
176 | | - if success: |
177 | | - await conv.send_message(await event.client.get_string("delete_success", account_name=account_name)) |
| 130 | + try: |
| 131 | + async with event.client.conversation(event.chat_id, timeout=60) as conv: |
| 132 | + await conv.send_message(await event.client.get_string("confirm_delete", account_name=account_name)) |
| 133 | + confirmation = await conv.get_response() |
| 134 | + if confirmation.text.lower() in ['да', 'yes']: |
| 135 | + async with get_db() as db: |
| 136 | + success = await db_manager.delete_account(db, account_name) |
| 137 | + if success: |
| 138 | + await conv.send_message(await event.client.get_string("delete_success", account_name=account_name)) |
| 139 | + else: |
| 140 | + await conv.send_message(await event.client.get_string("delete_fail", account_name=account_name)) |
178 | 141 | else: |
179 | | - await conv.send_message(await event.client.get_string("delete_fail", account_name=account_name)) |
180 | | - else: |
181 | | - await conv.send_message(await event.client.get_string("delete_cancelled")) |
| 142 | + await conv.send_message(await event.client.get_string("delete_cancelled")) |
| 143 | + except asyncio.TimeoutError: |
| 144 | + await event.respond(await event.client.get_string("delete_timeout")) |
| 145 | + |
182 | 146 |
|
183 | 147 | async def toggle_account_handler(event: events.NewMessage.Event): |
184 | 148 | """Enables or disables an account.""" |
@@ -229,13 +193,17 @@ async def about_command_handler(event: events.NewMessage.Event): |
229 | 193 | # --- Main Execution --- |
230 | 194 | if __name__ == "__main__": |
231 | 195 | console.print(text2art("DeBot", font="random", chr_ignore=True), style="cyan") |
| 196 | + console.print("\n coded by @whynothacked", style="yellow") |
| 197 | + |
232 | 198 | scheduler = AsyncIOScheduler(timezone="UTC") |
233 | 199 | scheduler.add_job(gc.collect, 'interval', seconds=GC_INTERVAL_SECONDS, id='gc_job') |
234 | 200 | scheduler.start() |
| 201 | + console.print(f"-> [system] - GC scheduled every {GC_INTERVAL_SECONDS} seconds.", style="blue") |
235 | 202 |
|
236 | 203 | try: |
237 | 204 | LOOP.run_forever() |
238 | 205 | except KeyboardInterrupt: |
239 | 206 | console.print("\n[MAIN] - Userbot stopped by user.", style="bold yellow") |
240 | 207 | finally: |
241 | 208 | if scheduler.running: scheduler.shutdown() |
| 209 | + console.print("[MAIN] - Userbot shutdown complete.", style="bold green") |
0 commit comments