From 730073eda613ef78f89b73b2c6c66576bfc0c70a Mon Sep 17 00:00:00 2001 From: "copilot-swe-agent[bot]" <198982749+Copilot@users.noreply.github.com> Date: Mon, 1 Dec 2025 15:18:12 +0000 Subject: [PATCH] Address code review: improve error handling for trap command Co-authored-by: kuhyx <147418882+kuhyx@users.noreply.github.com> --- main.py | 20 +++++++++++++++----- 1 file changed, 15 insertions(+), 5 deletions(-) diff --git a/main.py b/main.py index 9cc9695..04ffb1b 100644 --- a/main.py +++ b/main.py @@ -105,9 +105,13 @@ def send_message(message_content, recipients=PHONE_NUMBER): def get_trap_image(): """Fetch a random trap image from rule34.""" - r34 = rule34Py() - post = r34.random_post() - return download_image(post.image) + try: + r34 = rule34Py() + post = r34.random_post() + return download_image(post.image) + except Exception as e: + print(f"Error fetching trap image: {e}") + return None def can_use_trap(user_uuid): @@ -130,8 +134,12 @@ def get_trap_cooldown_remaining(user_uuid): async def handle_trap_command(recipient, user_uuid): """Handle trap command with 24hr rate limiting per user.""" if can_use_trap(user_uuid): - user_trap_usage[user_uuid] = datetime.now() - await send_image(get_trap_image(), recipient) + image = get_trap_image() + if image: + user_trap_usage[user_uuid] = datetime.now() + await send_image(image, recipient) + else: + send_message("Nie udalo sie pobrac obrazka. Sprobuj ponownie pozniej.", recipient) else: remaining = get_trap_cooldown_remaining(user_uuid) hours = int(remaining.total_seconds() // 3600) @@ -212,6 +220,8 @@ async def trigger_command(message_content, recipient, user_uuid=None): await handle_trap_command(recipient, user_uuid) last_command_time = current_time warning_sent = False + else: + send_message("Nie mozna zidentyfikowac uzytkownika.", recipient) return for command_triggers, command_function in command_map.items():