Add support for thread parameter in Webhook.send

This commit is contained in:
Rapptz 2021-05-05 21:15:24 -04:00
parent 4b51e3e253
commit 92ee2cd598
2 changed files with 32 additions and 0 deletions

View File

@ -253,9 +253,12 @@ class AsyncWebhookAdapter:
payload: Optional[Dict[str, Any]] = None, payload: Optional[Dict[str, Any]] = None,
multipart: Optional[List[Dict[str, Any]]] = None, multipart: Optional[List[Dict[str, Any]]] = None,
files: Optional[List[File]] = None, files: Optional[List[File]] = None,
thread_id: Optional[int] = None,
wait: bool = False, wait: bool = False,
): ):
params = {'wait': int(wait)} params = {'wait': int(wait)}
if thread_id:
params['thread_id'] = thread_id
route = Route('POST', '/webhooks/{webhook_id}/{webhook_token}', webhook_id=webhook_id, webhook_token=token) route = Route('POST', '/webhooks/{webhook_id}/{webhook_token}', webhook_id=webhook_id, webhook_token=token)
return self.request(route, session, payload=payload, multipart=multipart, files=files, params=params) return self.request(route, session, payload=payload, multipart=multipart, files=files, params=params)
@ -1162,6 +1165,7 @@ class Webhook(BaseWebhook):
embeds: List[Embed] = MISSING, embeds: List[Embed] = MISSING,
allowed_mentions: AllowedMentions = MISSING, allowed_mentions: AllowedMentions = MISSING,
view: View = MISSING, view: View = MISSING,
thread: Snowflake = MISSING,
wait: Literal[True], wait: Literal[True],
) -> WebhookMessage: ) -> WebhookMessage:
... ...
@ -1181,6 +1185,7 @@ class Webhook(BaseWebhook):
embeds: List[Embed] = MISSING, embeds: List[Embed] = MISSING,
allowed_mentions: AllowedMentions = MISSING, allowed_mentions: AllowedMentions = MISSING,
view: View = MISSING, view: View = MISSING,
thread: Snowflake = MISSING,
wait: Literal[False] = ..., wait: Literal[False] = ...,
) -> None: ) -> None:
... ...
@ -1199,6 +1204,7 @@ class Webhook(BaseWebhook):
embeds: List[Embed] = MISSING, embeds: List[Embed] = MISSING,
allowed_mentions: AllowedMentions = MISSING, allowed_mentions: AllowedMentions = MISSING,
view: View = MISSING, view: View = MISSING,
thread: Snowflake = MISSING,
wait: bool = False, wait: bool = False,
) -> Optional[WebhookMessage]: ) -> Optional[WebhookMessage]:
"""|coro| """|coro|
@ -1249,12 +1255,20 @@ class Webhook(BaseWebhook):
be mixed with the ``embed`` parameter. be mixed with the ``embed`` parameter.
allowed_mentions: :class:`AllowedMentions` allowed_mentions: :class:`AllowedMentions`
Controls the mentions being processed in this message. Controls the mentions being processed in this message.
.. versionadded:: 1.4
view: :class:`discord.ui.View` view: :class:`discord.ui.View`
The view to send with the message. You can only send a view The view to send with the message. You can only send a view
if this webhook is not partial and has state attached. A if this webhook is not partial and has state attached. A
webhook has state attached if the webhook is managed by the webhook has state attached if the webhook is managed by the
library. library.
.. versionadded:: 2.0
thread: :class:`~discord.abc.Snowflake`
The thread to send this webhook to.
.. versionadded:: 2.0
Raises Raises
-------- --------
HTTPException HTTPException
@ -1313,6 +1327,10 @@ class Webhook(BaseWebhook):
previous_allowed_mentions=previous_mentions, previous_allowed_mentions=previous_mentions,
) )
adapter = async_context.get() adapter = async_context.get()
thread_id: Optional[int] = None
if thread is not MISSING:
thread_id = thread.id
data = await adapter.execute_webhook( data = await adapter.execute_webhook(
self.id, self.id,
self.token, self.token,
@ -1320,6 +1338,7 @@ class Webhook(BaseWebhook):
payload=params.payload, payload=params.payload,
multipart=params.multipart, multipart=params.multipart,
files=params.files, files=params.files,
thread_id=thread_id,
wait=wait, wait=wait,
) )

View File

@ -262,9 +262,12 @@ class WebhookAdapter:
payload: Optional[Dict[str, Any]] = None, payload: Optional[Dict[str, Any]] = None,
multipart: Optional[List[Dict[str, Any]]] = None, multipart: Optional[List[Dict[str, Any]]] = None,
files: Optional[List[File]] = None, files: Optional[List[File]] = None,
thread_id: Optional[int] = None,
wait: bool = False, wait: bool = False,
): ):
params = {'wait': int(wait)} params = {'wait': int(wait)}
if thread_id:
params['thread_id'] = thread_id
route = Route('POST', '/webhooks/{webhook_id}/{webhook_token}', webhook_id=webhook_id, webhook_token=token) route = Route('POST', '/webhooks/{webhook_id}/{webhook_token}', webhook_id=webhook_id, webhook_token=token)
return self.request(route, session, payload=payload, multipart=multipart, files=files, params=params) return self.request(route, session, payload=payload, multipart=multipart, files=files, params=params)
@ -780,6 +783,7 @@ class SyncWebhook(BaseWebhook):
embed: Embed = MISSING, embed: Embed = MISSING,
embeds: List[Embed] = MISSING, embeds: List[Embed] = MISSING,
allowed_mentions: AllowedMentions = MISSING, allowed_mentions: AllowedMentions = MISSING,
thread: Snowflake = MISSING,
wait: bool = False, wait: bool = False,
) -> Optional[SyncWebhookMessage]: ) -> Optional[SyncWebhookMessage]:
"""Sends a message using the webhook. """Sends a message using the webhook.
@ -824,6 +828,10 @@ class SyncWebhook(BaseWebhook):
Controls the mentions being processed in this message. Controls the mentions being processed in this message.
.. versionadded:: 1.4 .. versionadded:: 1.4
thread: :class:`~discord.abc.Snowflake`
The thread to send this message to.
.. versionadded:: 2.0
Raises Raises
-------- --------
@ -866,6 +874,10 @@ class SyncWebhook(BaseWebhook):
previous_allowed_mentions=previous_mentions, previous_allowed_mentions=previous_mentions,
) )
adapter: WebhookAdapter = _context.adapter adapter: WebhookAdapter = _context.adapter
thread_id: Optional[int] = None
if thread is not MISSING:
thread_id = thread.id
data = adapter.execute_webhook( data = adapter.execute_webhook(
self.id, self.id,
self.token, self.token,
@ -873,6 +885,7 @@ class SyncWebhook(BaseWebhook):
payload=params.payload, payload=params.payload,
multipart=params.multipart, multipart=params.multipart,
files=params.files, files=params.files,
thread_id=thread_id,
wait=wait, wait=wait,
) )
if wait: if wait: