diff --git a/handlers/admin/admin_users.py b/handlers/admin/admin_users.py index 38841988..b47c3221 100644 --- a/handlers/admin/admin_users.py +++ b/handlers/admin/admin_users.py @@ -3,6 +3,7 @@ from datetime import datetime from typing import Any from aiogram import F, Router, types +from aiogram.exceptions import TelegramBadRequest from aiogram.fsm.context import FSMContext from aiogram.fsm.state import State, StatesGroup from aiogram.types import CallbackQuery @@ -44,9 +45,11 @@ async def handle_search_user( state: FSMContext ): text = ( - "🔍 Введите ID или Username пользователя для поиска:" + "🔍 Поиск пользователя" + "\n\n📌 Введите ID, Username или перешлите сообщение пользователя." "\n\n🆔 ID - числовой айди" "\n📝 Username - юзернейм пользователя" + "\n\n✉️ Для поиска, вы можете просто переслать сообщение от пользователя." ) await state.set_state(UserEditorState.waiting_for_user_data) @@ -82,6 +85,11 @@ async def handle_user_data_input( ): kb = build_admin_back_kb() + if message.forward_from: + tg_id = message.forward_from.id + await process_user_search(message, state, session, tg_id) + return + if not message.text: await message.answer( text="🚫 Пожалуйста, отправьте текстовое сообщение.", @@ -508,6 +516,8 @@ async def handle_update_key( try: await update_subscription(tg_id, email, session) await handle_key_edit(callback_query, callback_data, session) + except TelegramBadRequest: + pass except Exception as e: logger.error(f"Ошибка при обновлении ключа {email} администратором: {e}") await callback_query.message.answer( @@ -539,7 +549,7 @@ async def handle_delete_key( await callback_query.message.edit_text( text="❓ Вы уверены, что хотите удалить ключ?", - reply_markup=build_key_delete_kb(callback_data.tg_id, client_id) + reply_markup=build_key_delete_kb(callback_data.tg_id, email) ) @@ -552,26 +562,27 @@ async def handle_delete_key_confirm( callback_data: AdminUserEditorCallback, session: Any ): - client_id = callback_data.data + email = callback_data.data record = await session.fetchrow( - "SELECT email FROM keys WHERE client_id = $1", client_id + "SELECT client_id FROM keys WHERE email = $1", email ) kb = build_editor_kb(callback_data.tg_id) if record: + client_id = record["client_id"] clusters = await get_servers_from_db() - async def delete_key_from_servers(email, client_id): + async def delete_key_from_servers(): tasks = [] for cluster_name, cluster_servers in clusters.items(): - for server in cluster_servers: + for _ in cluster_servers: tasks.append( delete_key_from_cluster(cluster_name, email, client_id) ) await asyncio.gather(*tasks) - await delete_key_from_servers(record["email"], client_id) + await delete_key_from_servers() await delete_key_from_db(client_id, session) await callback_query.message.edit_text( @@ -698,10 +709,13 @@ async def process_user_search( kb = build_user_edit_kb(tg_id, key_records) if edit: - await message.edit_text( - text=text, - reply_markup=kb - ) + try: + await message.edit_text( + text=text, + reply_markup=kb + ) + except TelegramBadRequest: + pass else: await message.answer( text=text, diff --git a/keyboards/admin/users_kb.py b/keyboards/admin/users_kb.py index 22133aaa..b71a3547 100644 --- a/keyboards/admin/users_kb.py +++ b/keyboards/admin/users_kb.py @@ -27,6 +27,13 @@ def build_user_edit_kb(tg_id: int, key_records: list) -> InlineKeyboardMarkup: ).pack() ) + builder.button( + text="✉️ Сообщение", + callback_data=AdminUserEditorCallback( + action="users_send_message", + tg_id=tg_id + ).pack() + ) builder.button( text="💸 Изменить баланс", callback_data=AdminUserEditorCallback( @@ -35,15 +42,7 @@ def build_user_edit_kb(tg_id: int, key_records: list) -> InlineKeyboardMarkup: ).pack() ) builder.button( - text="✉️ Сообщение", - callback_data=AdminUserEditorCallback( - action="users_send_message", - tg_id=tg_id - ).pack() - ) - - builder.button( - text="🔄 Восстановить триал", + text="♻️ Восстановить триал", callback_data=AdminUserEditorCallback( action="users_trial_restore", tg_id=tg_id @@ -188,14 +187,14 @@ def build_key_edit_kb(key_details: dict, email: str) -> InlineKeyboardMarkup: return builder.as_markup() -def build_key_delete_kb(tg_id: int, client_id: str) -> InlineKeyboardMarkup: +def build_key_delete_kb(tg_id: int, email: str) -> InlineKeyboardMarkup: builder = InlineKeyboardBuilder() builder.row( InlineKeyboardButton( text="✅ Да, удалить", callback_data=AdminUserEditorCallback( action="users_delete_key_confirm", - data=client_id, + data=email, tg_id=tg_id ).pack() )