simple rate limit handling
This commit is contained in:
parent
1026d47061
commit
b6ef3a0a1c
10
webhook.py
10
webhook.py
|
@ -3,6 +3,9 @@
|
||||||
|
|
||||||
import requests
|
import requests
|
||||||
import json
|
import json
|
||||||
|
import time
|
||||||
|
|
||||||
|
now = time.time()
|
||||||
|
|
||||||
# https://discordapp.com/developers/docs/resources/webhook#execute-webhook
|
# https://discordapp.com/developers/docs/resources/webhook#execute-webhook
|
||||||
class Webhook():
|
class Webhook():
|
||||||
|
@ -16,6 +19,8 @@ class Webhook():
|
||||||
"embeds": []
|
"embeds": []
|
||||||
}
|
}
|
||||||
|
|
||||||
|
self.requests = requests.Session()
|
||||||
|
|
||||||
def add_embed(self, embed):
|
def add_embed(self, embed):
|
||||||
self.wh["embeds"].append(embed)
|
self.wh["embeds"].append(embed)
|
||||||
|
|
||||||
|
@ -27,7 +32,10 @@ class Webhook():
|
||||||
self.wh["tts"] = tts
|
self.wh["tts"] = tts
|
||||||
#print(self.wh)
|
#print(self.wh)
|
||||||
|
|
||||||
r = requests.post(self.url, json=self.wh)
|
r = self.requests.post(self.url, json=self.wh)
|
||||||
|
|
||||||
|
if "X-RateLimit-Remaining" in r.headers and int(r.headers["X-RateLimit-Remaining"]) <= 5:
|
||||||
|
raise Exception("RateLimit {0}/{1}, reset in ~{2}s".format(r.headers["X-RateLimit-Remaining"], r.headers["X-RateLimit-Limit"], int(r.headers["X-RateLimit-Reset"])-int(now)))
|
||||||
|
|
||||||
if r.text != "":
|
if r.text != "":
|
||||||
raise Exception("Webhook error: {0}".format(r.text))
|
raise Exception("Webhook error: {0}".format(r.text))
|
||||||
|
|
Loading…
Reference in New Issue