From 5bb41a942edb967e1e4874d8201a81eb625c4a37 Mon Sep 17 00:00:00 2001 From: DerrtSML <93052047+DerrtSML@users.noreply.github.com> Date: Mon, 23 Jun 2025 18:39:41 +0300 Subject: [PATCH] Update bot.py --- bot.py | 45 +++++++++++++++++++++++++++++++-------------- 1 file changed, 31 insertions(+), 14 deletions(-) diff --git a/bot.py b/bot.py index 4bc89d2..b2157fb 100644 --- a/bot.py +++ b/bot.py @@ -75,9 +75,30 @@ async def start(update: Update, context: ContextTypes.DEFAULT_TYPE) -> None: "Отправь мне magnet-ссылку или URL torrent-файла, " "чтобы добавить загрузку.\n" "Используй /status для просмотра текущих загрузок.\n" - "Используй /stop_torrent для остановки загрузки." + "Используй /stop_torrent для остановки загрузки.\n" + "Используй /help для списка команд." ) +# --- Команда /help (новая функция) --- +async def help_command(update: Update, context: ContextTypes.DEFAULT_TYPE) -> None: + if update.message is None: + logger.warning("Received an update without a message object in help handler.") + return + + logger.info(f"Received /help command from {update.effective_user.id}") + + help_text = ( + "Вот список доступных команд:\n\n" + "**/start** - Начать работу с ботом и проверить подключение к qBittorrent.\n" + "**/status** - Показать текущий статус всех активных загрузок.\n" + "**/stop_torrent** - Выбрать и остановить загрузку торрента.\n" + "**/help** - Показать это справочное сообщение.\n\n" + "Также вы можете отправить мне *magnet-ссылку* или *URL torrent-файла* " + "для добавления загрузки. Бот предложит выбрать категорию и директорию." + ) + await update.message.reply_text(help_text, parse_mode="Markdown") + + # --- Команда /status --- async def status(update: Update, context: ContextTypes.DEFAULT_TYPE) -> None: if update.message is None: @@ -121,6 +142,7 @@ async def status(update: Update, context: ContextTypes.DEFAULT_TYPE) -> None: else: eta_str = "Завершено" + status_messages.append( f"📊 *{torrent.name}*\n" f" Состояние: {torrent.state}\n" @@ -141,7 +163,7 @@ async def status(update: Update, context: ContextTypes.DEFAULT_TYPE) -> None: logger.error(f"An unexpected error occurred in status command: {e}") await update.message.reply_text(f"Произошла непредвиденная ошибка: {e}") -# --- Команда /stop_torrent (новая команда) --- +# --- Команда /stop_torrent --- async def stop_torrent(update: Update, context: ContextTypes.DEFAULT_TYPE) -> None: if update.message is None: logger.warning("Received an update without a message object in stop_torrent handler.") @@ -163,12 +185,10 @@ async def stop_torrent(update: Update, context: ContextTypes.DEFAULT_TYPE) -> No keyboard = [] for torrent in torrents: - # Используем сокращенный хэш или имя, если хэш слишком длинный для кнопки display_name = torrent.name - if len(display_name) > 40: # Обрезаем длинные имена + if len(display_name) > 40: display_name = display_name[:37] + "..." - # callback_data будет содержать полный хэш торрента keyboard.append([InlineKeyboardButton(display_name, callback_data=f"stop_hash_{torrent.hash}")]) reply_markup = InlineKeyboardMarkup(keyboard) @@ -181,12 +201,11 @@ async def stop_torrent(update: Update, context: ContextTypes.DEFAULT_TYPE) -> No logger.error(f"An unexpected error occurred in stop_torrent command: {e}") await update.message.reply_text(f"Произошла непредвиденная ошибка: {e}") -# --- Обработка кнопки "Остановить торрент" (новая функция) --- +# --- Обработка кнопки "Остановить торрент" --- async def stop_torrent_callback(update: Update, context: ContextTypes.DEFAULT_TYPE) -> None: query = update.callback_query - await query.answer() # Всегда отвечайте на CallbackQuery + await query.answer() - # Извлекаем хэш торрента из callback_data torrent_hash = query.data.replace("stop_hash_", "") logger.info(f"Attempting to stop torrent with hash: {torrent_hash}") @@ -197,7 +216,6 @@ async def stop_torrent_callback(update: Update, context: ContextTypes.DEFAULT_TY return try: - # qBittorrent API метод для остановки торрента qb.torrents_stop(torrent_hashes=torrent_hash) await query.edit_message_text(f"Торрент ({torrent_hash[:6]}...) успешно остановлен.") except APIError as e: @@ -225,7 +243,6 @@ async def handle_url(update: Update, context: ContextTypes.DEFAULT_TYPE) -> None try: context.user_data['current_torrent_url'] = text - # Запрашиваем категории - ИЗМЕНЕНО НА qb.torrents_categories() categories_dict = qb.torrents_categories() category_keyboard = [] for category_name in categories_dict.keys(): @@ -270,9 +287,9 @@ async def send_directory_options(query, context): try: available_paths = [ qb.app.default_save_path, - "/share/Data/torrents", # ЗАМЕНИТЕ НА СВОИ АКТУАЛЬНЫЕ ПУТИ - "/share/Data/Films", - "/share/Data/Serials", + "/share/Data/Films", # ЗАМЕНИТЕ НА СВОИ АКТУАЛЬНЫЕ ПУТИ + "/share/Data/Serials", + "/share/Data/torrents", ] available_paths = sorted(list(set([p.replace(os.sep, '/') for p in available_paths if p]))) @@ -373,8 +390,8 @@ def main() -> None: # --- Добавление обработчиков команд --- application.add_handler(CommandHandler("start", start)) application.add_handler(CommandHandler("status", status)) - # Новая команда для остановки торрентов application.add_handler(CommandHandler("stop_torrent", stop_torrent)) + application.add_handler(CommandHandler("help", help_command)) # Новая команда /help # --- Добавление обработчиков сообщений --- url_regex = r"magnet:\?xt=urn:[a-z0-9]+"