mirror of
https://github.com/kuhyx/signal-bot.git
synced 2026-07-04 11:43:03 +02:00
Address code review: improve error handling for trap command
Co-authored-by: kuhyx <147418882+kuhyx@users.noreply.github.com>
This commit is contained in:
parent
39ea43d2a4
commit
730073eda6
20
main.py
20
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():
|
||||
|
||||
Loading…
Reference in New Issue
Block a user