Skip to content

Commit

Permalink
Handle cloudflare bans on webhook requests
Browse files Browse the repository at this point in the history
  • Loading branch information
Rapptz committed Sep 10, 2020
1 parent 90b696f commit e286496
Showing 1 changed file with 8 additions and 0 deletions.
8 changes: 8 additions & 0 deletions discord/webhook.py
Original file line number Diff line number Diff line change
Expand Up @@ -222,6 +222,10 @@ async def request(self, verb, url, payload=None, multipart=None, *, files=None,

# we are being rate limited
if r.status == 429:
if not r.headers.get('Via'):
# Banned by Cloudflare more than likely.
raise HTTPException(r, data)

retry_after = response['retry_after'] / 1000.0
log.warning('Webhook ID %s is rate limited. Retrying in %.2f seconds', _id, retry_after)
await asyncio.sleep(retry_after)
Expand Down Expand Up @@ -317,6 +321,10 @@ def request(self, verb, url, payload=None, multipart=None, *, files=None, reason
# we are being rate limited
if r.status == 429:
if self.sleep:
if not r.headers.get('Via'):
# Banned by Cloudflare more than likely.
raise HTTPException(r, data)

retry_after = response['retry_after'] / 1000.0
log.warning('Webhook ID %s is rate limited. Retrying in %.2f seconds', _id, retry_after)
time.sleep(retry_after)
Expand Down

0 comments on commit e286496

Please sign in to comment.