Skip to content

Commit

Permalink
Webhooks creating forum posts (Pycord-Development#1405)
Browse files Browse the repository at this point in the history
* Implement creating forum posts with webhooks

* Rename `name` to `thread_name`

* Update discord/webhook/async_.py

Co-authored-by: Dorukyum <53639936+Dorukyum@users.noreply.github.com>

* Update discord/webhook/sync.py

Co-authored-by: Dorukyum <53639936+Dorukyum@users.noreply.github.com>
  • Loading branch information
Middledot and Dorukyum authored Jun 25, 2022
1 parent 032f37d commit 3c2fb0f
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 7 deletions.
22 changes: 19 additions & 3 deletions discord/webhook/async_.py
Original file line number Diff line number Diff line change
Expand Up @@ -291,11 +291,16 @@ def execute_webhook(
multipart: Optional[List[Dict[str, Any]]] = None,
files: Optional[List[File]] = None,
thread_id: Optional[int] = None,
thread_name: Optional[str] = None,
wait: bool = False,
) -> Response[Optional[MessagePayload]]:
params = {"wait": int(wait)}
if thread_id:
params["thread_id"] = thread_id

if thread_name:
payload["thread_name"] = thread_name

route = Route(
"POST",
"/webhooks/{webhook_id}/{webhook_token}",
Expand Down Expand Up @@ -1371,6 +1376,7 @@ async def send(
allowed_mentions: AllowedMentions = MISSING,
view: View = MISSING,
thread: Snowflake = MISSING,
thread_name: Optional[str] = None,
wait: Literal[True],
) -> WebhookMessage:
...
Expand All @@ -1391,6 +1397,7 @@ async def send(
allowed_mentions: AllowedMentions = MISSING,
view: View = MISSING,
thread: Snowflake = MISSING,
thread_name: Optional[str] = None,
wait: Literal[False] = ...,
) -> None:
...
Expand All @@ -1410,6 +1417,7 @@ async def send(
allowed_mentions: AllowedMentions = MISSING,
view: View = MISSING,
thread: Snowflake = MISSING,
thread_name: Optional[str] = None,
wait: bool = False,
delete_after: float = None,
) -> Optional[WebhookMessage]:
Expand Down Expand Up @@ -1476,6 +1484,10 @@ async def send(
thread: :class:`~discord.abc.Snowflake`
The thread to send this webhook to.
.. versionadded:: 2.0
thread_name: :class:`str`
The name of the thread to create. Only works for forum channels.
.. versionadded:: 2.0
delete_after: :class:`float`
If provided, the number of seconds to wait in the background
Expand All @@ -1494,9 +1506,9 @@ async def send(
ValueError
The length of ``embeds`` was invalid.
InvalidArgument
There was no token associated with this webhook or ``ephemeral``
was passed with the improper webhook type or there was no state
attached with this webhook when giving it a view.
Either there was no token associated with this webhook, ``ephemeral`` was passed
with the improper webhook type, there was no state attached with this webhook when
giving it a view, or you specified both ``thread_name`` and ``thread``.
Returns
---------
Expand All @@ -1511,6 +1523,9 @@ async def send(
if content is None:
content = MISSING

if thread and thread_name:
raise InvalidArgument("You cannot specify both a thread and thread_name")

application_webhook = self.type is WebhookType.application
if ephemeral and not application_webhook:
raise InvalidArgument("ephemeral messages can only be sent from application webhooks")
Expand Down Expand Up @@ -1551,6 +1566,7 @@ async def send(
multipart=params.multipart,
files=params.files,
thread_id=thread_id,
thread_name=thread_name,
wait=wait,
)

Expand Down
25 changes: 21 additions & 4 deletions discord/webhook/sync.py
Original file line number Diff line number Diff line change
Expand Up @@ -308,11 +308,16 @@ def execute_webhook(
multipart: Optional[List[Dict[str, Any]]] = None,
files: Optional[List[File]] = None,
thread_id: Optional[int] = None,
thread_name: Optional[str] = None,
wait: bool = False,
):
params = {"wait": int(wait)}
if thread_id:
params["thread_id"] = thread_id

if thread_name:
payload["thread_name"] = thread_name

route = Route(
"POST",
"/webhooks/{webhook_id}/{webhook_token}",
Expand Down Expand Up @@ -909,6 +914,7 @@ def send(
embeds: List[Embed] = MISSING,
allowed_mentions: AllowedMentions = MISSING,
thread: Snowflake = MISSING,
thread_name: Optional[str] = None,
wait: Literal[True],
) -> SyncWebhookMessage:
...
Expand All @@ -927,6 +933,7 @@ def send(
embeds: List[Embed] = MISSING,
allowed_mentions: AllowedMentions = MISSING,
thread: Snowflake = MISSING,
thread_name: Optional[str] = None,
wait: Literal[False] = ...,
) -> None:
...
Expand All @@ -944,6 +951,7 @@ def send(
embeds: List[Embed] = MISSING,
allowed_mentions: AllowedMentions = MISSING,
thread: Snowflake = MISSING,
thread_name: Optional[str] = None,
wait: bool = False,
) -> Optional[SyncWebhookMessage]:
"""Sends a message using the webhook.
Expand All @@ -958,7 +966,7 @@ def send(
``embeds`` parameter, which must be a :class:`list` of :class:`Embed` objects to send.
Parameters
------------
----------
content: :class:`str`
The content of the message to send.
wait: :class:`bool`
Expand Down Expand Up @@ -992,10 +1000,14 @@ def send(
thread: :class:`~discord.abc.Snowflake`
The thread to send this message to.
.. versionadded:: 2.0
thread_name: :class:`str`
The name of the thread to create. Only works for forum channels.
.. versionadded:: 2.0
Raises
--------
------
HTTPException
Sending the message failed.
NotFound
Expand All @@ -1007,10 +1019,11 @@ def send(
ValueError
The length of ``embeds`` was invalid
InvalidArgument
There was no token associated with this webhook.
There was no token associated with this webhook or you specified both
a thread to send to and a thread to create (the ``thread`` and ``thread_name`` parameters).
Returns
---------
-------
Optional[:class:`SyncWebhookMessage`]
If ``wait`` is ``True`` then the message that was sent, otherwise ``None``.
"""
Expand All @@ -1022,6 +1035,9 @@ def send(
if content is None:
content = MISSING

if thread and thread_name:
raise InvalidArgument("You cannot specify both a thread and a thread name")

params = handle_message_parameters(
content=content,
username=username,
Expand All @@ -1047,6 +1063,7 @@ def send(
multipart=params.multipart,
files=params.files,
thread_id=thread_id,
thread_name=thread_name,
wait=wait,
)
if wait:
Expand Down

0 comments on commit 3c2fb0f

Please sign in to comment.