Skip to content

Commit d78a85d

Browse files
authored
Added file editing functions
Added edit_files and edit_multipart_helper functions
1 parent 5bee7e3 commit d78a85d

File tree

1 file changed

+83
-0
lines changed

1 file changed

+83
-0
lines changed

discord/http.py

Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,7 @@
8787
sticker,
8888
)
8989
from .types.snowflake import Snowflake, SnowflakeList
90+
from .types.message import Attachment
9091

9192
from types import TracebackType
9293

@@ -548,6 +549,88 @@ def send_files(
548549
stickers=stickers,
549550
components=components,
550551
)
552+
553+
def edit_multipart_helper(
554+
self,
555+
route: Route,
556+
*,
557+
files: Sequence[File],
558+
attachments: List[Attachment] = None,
559+
suppress: bool = False,
560+
content: Optional[str] = None,
561+
embed: Optional[embed.Embed] = None,
562+
embeds: Optional[Iterable[Optional[embed.Embed]]] = None,
563+
allowed_mentions: Optional[message.AllowedMentions] = None,
564+
components: Optional[List[components.Component]] = None,
565+
) -> Response[message.Message]:
566+
form = []
567+
568+
payload: Dict[str, Any] = {}
569+
if attachments:
570+
payload['attachments'] = attachments
571+
if suppress:
572+
payload['suppress'] = suppress
573+
if content:
574+
payload['content'] = content
575+
if embed:
576+
payload['embeds'] = [embed]
577+
if embeds:
578+
payload['embeds'] = embeds
579+
if allowed_mentions:
580+
payload['allowed_mentions'] = allowed_mentions
581+
if components:
582+
payload['components'] = components
583+
584+
form.append({'name': 'payload_json', 'value': utils._to_json(payload)})
585+
if len(files) == 1:
586+
file = files[0]
587+
form.append(
588+
{
589+
'name': 'file',
590+
'value': file.fp,
591+
'filename': file.filename,
592+
'content_type': 'application/octet-stream',
593+
}
594+
)
595+
else:
596+
for index, file in enumerate(files):
597+
form.append(
598+
{
599+
'name': f'file{index}',
600+
'value': file.fp,
601+
'filename': file.filename,
602+
'content_type': 'application/octet-stream',
603+
}
604+
)
605+
606+
return self.request(route, form=form, files=files)
607+
608+
def edit_files(
609+
self,
610+
channel_id: Snowflake,
611+
message_id: Snowflake,
612+
*,
613+
files: Sequence[File],
614+
attachments: List[Attachment] = None,
615+
suppress: bool = False,
616+
content: Optional[str] = None,
617+
embed: Optional[embed.Embed] = None,
618+
embeds: Optional[List[embed.Embed]] = None,
619+
allowed_mentions: Optional[message.AllowedMentions] = None,
620+
components: Optional[List[components.Component]] = None,
621+
) -> Response[message.Message]:
622+
r = Route('PATCH', f'/channels/{channel_id}/messages/{message_id}', channel_id=channel_id, message_id=message_id)
623+
return self.edit_multipart_helper(
624+
r,
625+
files=files,
626+
attachments=attachments,
627+
suppress=suppress,
628+
content=content,
629+
embed=embed,
630+
embeds=embeds,
631+
allowed_mentions=allowed_mentions,
632+
components=components,
633+
)
551634

552635
def delete_message(
553636
self, channel_id: Snowflake, message_id: Snowflake, *, reason: Optional[str] = None

0 commit comments

Comments
 (0)