Address code review: improve error handling for trap command

Co-authored-by: kuhyx <147418882+kuhyx@users.noreply.github.com>
This commit is contained in:
copilot-swe-agent[bot] 2025-12-01 15:18:12 +00:00
parent 39ea43d2a4
commit 730073eda6

20
main.py
View File

@ -105,9 +105,13 @@ def send_message(message_content, recipients=PHONE_NUMBER):
def get_trap_image(): def get_trap_image():
"""Fetch a random trap image from rule34.""" """Fetch a random trap image from rule34."""
r34 = rule34Py() try:
post = r34.random_post() r34 = rule34Py()
return download_image(post.image) 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): 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): async def handle_trap_command(recipient, user_uuid):
"""Handle trap command with 24hr rate limiting per user.""" """Handle trap command with 24hr rate limiting per user."""
if can_use_trap(user_uuid): if can_use_trap(user_uuid):
user_trap_usage[user_uuid] = datetime.now() image = get_trap_image()
await send_image(get_trap_image(), recipient) 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: else:
remaining = get_trap_cooldown_remaining(user_uuid) remaining = get_trap_cooldown_remaining(user_uuid)
hours = int(remaining.total_seconds() // 3600) 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) await handle_trap_command(recipient, user_uuid)
last_command_time = current_time last_command_time = current_time
warning_sent = False warning_sent = False
else:
send_message("Nie mozna zidentyfikowac uzytkownika.", recipient)
return return
for command_triggers, command_function in command_map.items(): for command_triggers, command_function in command_map.items():