Skip to content

Commit 702daef

Browse files
authored
Add file and files parameters to InteractionResponse.edit_message() (#1340)
1 parent 8df222d commit 702daef

File tree

1 file changed

+43
-8
lines changed

1 file changed

+43
-8
lines changed

discord/interactions.py

Lines changed: 43 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -677,6 +677,8 @@ async def edit_message(
677677
content: Optional[Any] = MISSING,
678678
embed: Optional[Embed] = MISSING,
679679
embeds: List[Embed] = MISSING,
680+
file: File = MISSING,
681+
files: List[File] = MISSING,
680682
attachments: List[Attachment] = MISSING,
681683
view: Optional[View] = MISSING,
682684
delete_after: Optional[float] = None,
@@ -695,6 +697,11 @@ async def edit_message(
695697
embed: Optional[:class:`Embed`]
696698
The embed to edit the message with. ``None`` suppresses the embeds.
697699
This should not be mixed with the ``embeds`` parameter.
700+
file: :class:`File`
701+
A new file to add to the message. This cannot be mixed with ``files`` parameter.
702+
files: List[:class:`File`]
703+
A list of new files to add to the message. Must be a maximum of 10. This
704+
cannot be mixed with the ``file`` parameter.
698705
attachments: List[:class:`Attachment`]
699706
A list of attachments to keep in the message. If ``[]`` is passed
700707
then all attachments are removed.
@@ -742,16 +749,44 @@ async def edit_message(
742749
if view is not MISSING:
743750
state.prevent_view_updates_for(message_id)
744751
payload["components"] = [] if view is None else view.to_components()
752+
753+
if file is not MISSING and files is not MISSING:
754+
raise InvalidArgument("cannot pass both file and files parameter to edit_message()")
755+
756+
if file is not MISSING:
757+
if not isinstance(file, File):
758+
raise InvalidArgument("file parameter must be a File")
759+
else:
760+
files = [file]
761+
if "attachments" not in payload:
762+
# we keep previous attachments when adding a new file
763+
payload["attachments"] = [a.to_dict() for a in msg.attachments]
764+
765+
if files is not MISSING:
766+
if len(files) > 10:
767+
raise InvalidArgument("files parameter must be a list of up to 10 elements")
768+
elif not all(isinstance(file, File) for file in files):
769+
raise InvalidArgument("files parameter must be a list of File")
770+
if "attachments" not in payload:
771+
# we keep previous attachments when adding new files
772+
payload["attachments"] = [a.to_dict() for a in msg.attachments]
773+
745774
adapter = async_context.get()
746-
await self._locked_response(
747-
adapter.create_interaction_response(
748-
parent.id,
749-
parent.token,
750-
session=parent._session,
751-
type=InteractionResponseType.message_update.value,
752-
data=payload,
775+
try:
776+
await self._locked_response(
777+
adapter.create_interaction_response(
778+
parent.id,
779+
parent.token,
780+
session=parent._session,
781+
type=InteractionResponseType.message_update.value,
782+
data=payload,
783+
files=files,
784+
)
753785
)
754-
)
786+
finally:
787+
if files:
788+
for file in files:
789+
file.close()
755790

756791
if view and not view.is_finished():
757792
state.store_view(view, message_id)

0 commit comments

Comments
 (0)