Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add suppress and allowed_mentions parameters #2138

Merged
merged 7 commits into from
Jun 24, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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 @@
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 @@
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 @@
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 @@
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 @@
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 @@
# 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

Check warning on line 1051 in discord/interactions.py

View check run for this annotation

Codecov / codecov/patch

discord/interactions.py#L1048-L1051

Added lines #L1048 - L1051 were not covered by tests

if allowed_mentions is None:
payload["allowed_mentions"] = (

Check warning on line 1054 in discord/interactions.py

View check run for this annotation

Codecov / codecov/patch

discord/interactions.py#L1053-L1054

Added lines #L1053 - L1054 were not covered by tests
state.allowed_mentions and state.allowed_mentions.to_dict()
)

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

Check warning on line 1059 in discord/interactions.py

View check run for this annotation

Codecov / codecov/patch

discord/interactions.py#L1058-L1059

Added lines #L1058 - L1059 were not covered by tests
allowed_mentions
).to_dict()
else:
payload["allowed_mentions"] = allowed_mentions.to_dict()

Check warning on line 1063 in discord/interactions.py

View check run for this annotation

Codecov / codecov/patch

discord/interactions.py#L1063

Added line #L1063 was not covered by tests

adapter = async_context.get()
http = parent._state.http
try:
Expand Down Expand Up @@ -1215,6 +1248,7 @@
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 @@
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 @@
"""
if attachments is MISSING:
attachments = self.attachments or MISSING
if suppress is MISSING:
suppress = self.flags.suppress_embeds

Check warning on line 1306 in discord/interactions.py

View check run for this annotation

Codecov / codecov/patch

discord/interactions.py#L1305-L1306

Added lines #L1305 - L1306 were not covered by tests
return await self._state._interaction.edit_original_response(
content=content,
embeds=embeds,
Expand All @@ -1276,6 +1314,7 @@
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 @@
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 @@
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

Check warning on line 655 in discord/webhook/async_.py

View check run for this annotation

Codecov / codecov/patch

discord/webhook/async_.py#L654-L655

Added lines #L654 - L655 were not covered by tests

if allowed_mentions:
if previous_allowed_mentions is not None:
Expand Down Expand Up @@ -827,6 +830,7 @@
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 @@
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 @@
if attachments is MISSING:
attachments = self.attachments or MISSING

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

Check warning on line 908 in discord/webhook/async_.py

View check run for this annotation

Codecov / codecov/patch

discord/webhook/async_.py#L907-L908

Added lines #L907 - L908 were not covered by tests

return await self._state._webhook.edit_message(
self.id,
content=content,
Expand All @@ -909,6 +918,7 @@
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 @@
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 @@
.. 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 @@
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 @@
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 @@
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 @@
elif isinstance(self.channel, Thread):
thread = Object(self.channel.id)

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

Check warning on line 524 in discord/webhook/sync.py

View check run for this annotation

Codecov / codecov/patch

discord/webhook/sync.py#L523-L524

Added lines #L523 - L524 were not covered by tests

return self._state._webhook.edit_message(
self.id,
content=content,
Expand All @@ -526,6 +532,7 @@
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 @@
thread: Snowflake = MISSING,
thread_name: str | None = None,
wait: Literal[False] = ...,
suppress: bool = MISSING,
) -> None:
...

Expand All @@ -970,6 +978,7 @@
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 @@
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 @@
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 @@
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 @@
embeds=embeds,
allowed_mentions=allowed_mentions,
previous_allowed_mentions=previous_mentions,
suppress=suppress,
)
adapter: WebhookAdapter = _get_webhook_adapter()

Expand Down