From 275a754abd781b5cc13e548b2389c66058ca8d12 Mon Sep 17 00:00:00 2001 From: Rapptz Date: Wed, 21 Apr 2021 23:45:06 -0400 Subject: [PATCH] Add support for editing message attachments --- discord/message.py | 27 +++++++++++++++++++++++++++ 1 file changed, 27 insertions(+) diff --git a/discord/message.py b/discord/message.py index d30eabe1d..288475a41 100644 --- a/discord/message.py +++ b/discord/message.py @@ -258,6 +258,23 @@ class Attachment(Hashable): data = await self.read(use_cached=use_cached) return File(io.BytesIO(data), filename=self.filename, spoiler=spoiler) + def to_dict(self): + result = { + 'filename': self.filename, + 'id': self.id, + 'proxy_url': self.proxy_url, + 'size': self.size, + 'url': self.url, + 'spoiler': self.is_spoiler(), + } + if self.height: + result['height'] = self.height + if self.width: + result['width'] = self.width + if self.content_type: + result['content_type'] = self.content_type + return result + class DeletedReferencedMessage: """A special sentinel type that denotes whether the resolved message referenced message had since been deleted. @@ -1000,6 +1017,9 @@ class Message(Hashable): embed: Optional[:class:`Embed`] The new embed to replace the original with. Could be ``None`` to remove the embed. + attachments: List[:class:`Attachment`] + A list of attachments to keep in the message. If ``[]`` is passed + then all attachments are removed. suppress: :class:`bool` Whether to suppress embeds for the message. This removes all the embeds if set to ``True``. If set to ``False`` @@ -1068,6 +1088,13 @@ class Message(Hashable): allowed_mentions = allowed_mentions.to_dict() fields['allowed_mentions'] = allowed_mentions + try: + attachments = fields.pop('attachments') + except KeyError: + pass + else: + fields['attachments'] = [a.to_dict() for a in attachments] + if fields: data = await self._state.http.edit_message(self.channel.id, self.id, **fields) self._update(data)