try and fix
Failed to send Discord message: 'Response' object has no attribute 'raise_for_status'
This commit is contained in:
parent
2d96b56a55
commit
c680ed17f1
@ -1,22 +1,33 @@
|
|||||||
import urequests as requests
|
import urequests as requests
|
||||||
|
import ujson
|
||||||
from secrets import secrets
|
from secrets import secrets
|
||||||
|
|
||||||
def send_discord_message(message, username="Auto Garden Bot"):
|
def send_discord_message(message, username="Auto Garden Bot"):
|
||||||
|
response = None
|
||||||
try:
|
try:
|
||||||
data = {
|
data = {
|
||||||
"content": message,
|
"content": message,
|
||||||
"username": username
|
"username": username
|
||||||
}
|
}
|
||||||
|
headers = {"Content-Type": "application/json"}
|
||||||
response = requests.post(secrets['discord_webhook_url'], json=data)
|
response = requests.post(
|
||||||
response.raise_for_status()
|
secrets['discord_webhook_url'],
|
||||||
print(f"Discord message sent successfully, code {response.status_code}")
|
data=ujson.dumps(data),
|
||||||
return True
|
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:
|
except Exception as e:
|
||||||
print(f"Failed to send Discord message: {str(e)}")
|
print(f"Failed to send Discord message: {str(e)}")
|
||||||
return False
|
return False
|
||||||
|
|
||||||
finally:
|
finally:
|
||||||
if 'response' in locals():
|
if response:
|
||||||
response.close()
|
try:
|
||||||
|
response.close()
|
||||||
|
except:
|
||||||
|
pass
|
||||||
Loading…
x
Reference in New Issue
Block a user