try and fix
Failed to send Discord message: 'Response' object has no attribute 'raise_for_status'
This commit is contained in:
parent
cf4e7fdddb
commit
b0f411a5b8
@ -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}")
|
||||
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():
|
||||
if response:
|
||||
try:
|
||||
response.close()
|
||||
except:
|
||||
pass
|
||||
Loading…
x
Reference in New Issue
Block a user