From c680ed17f1824013084e3260d9b0b01f222478be Mon Sep 17 00:00:00 2001 From: sickprodigy Date: Mon, 3 Nov 2025 20:35:26 -0500 Subject: [PATCH] try and fix Failed to send Discord message: 'Response' object has no attribute 'raise_for_status' --- Scripts/discord_webhook.py | 29 ++++++++++++++++++++--------- 1 file changed, 20 insertions(+), 9 deletions(-) diff --git a/Scripts/discord_webhook.py b/Scripts/discord_webhook.py index df8be47..f32b49a 100644 --- a/Scripts/discord_webhook.py +++ b/Scripts/discord_webhook.py @@ -1,22 +1,33 @@ import urequests as requests +import ujson from secrets import secrets def send_discord_message(message, username="Auto Garden Bot"): + response = None try: data = { "content": message, "username": username } - - response = requests.post(secrets['discord_webhook_url'], json=data) - response.raise_for_status() - print(f"Discord message sent successfully, code {response.status_code}") - return True - + headers = {"Content-Type": "application/json"} + response = requests.post( + secrets['discord_webhook_url'], + data=ujson.dumps(data), + headers=headers + ) + status = getattr(response, "status", getattr(response, "status_code", None)) + if status and 200 <= status < 300: + print(f"Discord message sent successfully, code {status}") + return True + else: + print(f"Discord webhook error: HTTP {status}, body: {getattr(response, 'text', '')}") + return False except Exception as e: print(f"Failed to send Discord message: {str(e)}") return False - finally: - if 'response' in locals(): - response.close() \ No newline at end of file + if response: + try: + response.close() + except: + pass \ No newline at end of file