Update bot.py

This commit is contained in:
DerrtSML 2025-06-23 20:25:10 +03:00 committed by GitHub
parent fb31ae8c01
commit ee23ab8ad7
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

51
bot.py
View File

@ -401,7 +401,58 @@ async def echo(update: Update, context: ContextTypes.DEFAULT_TYPE) -> None:
logger.info(f"Received non-command text: {update.message.text} from {update.effective_user.id}")
await update.message.reply_text(f"Вы сказали: {update.message.text}")
# --- Обработка выбора директории и добавление торрента (ОБНОВЛЕНО с логированием) ---
async def select_directory_callback(update: Update, context: ContextTypes.DEFAULT_TYPE) -> None:
query = update.callback_query
await query.answer()
data = query.data.split('_')
# Собираем путь обратно из частей, если в нем были подчеркивания
selected_directory = "_".join(data[2:])
logger.info(f"Selected directory: {selected_directory}")
torrent_url = context.user_data.get('current_torrent_url')
category = context.user_data.get('selected_category')
if not torrent_url:
await query.edit_message_text("Ошибка: URL торрента не найден. Пожалуйста, попробуйте снова.")
return
if not init_qbittorrent_client():
await query.edit_message_text(
"Не удалось подключиться к qBittorrent. Проверьте переменные окружения и доступность сервера."
)
return
try:
# --- НОВОЕ ЛОГИРОВАНИЕ ПЕРЕД ДОБАВЛЕНИЕМ ТОРРЕНТА ---
logger.info(f"Attempting to add torrent:")
logger.info(f" URL: {torrent_url}")
logger.info(f" Category: {category or 'None'}")
logger.info(f" Save Path: {selected_directory}")
# --- КОНЕЦ НОВОГО ЛОГИРОВАНИЯ ---
qb.torrents_add(
urls=torrent_url,
category=category,
save_path=selected_directory
)
await query.edit_message_text(
f"Торрент успешно добавлен в qBittorrent.\n"
f"Категория: {category or 'Без категории'}\n"
f"Директория: {selected_directory}"
)
if 'current_torrent_url' in context.user_data:
del context.user_data['current_torrent_url']
if 'selected_category' in context.user_data:
del context.user_data['selected_category']
except APIError as e:
logger.error(f"API Error adding torrent: {e}") # Более специфичное сообщение
await query.edit_message_text(f"Ошибка API при добавлении торрента: {e}")
except Exception as e:
logger.error(f"An unexpected error occurred during torrent addition: {e}") # Более специфичное сообщение
await query.edit_message_text(f"Произошла непредвиденная ошибка при добавлении торрента: {e}")
# --- Основная функция, запускающая бота ---
def main() -> None:
if not TELEGRAM_BOT_TOKEN: