Add support for editing message attachments

This commit is contained in:
Josh
2022-02-20 13:02:53 +10:00
committed by GitHub
parent 588cda0996
commit dede5539ee
6 changed files with 347 additions and 123 deletions

View File

@ -59,6 +59,7 @@ if TYPE_CHECKING:
from ..file import File
from ..embeds import Embed
from ..mentions import AllowedMentions
from ..message import Attachment
from ..types.webhook import (
Webhook as WebhookPayload,
)
@ -381,8 +382,7 @@ class SyncWebhookMessage(Message):
content: Optional[str] = MISSING,
embeds: List[Embed] = MISSING,
embed: Optional[Embed] = MISSING,
file: File = MISSING,
files: List[File] = MISSING,
attachments: List[Union[Attachment, File]] = MISSING,
allowed_mentions: Optional[AllowedMentions] = None,
) -> SyncWebhookMessage:
"""Edits the message.
@ -396,11 +396,15 @@ class SyncWebhookMessage(Message):
embed: Optional[:class:`Embed`]
The embed to edit the message with. ``None`` suppresses the embeds.
This should not be mixed with the ``embeds`` parameter.
file: :class:`File`
The file to upload. This cannot be mixed with ``files`` parameter.
files: List[:class:`File`]
A list of files to send with the content. This cannot be mixed with the
``file`` parameter.
attachments: List[Union[:class:`Attachment`, :class:`File`]]
A list of attachments to keep in the message as well as new files to upload. If ``[]`` is passed
then all attachments are removed.
.. note::
New files will always appear after current attachments.
.. versionadded:: 2.0
allowed_mentions: :class:`AllowedMentions`
Controls the mentions being processed in this message.
See :meth:`.abc.Messageable.send` for more information.
@ -412,7 +416,7 @@ class SyncWebhookMessage(Message):
Forbidden
Edited a message that is not yours.
TypeError
You specified both ``embed`` and ``embeds`` or ``file`` and ``files``
You specified both ``embed`` and ``embeds``
ValueError
The length of ``embeds`` was invalid
InvalidArgument
@ -428,11 +432,58 @@ class SyncWebhookMessage(Message):
content=content,
embeds=embeds,
embed=embed,
file=file,
files=files,
attachments=attachments,
allowed_mentions=allowed_mentions,
)
def add_files(self, *files: File) -> SyncWebhookMessage:
r"""Adds new files to the end of the message attachments.
.. versionadded:: 2.0
Parameters
-----------
\*files: :class:`File`
New files to add to the message.
Raises
-------
HTTPException
Editing the message failed.
Forbidden
Tried to edit a message that isn't yours.
Returns
--------
:class:`SyncWebhookMessage`
The newly edited message.
"""
return self.edit(attachments=[*self.attachments, *files])
def remove_attachments(self, *attachments: Attachment) -> SyncWebhookMessage:
r"""Removes attachments from the message.
.. versionadded:: 2.0
Parameters
-----------
\*attachments: :class:`Attachment`
Attachments to remove from the message.
Raises
-------
HTTPException
Editing the message failed.
Forbidden
Tried to edit a message that isn't yours.
Returns
--------
:class:`SyncWebhookMessage`
The newly edited message.
"""
return self.edit(attachments=[a for a in self.attachments if a not in attachments])
def delete(self, *, delay: Optional[float] = None) -> None:
"""Deletes the message.
@ -966,8 +1017,7 @@ class SyncWebhook(BaseWebhook):
content: Optional[str] = MISSING,
embeds: List[Embed] = MISSING,
embed: Optional[Embed] = MISSING,
file: File = MISSING,
files: List[File] = MISSING,
attachments: List[Union[Attachment, File]] = MISSING,
allowed_mentions: Optional[AllowedMentions] = None,
) -> SyncWebhookMessage:
"""Edits a message owned by this webhook.
@ -988,11 +1038,11 @@ class SyncWebhook(BaseWebhook):
embed: Optional[:class:`Embed`]
The embed to edit the message with. ``None`` suppresses the embeds.
This should not be mixed with the ``embeds`` parameter.
file: :class:`File`
The file to upload. This cannot be mixed with ``files`` parameter.
files: List[:class:`File`]
A list of files to send with the content. This cannot be mixed with the
``file`` parameter.
attachments: List[Union[:class:`Attachment`, :class:`File`]]
A list of attachments to keep in the message as well as new files to upload. If ``[]`` is passed
then all attachments are removed.
.. versionadded:: 2.0
allowed_mentions: :class:`AllowedMentions`
Controls the mentions being processed in this message.
See :meth:`.abc.Messageable.send` for more information.
@ -1004,7 +1054,7 @@ class SyncWebhook(BaseWebhook):
Forbidden
Edited a message that is not yours.
TypeError
You specified both ``embed`` and ``embeds`` or ``file`` and ``files``
You specified both ``embed`` and ``embeds``
ValueError
The length of ``embeds`` was invalid
InvalidArgument
@ -1017,8 +1067,7 @@ class SyncWebhook(BaseWebhook):
previous_mentions: Optional[AllowedMentions] = getattr(self._state, 'allowed_mentions', None)
params = handle_message_parameters(
content=content,
file=file,
files=files,
attachments=attachments,
embed=embed,
embeds=embeds,
allowed_mentions=allowed_mentions,