Skip to content

Commit

Permalink
feat(webhook): add applied_tags parameter (#1085)
Browse files Browse the repository at this point in the history
  • Loading branch information
shiftinv authored Jan 2, 2024
1 parent f780cf5 commit 6430c2b
Show file tree
Hide file tree
Showing 3 changed files with 66 additions and 15 deletions.
1 change: 1 addition & 0 deletions changelog/1085.feature.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Add ``applied_tags`` parameter to :meth:`Webhook.send`.
38 changes: 30 additions & 8 deletions disnake/webhook/async_.py
Original file line number Diff line number Diff line change
Expand Up @@ -492,7 +492,9 @@ def handle_message_parameters_dict(
allowed_mentions: Optional[AllowedMentions] = MISSING,
previous_allowed_mentions: Optional[AllowedMentions] = None,
stickers: Sequence[Union[GuildSticker, StandardSticker, StickerItem]] = MISSING,
thread_name: Optional[str] = None,
# these parameters are exclusive to webhooks in forum channels
thread_name: str = MISSING,
applied_tags: Sequence[Snowflake] = MISSING,
) -> DictPayloadParameters:
if files is not MISSING and file is not MISSING:
raise TypeError("Cannot mix file and files keyword arguments.")
Expand Down Expand Up @@ -554,8 +556,10 @@ def handle_message_parameters_dict(
if stickers is not MISSING:
payload["sticker_ids"] = [s.id for s in stickers]

if thread_name is not None:
if thread_name:
payload["thread_name"] = thread_name
if applied_tags:
payload["applied_tags"] = [t.id for t in applied_tags]

return DictPayloadParameters(payload=payload, files=files)

Expand All @@ -579,7 +583,9 @@ def handle_message_parameters(
allowed_mentions: Optional[AllowedMentions] = MISSING,
previous_allowed_mentions: Optional[AllowedMentions] = None,
stickers: Sequence[Union[GuildSticker, StandardSticker, StickerItem]] = MISSING,
thread_name: Optional[str] = None,
# these parameters are exclusive to webhooks in forum channels
thread_name: str = MISSING,
applied_tags: Sequence[Snowflake] = MISSING,
) -> PayloadParameters:
params = handle_message_parameters_dict(
content=content,
Expand All @@ -600,6 +606,7 @@ def handle_message_parameters(
previous_allowed_mentions=previous_allowed_mentions,
stickers=stickers,
thread_name=thread_name,
applied_tags=applied_tags,
)

if params.files:
Expand Down Expand Up @@ -1451,6 +1458,7 @@ async def send(
components: Components[MessageUIComponent] = ...,
thread: Snowflake = ...,
thread_name: str = ...,
applied_tags: Sequence[Snowflake] = ...,
wait: Literal[True],
delete_after: float = ...,
) -> WebhookMessage:
Expand All @@ -1476,6 +1484,7 @@ async def send(
components: Components[MessageUIComponent] = ...,
thread: Snowflake = ...,
thread_name: str = ...,
applied_tags: Sequence[Snowflake] = ...,
wait: Literal[False] = ...,
delete_after: float = ...,
) -> None:
Expand All @@ -1499,7 +1508,8 @@ async def send(
view: View = MISSING,
components: Components[MessageUIComponent] = MISSING,
thread: Snowflake = MISSING,
thread_name: Optional[str] = None,
thread_name: str = MISSING,
applied_tags: Sequence[Snowflake] = MISSING,
wait: bool = False,
delete_after: float = MISSING,
) -> Optional[WebhookMessage]:
Expand All @@ -1516,6 +1526,10 @@ async def send(
it must be a rich embed type. You cannot mix the ``embed`` parameter with the
``embeds`` parameter, which must be a :class:`list` of :class:`Embed` objects to send.
To send a message in a thread, provide the ``thread`` parameter.
If this webhook is in a :class:`ForumChannel`, the ``thread_name`` parameter can
be used to create a new thread instead (optionally with ``applied_tags``).
.. versionchanged:: 2.6
Raises :exc:`WebhookTokenMissing` instead of ``InvalidArgument``.
Expand Down Expand Up @@ -1587,6 +1601,11 @@ async def send(
representing the created thread, may be a :class:`PartialMessageable`.
.. versionadded:: 2.6
applied_tags: Sequence[:class:`abc.Snowflake`]
If in a forum channel and creating a new thread (see ``thread_name`` above),
the tags to apply to the new thread. Maximum of 5.
.. versionadded:: 2.10
wait: :class:`bool`
Whether the server should wait before sending a response. This essentially
Expand Down Expand Up @@ -1632,7 +1651,7 @@ async def send(
You specified both ``embed`` and ``embeds`` or ``file`` and ``files``.
``ephemeral`` was passed with the improper webhook type.
There was no state attached with this webhook when giving it a view.
Both ``thread`` and ``thread_name`` were provided.
Both ``thread`` and ``thread_name``/``applied_tags`` were provided.
WebhookTokenMissing
There was no token associated with this webhook.
ValueError
Expand Down Expand Up @@ -1666,9 +1685,11 @@ async def send(
view.timeout = 15 * 60.0

thread_id: Optional[int] = None
if thread is not MISSING and thread_name is not None:
raise TypeError("only one of thread and thread_name can be provided.")
elif thread is not MISSING:
if thread is not MISSING:
if thread_name or applied_tags:
raise TypeError(
"Cannot use `thread_name` or `applied_tags` when `thread` is provided."
)
thread_id = thread.id

params = handle_message_parameters(
Expand All @@ -1686,6 +1707,7 @@ async def send(
view=view,
components=components,
thread_name=thread_name,
applied_tags=applied_tags,
allowed_mentions=allowed_mentions,
previous_allowed_mentions=previous_mentions,
)
Expand Down
42 changes: 35 additions & 7 deletions disnake/webhook/sync.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,19 @@
import threading
import time
from errno import ECONNRESET
from typing import TYPE_CHECKING, Any, Dict, List, Literal, Optional, Tuple, Type, Union, overload
from typing import (
TYPE_CHECKING,
Any,
Dict,
List,
Literal,
Optional,
Sequence,
Tuple,
Type,
Union,
overload,
)
from urllib.parse import quote as urlquote

from .. import utils
Expand Down Expand Up @@ -872,6 +884,7 @@ def send(
allowed_mentions: AllowedMentions = ...,
thread: Snowflake = ...,
thread_name: str = ...,
applied_tags: Sequence[Snowflake] = ...,
wait: Literal[True],
) -> SyncWebhookMessage:
...
Expand All @@ -893,6 +906,7 @@ def send(
allowed_mentions: AllowedMentions = ...,
thread: Snowflake = ...,
thread_name: str = ...,
applied_tags: Sequence[Snowflake] = ...,
wait: Literal[False] = ...,
) -> None:
...
Expand All @@ -912,7 +926,8 @@ def send(
flags: MessageFlags = MISSING,
allowed_mentions: AllowedMentions = MISSING,
thread: Snowflake = MISSING,
thread_name: Optional[str] = None,
thread_name: str = MISSING,
applied_tags: Sequence[Snowflake] = MISSING,
wait: bool = False,
) -> Optional[SyncWebhookMessage]:
"""Sends a message using the webhook.
Expand All @@ -926,6 +941,10 @@ def send(
it must be a rich embed type. You cannot mix the ``embed`` parameter with the
``embeds`` parameter, which must be a :class:`list` of :class:`Embed` objects to send.
To send a message in a thread, provide the ``thread`` parameter.
If this webhook is in a :class:`ForumChannel`, the ``thread_name`` parameter can
be used to create a new thread instead (optionally with ``applied_tags``).
.. versionchanged:: 2.6
Raises :exc:`WebhookTokenMissing` instead of ``InvalidArgument``.
Expand Down Expand Up @@ -963,11 +982,17 @@ def send(
.. versionadded:: 2.0
thread_name: :class:`str`
If in a forum channel, and thread is not specified,
If in a forum channel, and ``thread`` is not specified,
the name of the newly created thread.
.. versionadded:: 2.6
applied_tags: Sequence[:class:`abc.Snowflake`]
If in a forum channel and creating a new thread (see ``thread_name`` above),
the tags to apply to the new thread. Maximum of 5.
.. versionadded:: 2.10
suppress_embeds: :class:`bool`
Whether to suppress embeds for the message. This hides
all the embeds from the UI if set to ``True``.
Expand Down Expand Up @@ -999,7 +1024,7 @@ def send(
The authorization token for the webhook is incorrect.
TypeError
You specified both ``embed`` and ``embeds`` or ``file`` and ``files``,
or both ``thread`` and ``thread_name`` were provided.
or both ``thread`` and ``thread_name``/``applied_tags`` were provided.
ValueError
The length of ``embeds`` was invalid
Expand All @@ -1021,9 +1046,11 @@ def send(
content = MISSING

thread_id: Optional[int] = None
if thread is not MISSING and thread_name is not None:
raise TypeError("only one of thread and thread_name can be provided.")
elif thread is not MISSING:
if thread is not MISSING:
if thread_name or applied_tags:
raise TypeError(
"Cannot use `thread_name` or `applied_tags` when `thread` is provided."
)
thread_id = thread.id

params = handle_message_parameters(
Expand All @@ -1038,6 +1065,7 @@ def send(
embed=embed,
embeds=embeds,
thread_name=thread_name,
applied_tags=applied_tags,
allowed_mentions=allowed_mentions,
previous_allowed_mentions=previous_mentions,
)
Expand Down

0 comments on commit 6430c2b

Please sign in to comment.