Skip to content

Commit b63ac98

Browse files
committed
fix(ratelimit handling): Fixed old ratelimit handling for webhooks
1 parent 3c11647 commit b63ac98

File tree

1 file changed

+19
-11
lines changed

1 file changed

+19
-11
lines changed

discord/webhook.py

Lines changed: 19 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -99,11 +99,17 @@ def delete_webhook(self, *, reason=None):
9999
def edit_webhook(self, *, reason=None, **payload):
100100
return self.request('PATCH', self._request_url, payload=payload, reason=reason)
101101

102-
def edit_webhook_message(self, message_id, payload):
103-
return self.request('PATCH', '{}/messages/{}'.format(self._request_url, message_id), payload=payload)
104-
105-
def delete_webhook_message(self, message_id):
106-
return self.request('DELETE', '{}/messages/{}'.format(self._request_url, message_id))
102+
def edit_webhook_message(self, message_id, payload, thread_id: int = None):
103+
url = '{}/messages/{}'.format(self._request_url, message_id)
104+
if thread_id:
105+
url += f'?thread_id={thread_id}'
106+
return self.request('PATCH', url, payload=payload)
107+
108+
def delete_webhook_message(self, message_id, thread_id: int = None):
109+
url = '{}/messages/{}'.format(self._request_url, message_id)
110+
if thread_id:
111+
url += f'?thread_id={thread_id}'
112+
return self.request('DELETE', url)
107113

108114
def handle_execution_response(self, data, *, wait):
109115
"""Transforms the webhook execution response into something
@@ -129,7 +135,7 @@ async def _wrap_coroutine_and_cleanup(self, coro, cleanup):
129135
finally:
130136
cleanup()
131137

132-
def execute_webhook(self, *, payload, wait=False, file=None, files=None):
138+
def execute_webhook(self, *, payload, wait=False, thread_id: int = None, file=None, files=None):
133139
cleanup = None
134140
if file is not None:
135141
multipart = {
@@ -159,6 +165,8 @@ def _anon():
159165
files_to_pass = None
160166

161167
url = '%s?wait=%d' % (self._request_url, wait)
168+
if thread_id:
169+
url += f'&thread_id={thread_id}'
162170
maybe_coro = None
163171
try:
164172
maybe_coro = self.request('POST', url, multipart=multipart, payload=data, files=files_to_pass)
@@ -241,7 +249,7 @@ async def request(self, verb, url, payload=None, multipart=None, *, files=None,
241249
# Banned by Cloudflare more than likely.
242250
raise HTTPException(r, data)
243251

244-
retry_after = response['retry_after'] / 1000.0
252+
retry_after = response['retry_after']
245253
log.warning('Webhook ID %s is rate limited. Retrying in %.2f seconds', _id, retry_after)
246254
await asyncio.sleep(retry_after)
247255
continue
@@ -344,7 +352,7 @@ def request(self, verb, url, payload=None, multipart=None, *, files=None, reason
344352
# Banned by Cloudflare more than likely.
345353
raise HTTPException(r, data)
346354

347-
retry_after = response['retry_after'] / 1000.0
355+
retry_after = response['retry_after']
348356
log.warning('Webhook ID %s is rate limited. Retrying in %.2f seconds', _id, retry_after)
349357
time.sleep(retry_after)
350358
continue
@@ -730,7 +738,7 @@ def guild(self):
730738

731739
@property
732740
def channel(self):
733-
"""Optional[:class:`TextChannel`]: The text channel this webhook belongs to.
741+
"""Optional[:class:`TextChannel`, :class:`ForumChannel`]: The text or forum channel this webhook belongs to.
734742
735743
If this is a partial webhook, then this will always return ``None``.
736744
"""
@@ -882,7 +890,7 @@ def edit(self, *, reason=None, **kwargs):
882890

883891
return self._adapter.edit_webhook(reason=reason, **payload)
884892

885-
def send(self, content=None, *, wait=False, username=None, avatar_url=None, tts=False,
893+
def send(self, content=None, *, wait=False, thread_id: int = None, username=None, avatar_url=None, tts=False,
886894
file=None, files=None, embed=None, embeds=None, allowed_mentions=None):
887895
"""|maybecoro|
888896
@@ -986,7 +994,7 @@ def send(self, content=None, *, wait=False, username=None, avatar_url=None, tts=
986994
elif previous_mentions is not None:
987995
payload['allowed_mentions'] = previous_mentions.to_dict()
988996

989-
return self._adapter.execute_webhook(wait=wait, file=file, files=files, payload=payload)
997+
return self._adapter.execute_webhook(wait=wait, thread_id=thread_id, file=file, files=files, payload=payload)
990998

991999
def execute(self, *args, **kwargs):
9921000
"""An alias for :meth:`~.Webhook.send`."""

0 commit comments

Comments
 (0)