Update bot.py

This commit is contained in:
DerrtSML 2025-06-23 18:39:41 +03:00 committed by GitHub
parent 9126ec38ac
commit 5bb41a942e
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

45
bot.py
View File

@ -75,9 +75,30 @@ async def start(update: Update, context: ContextTypes.DEFAULT_TYPE) -> None:
"Отправь мне magnet-ссылку или URL torrent-файла, " "Отправь мне magnet-ссылку или URL torrent-файла, "
"чтобы добавить загрузку.\n" "чтобы добавить загрузку.\n"
"Используй /status для просмотра текущих загрузок.\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 --- # --- Команда /status ---
async def status(update: Update, context: ContextTypes.DEFAULT_TYPE) -> None: async def status(update: Update, context: ContextTypes.DEFAULT_TYPE) -> None:
if update.message is None: if update.message is None:
@ -121,6 +142,7 @@ async def status(update: Update, context: ContextTypes.DEFAULT_TYPE) -> None:
else: else:
eta_str = "Завершено" eta_str = "Завершено"
status_messages.append( status_messages.append(
f"📊 *{torrent.name}*\n" f"📊 *{torrent.name}*\n"
f" Состояние: {torrent.state}\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}") logger.error(f"An unexpected error occurred in status command: {e}")
await update.message.reply_text(f"Произошла непредвиденная ошибка: {e}") await update.message.reply_text(f"Произошла непредвиденная ошибка: {e}")
# --- Команда /stop_torrent (новая команда) --- # --- Команда /stop_torrent ---
async def stop_torrent(update: Update, context: ContextTypes.DEFAULT_TYPE) -> None: async def stop_torrent(update: Update, context: ContextTypes.DEFAULT_TYPE) -> None:
if update.message is None: if update.message is None:
logger.warning("Received an update without a message object in stop_torrent handler.") 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 = [] keyboard = []
for torrent in torrents: for torrent in torrents:
# Используем сокращенный хэш или имя, если хэш слишком длинный для кнопки
display_name = torrent.name display_name = torrent.name
if len(display_name) > 40: # Обрезаем длинные имена if len(display_name) > 40:
display_name = display_name[:37] + "..." display_name = display_name[:37] + "..."
# callback_data будет содержать полный хэш торрента
keyboard.append([InlineKeyboardButton(display_name, callback_data=f"stop_hash_{torrent.hash}")]) keyboard.append([InlineKeyboardButton(display_name, callback_data=f"stop_hash_{torrent.hash}")])
reply_markup = InlineKeyboardMarkup(keyboard) 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}") logger.error(f"An unexpected error occurred in stop_torrent command: {e}")
await update.message.reply_text(f"Произошла непредвиденная ошибка: {e}") await update.message.reply_text(f"Произошла непредвиденная ошибка: {e}")
# --- Обработка кнопки "Остановить торрент" (новая функция) --- # --- Обработка кнопки "Остановить торрент" ---
async def stop_torrent_callback(update: Update, context: ContextTypes.DEFAULT_TYPE) -> None: async def stop_torrent_callback(update: Update, context: ContextTypes.DEFAULT_TYPE) -> None:
query = update.callback_query query = update.callback_query
await query.answer() # Всегда отвечайте на CallbackQuery await query.answer()
# Извлекаем хэш торрента из callback_data
torrent_hash = query.data.replace("stop_hash_", "") torrent_hash = query.data.replace("stop_hash_", "")
logger.info(f"Attempting to stop torrent with hash: {torrent_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 return
try: try:
# qBittorrent API метод для остановки торрента
qb.torrents_stop(torrent_hashes=torrent_hash) qb.torrents_stop(torrent_hashes=torrent_hash)
await query.edit_message_text(f"Торрент ({torrent_hash[:6]}...) успешно остановлен.") await query.edit_message_text(f"Торрент ({torrent_hash[:6]}...) успешно остановлен.")
except APIError as e: except APIError as e:
@ -225,7 +243,6 @@ async def handle_url(update: Update, context: ContextTypes.DEFAULT_TYPE) -> None
try: try:
context.user_data['current_torrent_url'] = text context.user_data['current_torrent_url'] = text
# Запрашиваем категории - ИЗМЕНЕНО НА qb.torrents_categories()
categories_dict = qb.torrents_categories() categories_dict = qb.torrents_categories()
category_keyboard = [] category_keyboard = []
for category_name in categories_dict.keys(): for category_name in categories_dict.keys():
@ -270,9 +287,9 @@ async def send_directory_options(query, context):
try: try:
available_paths = [ available_paths = [
qb.app.default_save_path, qb.app.default_save_path,
"/share/Data/torrents", # ЗАМЕНИТЕ НА СВОИ АКТУАЛЬНЫЕ ПУТИ "/share/Data/Films", # ЗАМЕНИТЕ НА СВОИ АКТУАЛЬНЫЕ ПУТИ
"/share/Data/Films", "/share/Data/Serials",
"/share/Data/Serials", "/share/Data/torrents",
] ]
available_paths = sorted(list(set([p.replace(os.sep, '/') for p in available_paths if p]))) 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("start", start))
application.add_handler(CommandHandler("status", status)) application.add_handler(CommandHandler("status", status))
# Новая команда для остановки торрентов
application.add_handler(CommandHandler("stop_torrent", stop_torrent)) 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]+" url_regex = r"magnet:\?xt=urn:[a-z0-9]+"