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

Add file and files parameters to InteractionResponse.edit_message() #1340

Merged
merged 5 commits into from
May 13, 2022
Merged
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
52 changes: 44 additions & 8 deletions discord/interactions.py
Original file line number Diff line number Diff line change
Expand Up @@ -677,6 +677,8 @@ async def edit_message(
content: Optional[Any] = MISSING,
embed: Optional[Embed] = MISSING,
embeds: List[Embed] = MISSING,
file: File = MISSING,
files: List[File] = MISSING,
attachments: List[Attachment] = MISSING,
view: Optional[View] = MISSING,
delete_after: Optional[float] = None,
Expand All @@ -695,6 +697,11 @@ async def edit_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`
A new file to add to the message. This cannot be mixed with ``files`` parameter.
files: List[:class:`File`]
A list of new files to add to the message. Must be a maximum of 10. This
cannot be mixed with the ``file`` parameter.
attachments: List[:class:`Attachment`]
A list of attachments to keep in the message. If ``[]`` is passed
then all attachments are removed.
Expand Down Expand Up @@ -742,16 +749,45 @@ async def edit_message(
if view is not MISSING:
state.prevent_view_updates_for(message_id)
payload["components"] = [] if view is None else view.to_components()

if file is not MISSING and files is not MISSING:
raise InvalidArgument("cannot pass both file and files parameter to edit_message()")

if file is not MISSING:
if not isinstance(file, File):
raise InvalidArgument("file parameter must be a File")
else:
files = [file]
if "attachments" not in payload:
# we keep previous attachments when adding a new file
payload["attachments"] = [a.to_dict() for a in msg.attachments]

if files is not MISSING:
if len(files) > 10:
raise InvalidArgument("files parameter must be a list of up to 10 elements")
elif not all(isinstance(file, File) for file in files):
raise InvalidArgument("files parameter must be a list of File")
else:
if "attachments" not in payload:
# we keep previous attachments when adding new files
payload["attachments"] = [a.to_dict() for a in msg.attachments]
krittick marked this conversation as resolved.
Show resolved Hide resolved

adapter = async_context.get()
await self._locked_response(
adapter.create_interaction_response(
parent.id,
parent.token,
session=parent._session,
type=InteractionResponseType.message_update.value,
data=payload,
try:
await self._locked_response(
adapter.create_interaction_response(
parent.id,
parent.token,
session=parent._session,
type=InteractionResponseType.message_update.value,
data=payload,
files=files,
)
)
)
finally:
if files:
for file in files:
file.close()

if view and not view.is_finished():
state.store_view(view, message_id)
Expand Down