Skip to content

Commit

Permalink
feat: add suppress and allowed_mentions parameters (#2138)
Browse files Browse the repository at this point in the history
* add undocmented parameters

* docs and improved logic

* style(pre-commit): auto fixes from pre-commit.com hooks

* Changelog

* style(pre-commit): auto fixes from pre-commit.com hooks

---------

Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
  • Loading branch information
OmLanke and pre-commit-ci[bot] authored Jun 24, 2023
1 parent b101302 commit b076bd3
Show file tree
Hide file tree
Showing 4 changed files with 72 additions and 2 deletions.
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,9 @@ These changes are available on the `master` branch, but have not yet been releas
([#2106](https://github.com/Pycord-Development/pycord/pull/2106))
- Added Annotated forms support for typehinting slash command options.
([#2124](https://github.com/Pycord-Development/pycord/pull/2124))
- Added `suppress` and `allowed_mentions` parameters to `Webhook` and
`InteractionResponse` edit methods.
([#2138](https://github.com/Pycord-Development/pycord/pull/2138))

### Changed

Expand Down
39 changes: 39 additions & 0 deletions discord/interactions.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@
from .enums import InteractionResponseType, InteractionType, try_enum
from .errors import ClientException, InteractionResponded, InvalidArgument
from .file import File
from .flags import MessageFlags
from .member import Member
from .message import Attachment, Message
from .object import Object
Expand Down Expand Up @@ -386,6 +387,7 @@ async def edit_original_response(
view: View | None = MISSING,
allowed_mentions: AllowedMentions | None = None,
delete_after: float | None = None,
suppress: bool = False,
) -> InteractionMessage:
"""|coro|
Expand Down Expand Up @@ -424,6 +426,8 @@ async def edit_original_response(
If provided, the number of seconds to wait in the background
before deleting the message we just edited. If the deletion fails,
then it is silently ignored.
suppress: :class:`bool`
Whether to suppress embeds for the message.
Returns
-------
Expand Down Expand Up @@ -453,6 +457,7 @@ async def edit_original_response(
view=view,
allowed_mentions=allowed_mentions,
previous_allowed_mentions=previous_mentions,
suppress=suppress,
)
adapter = async_context.get()
http = self._state.http
Expand Down Expand Up @@ -936,6 +941,8 @@ async def edit_message(
attachments: list[Attachment] = MISSING,
view: View | None = MISSING,
delete_after: float | None = None,
suppress: bool | None = MISSING,
allowed_mentions: AllowedMentions | None = None,
) -> None:
"""|coro|
Expand Down Expand Up @@ -966,6 +973,15 @@ async def edit_message(
If provided, the number of seconds to wait in the background
before deleting the message we just edited. If the deletion fails,
then it is silently ignored.
suppress: Optional[:class:`bool`]
Whether to suppress embeds for the message.
allowed_mentions: Optional[:class:`~discord.AllowedMentions`]
Controls the mentions being processed in this message. If this is
passed, then the object is merged with :attr:`~discord.Client.allowed_mentions`.
The merging behaviour only overrides attributes that have been explicitly passed
to the object, otherwise it uses the attributes set in :attr:`~discord.Client.allowed_mentions`.
If no object is passed at all then the defaults given by :attr:`~discord.Client.allowed_mentions`
are used instead.
Raises
------
Expand Down Expand Up @@ -1029,6 +1045,23 @@ async def edit_message(
# we keep previous attachments when adding new files
payload["attachments"] = [a.to_dict() for a in msg.attachments]

if suppress is not MISSING:
flags = MessageFlags._from_value(self._parent.message.flags.value)
flags.suppress_embeds = suppress
payload["flags"] = flags.value

if allowed_mentions is None:
payload["allowed_mentions"] = (
state.allowed_mentions and state.allowed_mentions.to_dict()
)

elif state.allowed_mentions is not None:
payload["allowed_mentions"] = state.allowed_mentions.merge(
allowed_mentions
).to_dict()
else:
payload["allowed_mentions"] = allowed_mentions.to_dict()

adapter = async_context.get()
http = parent._state.http
try:
Expand Down Expand Up @@ -1215,6 +1248,7 @@ async def edit(
view: View | None = MISSING,
allowed_mentions: AllowedMentions | None = None,
delete_after: float | None = None,
suppress: bool | None = MISSING,
) -> InteractionMessage:
"""|coro|
Expand Down Expand Up @@ -1247,6 +1281,8 @@ async def edit(
If provided, the number of seconds to wait in the background
before deleting the message we just edited. If the deletion fails,
then it is silently ignored.
suppress: Optional[:class:`bool`]
Whether to suppress embeds for the message.
Returns
-------
Expand All @@ -1266,6 +1302,8 @@ async def edit(
"""
if attachments is MISSING:
attachments = self.attachments or MISSING
if suppress is MISSING:
suppress = self.flags.suppress_embeds
return await self._state._interaction.edit_original_response(
content=content,
embeds=embeds,
Expand All @@ -1276,6 +1314,7 @@ async def edit(
view=view,
allowed_mentions=allowed_mentions,
delete_after=delete_after,
suppress=suppress,
)

async def delete(self, *, delay: float | None = None) -> None:
Expand Down
18 changes: 16 additions & 2 deletions discord/webhook/async_.py
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@
InvalidArgument,
NotFound,
)
from ..flags import MessageFlags
from ..http import Route
from ..message import Attachment, Message
from ..mixins import Hashable
Expand Down Expand Up @@ -622,6 +623,7 @@ def handle_message_parameters(
view: View | None = MISSING,
allowed_mentions: AllowedMentions | None = MISSING,
previous_allowed_mentions: AllowedMentions | None = None,
suppress: bool = False,
) -> ExecuteWebhookParameters:
if files is not MISSING and file is not MISSING:
raise TypeError("Cannot mix file and files keyword arguments.")
Expand All @@ -648,8 +650,9 @@ def handle_message_parameters(
payload["avatar_url"] = str(avatar_url)
if username:
payload["username"] = username
if ephemeral:
payload["flags"] = 64

flags = MessageFlags(suppress_embeds=suppress, ephemeral=ephemeral)
payload["flags"] = flags.value

if allowed_mentions:
if previous_allowed_mentions is not None:
Expand Down Expand Up @@ -827,6 +830,7 @@ async def edit(
attachments: list[Attachment] = MISSING,
view: View | None = MISSING,
allowed_mentions: AllowedMentions | None = None,
suppress: bool | None = MISSING,
) -> WebhookMessage:
"""|coro|
Expand Down Expand Up @@ -868,6 +872,8 @@ async def edit(
the view is removed.
.. versionadded:: 2.0
suppress: Optional[:class:`bool`]
Whether to suppress embeds for the message.
Returns
-------
Expand Down Expand Up @@ -898,6 +904,9 @@ async def edit(
if attachments is MISSING:
attachments = self.attachments or MISSING

if suppress is MISSING:
suppress = self.flags.suppress_embeds

return await self._state._webhook.edit_message(
self.id,
content=content,
Expand All @@ -909,6 +918,7 @@ async def edit(
view=view,
allowed_mentions=allowed_mentions,
thread=thread,
suppress=suppress,
)

async def delete(self, *, delay: float | None = None) -> None:
Expand Down Expand Up @@ -1845,6 +1855,7 @@ async def edit_message(
view: View | None = MISSING,
allowed_mentions: AllowedMentions | None = None,
thread: Snowflake | None = MISSING,
suppress: bool = False,
) -> WebhookMessage:
"""|coro|
Expand Down Expand Up @@ -1892,6 +1903,8 @@ async def edit_message(
.. versionadded:: 2.0
thread: Optional[:class:`~discord.abc.Snowflake`]
The thread that contains the message.
suppress: :class:`bool`
Whether to suppress embeds for the message.
Returns
-------
Expand Down Expand Up @@ -1939,6 +1952,7 @@ async def edit_message(
view=view,
allowed_mentions=allowed_mentions,
previous_allowed_mentions=previous_mentions,
suppress=suppress,
)

thread_id: int | None = None
Expand Down
14 changes: 14 additions & 0 deletions discord/webhook/sync.py
Original file line number Diff line number Diff line change
Expand Up @@ -472,6 +472,7 @@ def edit(
file: File = MISSING,
files: list[File] = MISSING,
allowed_mentions: AllowedMentions | None = None,
suppress: bool | None = MISSING,
) -> SyncWebhookMessage:
"""Edits the message.
Expand All @@ -492,6 +493,8 @@ def edit(
allowed_mentions: :class:`AllowedMentions`
Controls the mentions being processed in this message.
See :meth:`.abc.Messageable.send` for more information.
suppress: Optional[:class:`bool`]
Whether to suppress embeds for the message.
Returns
-------
Expand All @@ -517,6 +520,9 @@ def edit(
elif isinstance(self.channel, Thread):
thread = Object(self.channel.id)

if suppress is MISSING:
suppress = self.flags.suppress_embeds

return self._state._webhook.edit_message(
self.id,
content=content,
Expand All @@ -526,6 +532,7 @@ def edit(
files=files,
allowed_mentions=allowed_mentions,
thread=thread,
suppress=suppress,
)

def delete(self, *, delay: float | None = None) -> None:
Expand Down Expand Up @@ -952,6 +959,7 @@ def send(
thread: Snowflake = MISSING,
thread_name: str | None = None,
wait: Literal[False] = ...,
suppress: bool = MISSING,
) -> None:
...

Expand All @@ -970,6 +978,7 @@ def send(
thread: Snowflake = MISSING,
thread_name: str | None = None,
wait: bool = False,
suppress: bool = False,
) -> SyncWebhookMessage | None:
"""Sends a message using the webhook.
Expand Down Expand Up @@ -1022,6 +1031,8 @@ def send(
The name of the thread to create. Only works for forum channels.
.. versionadded:: 2.0
suppress: :class:`bool`
Whether to suppress embeds for the message.
Returns
-------
Expand Down Expand Up @@ -1070,6 +1081,7 @@ def send(
embeds=embeds,
allowed_mentions=allowed_mentions,
previous_allowed_mentions=previous_mentions,
suppress=suppress,
)
adapter: WebhookAdapter = _get_webhook_adapter()
thread_id: int | None = None
Expand Down Expand Up @@ -1151,6 +1163,7 @@ def edit_message(
files: list[File] = MISSING,
allowed_mentions: AllowedMentions | None = None,
thread: Snowflake | None = MISSING,
suppress: bool = False,
) -> SyncWebhookMessage:
"""Edits a message owned by this webhook.
Expand Down Expand Up @@ -1211,6 +1224,7 @@ def edit_message(
embeds=embeds,
allowed_mentions=allowed_mentions,
previous_allowed_mentions=previous_mentions,
suppress=suppress,
)
adapter: WebhookAdapter = _get_webhook_adapter()

Expand Down

0 comments on commit b076bd3

Please sign in to comment.