Spaces:
Runtime error
Runtime error
| # filename: handlers/user.py | |
| import logging | |
| from telethon import events, Button | |
| from core.bot import bot, BATCH_JOBS, ACTIVE_USER_TASKS, USER_TURN_ORDER | |
| import templates | |
| from database import manager as db_manager | |
| from datetime import datetime | |
| import config | |
| from utils.helpers import format_bytes | |
| logger = logging.getLogger(__name__) | |
| async def myaccount_handler(event): | |
| await process_myaccount_query(event) | |
| async def myaccount_button_handler(event): | |
| await process_myaccount_query(event) | |
| async def process_myaccount_query(event): | |
| """Core logic for handling myaccount requests from commands or buttons.""" | |
| user_id = event.sender_id | |
| user = await db_manager.get_user(user_id) | |
| if not user: | |
| # This case is unlikely if they used /start, but good to have. | |
| sender = await event.get_sender() | |
| user = await db_manager.add_or_update_user(sender.id, sender.username, sender.first_name) | |
| if user.get('is_premium') and user.get('premium_expiry_date') > datetime.utcnow(): | |
| response = templates.BotResponses.MY_ACCOUNT_PREMIUM.format( | |
| user_id=user['user_id'], | |
| expiry_date=user['premium_expiry_date'].strftime('%Y-%m-%d %H:%M IST') | |
| ) | |
| else: | |
| response = templates.BotResponses.MY_ACCOUNT_FREE.format( | |
| user_id=user['user_id'], | |
| free_limit=format_bytes(config.FREE_USER_FILE_SIZE_LIMIT_BYTES), | |
| owner_id=config.OWNER_ID | |
| ) | |
| # For callback queries, we edit the message. For commands, we reply. | |
| if isinstance(event, events.CallbackQuery.Event): | |
| await event.edit(response, buttons=[Button.inline("⬅️ Back", data="start_menu")]) | |
| else: | |
| await event.reply(response) | |
| async def cancel_handler(event): | |
| batch_id = event.pattern_match.group(1) | |
| user_id = event.sender_id | |
| job = BATCH_JOBS.get(batch_id) | |
| if not job: | |
| await event.reply("This job ID is invalid or has already been completed.") | |
| return | |
| if job['user_id'] != user_id: | |
| await event.reply("You can only cancel your own jobs.") | |
| return | |
| # --- Cancellation Logic --- | |
| # 1. Clear pending tasks for this user from the scheduler's view | |
| if user_id in ACTIVE_USER_TASKS: | |
| ACTIVE_USER_TASKS[user_id] = [task for task in ACTIVE_USER_TASKS[user_id] if task['batch_id'] != batch_id] | |
| if not ACTIVE_USER_TASKS[user_id]: | |
| del ACTIVE_USER_TASKS[user_id] | |
| # Try to remove the user from the turn order deque | |
| try: | |
| USER_TURN_ORDER.remove(user_id) | |
| except ValueError: | |
| pass # User might not be in the deque anymore | |
| # 2. Delete the job from the main tracking dictionary | |
| del BATCH_JOBS[batch_id] | |
| # 3. Inform the user | |
| await bot.edit_message( | |
| job['chat_id'], | |
| job['status_message_id'], | |
| templates.BotResponses.BATCH_CANCELLED.format(batch_id=batch_id) | |
| ) | |
| logger.info(f"User {user_id} cancelled Batch #{batch_id}") | |