Skip to content

Commit

Permalink
feat!: Implement attachment sending (#869)
Browse files Browse the repository at this point in the history
* feat!: Implement attachment sending

* Update interactions/client/context.py

Co-authored-by: Sofia <41456914+ffl0w@users.noreply.github.com>

* Update interactions/api/models/channel.py

Co-authored-by: Sofia <41456914+ffl0w@users.noreply.github.com>

* Update interactions/api/models/member.py

Co-authored-by: Sofia <41456914+ffl0w@users.noreply.github.com>

* Update interactions/api/models/message.py

Co-authored-by: Sofia <41456914+ffl0w@users.noreply.github.com>

* Update interactions/api/models/webhook.py

Co-authored-by: Sofia <41456914+ffl0w@users.noreply.github.com>

* fix: resolve circular import

Co-authored-by: Sofia <41456914+ffl0w@users.noreply.github.com>
  • Loading branch information
EepyElvyra and i0bs authored Jun 19, 2022
1 parent 8e0d2ab commit c2b8cd6
Show file tree
Hide file tree
Showing 10 changed files with 62 additions and 28 deletions.
10 changes: 7 additions & 3 deletions interactions/api/models/channel.py
Original file line number Diff line number Diff line change
Expand Up @@ -178,6 +178,7 @@ async def send(
content: Optional[str] = MISSING,
*,
tts: Optional[bool] = MISSING,
attachments: Optional[List["Attachment"]] = MISSING, # noqa
files: Optional[Union[File, List[File]]] = MISSING,
embeds: Optional[Union["Embed", List["Embed"]]] = MISSING, # noqa
allowed_mentions: Optional["MessageInteraction"] = MISSING, # noqa
Expand All @@ -201,6 +202,8 @@ async def send(
:type tts: Optional[bool]
:param files?: A file or list of files to be attached to the message.
:type files: Optional[Union[File, List[File]]]
:param attachments?: The attachments to attach to the message. Needs to be uploaded to the CDN first
:type attachments: Optional[List[Attachment]]
:param embeds?: An embed, or list of embeds for the message.
:type embeds: Optional[Union[Embed, List[Embed]]]
:param allowed_mentions?: The message interactions/mention limits that the message can refer to.
Expand All @@ -217,8 +220,7 @@ async def send(

_content: str = "" if content is MISSING else content
_tts: bool = False if tts is MISSING else tts
# _file = None if file is None else file
# _attachments = [] if attachments else None
_attachments = [] if attachments is MISSING else [a._json for a in attachments]
_allowed_mentions: dict = {} if allowed_mentions is MISSING else allowed_mentions
if not embeds or embeds is MISSING:
_embeds: list = []
Expand All @@ -240,6 +242,8 @@ async def send(
_files = [files._json_payload(0)]
files = [files]

_files.extend(_attachments)

payload = dict(
content=_content,
tts=_tts,
Expand All @@ -255,7 +259,7 @@ async def send(

# dumb hack, discord doesn't send the full author data
author = {"id": None, "username": None, "discriminator": None}
author.update(res["author"])
author |= res["author"]
res["author"] = author

return Message(**res, _client=self._client)
Expand Down
1 change: 1 addition & 0 deletions interactions/api/models/channel.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,7 @@ class Channel(ClientSerializerMixin):
files: Optional[Union[File, List[File]]] = ...,
embeds: Optional[Union[Embed, List[Embed]]] = ...,
allowed_mentions: Optional[MessageInteraction] = ...,
attachments: Optional[List["Attachment"]] = MISSING, # noqa
components: Optional[
Union[
"ActionRow",
Expand Down
8 changes: 6 additions & 2 deletions interactions/api/models/member.py
Original file line number Diff line number Diff line change
Expand Up @@ -214,6 +214,7 @@ async def send(
]
] = MISSING,
tts: Optional[bool] = MISSING,
attachments: Optional[List["Attachment"]] = MISSING, # noqa
files: Optional[Union[File, List[File]]] = MISSING,
embeds: Optional[Union["Embed", List["Embed"]]] = MISSING, # noqa
allowed_mentions: Optional["MessageInteraction"] = MISSING, # noqa
Expand All @@ -227,6 +228,8 @@ async def send(
:type components: Optional[Union[ActionRow, Button, SelectMenu, List[ActionRow], List[Button], List[SelectMenu]]]
:param tts?: Whether the message utilizes the text-to-speech Discord programme or not.
:type tts: Optional[bool]
:param attachments?: The attachments to attach to the message. Needs to be uploaded to the CDN first
:type attachments: Optional[List[Attachment]]
:param files?: A file or list of files to be attached to the message.
:type files: Optional[Union[File, List[File]]]
:param embeds?: An embed, or list of embeds for the message.
Expand All @@ -243,7 +246,7 @@ async def send(

_content: str = "" if content is MISSING else content
_tts: bool = False if tts is MISSING else tts
# _attachments = [] if attachments else None
_attachments = [] if attachments is MISSING else [a._json for a in attachments]
_embeds: list = (
[]
if not embeds or embeds is MISSING
Expand All @@ -264,7 +267,8 @@ async def send(
_files = [files._json_payload(0)]
files = [files]

# TODO: post-v4: Add attachments into Message obj.
_files.extend(_attachments)

payload = dict(
content=_content,
tts=_tts,
Expand Down
1 change: 1 addition & 0 deletions interactions/api/models/member.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@ class Member(ClientSerializerMixin):
components: Optional[
Union[ActionRow, Button, SelectMenu, List[ActionRow], List[Button], List[SelectMenu]]
] = ...,
attachments: Optional[List["Attachment"]] = ..., # noqa
tts: Optional[bool] = ...,
files: Optional[Union[File, List[File]]] = ...,
embeds: Optional[Union[Embed, List[Embed]]] = ...,
Expand Down
10 changes: 7 additions & 3 deletions interactions/api/models/message.py
Original file line number Diff line number Diff line change
Expand Up @@ -1031,6 +1031,7 @@ async def reply(
tts: Optional[bool] = MISSING,
embeds: Optional[Union["Embed", List["Embed"]]] = MISSING,
files: Optional[Union[File, List[File]]] = MISSING,
attachments: Optional[List["Attachment"]] = MISSING,
allowed_mentions: Optional["MessageInteraction"] = MISSING,
components: Optional[
Union[
Expand All @@ -1050,6 +1051,8 @@ async def reply(
:type content: Optional[str]
:param tts?: Whether the message utilizes the text-to-speech Discord programme or not.
:type tts: Optional[bool]
:param attachments?: The attachments to attach to the message. Needs to be uploaded to the CDN first
:type attachments: Optional[List[Attachment]]
:param files?: A file or list of files to be attached to the message.
:type files: Optional[Union[File, List[File]]]
:param embeds?: An embed, or list of embeds for the message.
Expand All @@ -1076,7 +1079,7 @@ async def reply(
)
_allowed_mentions: dict = {} if allowed_mentions is MISSING else allowed_mentions
_message_reference = MessageReference(message_id=int(self.id))._json

_attachments = [] if attachments is MISSING else [a._json for a in attachments]
if not components or components is MISSING:
_components = []
else:
Expand All @@ -1090,7 +1093,8 @@ async def reply(
_files = [files._json_payload(0)]
files = [files]

# TODO: post-v4: Add attachments into Message obj.
_files.extend(_attachments)

payload = dict(
content=_content,
tts=_tts,
Expand All @@ -1106,7 +1110,7 @@ async def reply(
)

author = {"id": None, "username": None, "discriminator": None}
author.update(res["author"])
author |= res["author"]
res["author"] = author

return Message(**res, _client=self._client)
Expand Down
1 change: 1 addition & 0 deletions interactions/api/models/message.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -284,6 +284,7 @@ class Message(ClientSerializerMixin):
files: Optional[Union[File, List[File]]] = MISSING,
embeds: Optional[Union[Embed, List[Embed]]] = MISSING,
allowed_mentions: Optional[MessageInteraction] = MISSING,
attachments: Optional[List["Attachment"]] = MISSING, # noqa
components: Optional[
Union[
ActionRow,
Expand Down
12 changes: 8 additions & 4 deletions interactions/api/models/webhook.py
Original file line number Diff line number Diff line change
Expand Up @@ -176,6 +176,7 @@ async def execute(
tts: Optional[bool] = MISSING,
embeds: Optional[Union["Embed", List["Embed"]]] = MISSING, # noqa
allowed_mentions: Any = MISSING,
attachments: Optional[List["Attachment"]] = MISSING, # noqa
components: Optional[
Union[
"ActionRow", # noqa
Expand Down Expand Up @@ -203,6 +204,8 @@ async def execute(
:type avatar_url: str
:param tts: true if this is a TTS message
:type tts: bool
:param attachments?: The attachments to attach to the message. Needs to be uploaded to the CDN first
:type attachments: Optional[List[Attachment]]
:param embeds: embedded ``rich`` content
:type embeds: Union[Embed, List[Embed]]
:param allowed_mentions: allowed mentions for the message
Expand All @@ -225,7 +228,8 @@ async def execute(

_content: str = "" if content is MISSING else content
_tts: bool = False if tts is MISSING else tts
# _attachments = [] if attachments else None
_attachments = [] if attachments is MISSING else [a._json for a in attachments]

_embeds: list = (
[]
if not embeds or embeds is MISSING
Expand All @@ -246,7 +250,9 @@ async def execute(
_files = [files._json_payload(0)]
files = [files]

msg = Message(
_files.extend(_attachments)

payload: dict = dict(
content=_content,
tts=_tts,
attachments=_files,
Expand All @@ -255,8 +261,6 @@ async def execute(
allowed_mentions=_allowed_mentions,
)

payload = msg._json

if username is not MISSING:
payload["username"] = username

Expand Down
1 change: 1 addition & 0 deletions interactions/api/models/webhook.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ class Webhook(ClientSerializerMixin):
Union[ActionRow, Button, SelectMenu, List[ActionRow], List[Button], List[SelectMenu]]
] = MISSING,
files: Optional[Union[File, List[File]]] = MISSING,
attachments: Optional[List["Attachment"]] = MISSING, # noqa
thread_id: Optional[int] = MISSING,
) -> Optional[Message]: ...
async def delete(self) -> None: ...
Expand Down
44 changes: 28 additions & 16 deletions interactions/client/context.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
from ..api.models.channel import Channel
from ..api.models.guild import Guild
from ..api.models.member import Member
from ..api.models.message import Embed, Message, MessageInteraction, MessageReference
from ..api.models.message import Attachment, Embed, Message, MessageInteraction, MessageReference
from ..api.models.misc import Snowflake
from ..api.models.user import User
from ..base import get_logger
Expand Down Expand Up @@ -105,7 +105,7 @@ async def send(
content: Optional[str] = MISSING,
*,
tts: Optional[bool] = MISSING,
# attachments: Optional[List[Any]] = None, # TODO: post-v4: Replace with own file type.
attachments: Optional[List[Attachment]] = MISSING,
embeds: Optional[Union[Embed, List[Embed]]] = MISSING,
allowed_mentions: Optional[MessageInteraction] = MISSING,
components: Optional[
Expand All @@ -121,6 +121,8 @@ async def send(
:type content: Optional[str]
:param tts?: Whether the message utilizes the text-to-speech Discord programme or not.
:type tts: Optional[bool]
:param attachments?: The attachments to attach to the message. Needs to be uploaded to the CDN first
:type attachments: Optional[List[Attachment]]
:param embeds?: An embed, or list of embeds for the message.
:type embeds: Optional[Union[Embed, List[Embed]]]
:param allowed_mentions?: The message interactions/mention limits that the message can refer to.
Expand All @@ -141,8 +143,6 @@ async def send(
else:
_content: str = "" if content is MISSING else content
_tts: bool = False if tts is MISSING else tts
# _file = None if file is None else file
# _attachments = [] if attachments else None

if (
embeds is MISSING
Expand Down Expand Up @@ -177,23 +177,24 @@ async def send(

_ephemeral: int = (1 << 6) if ephemeral else 0

# TODO: post-v4: Add attachments into Message obj.
payload: dict = dict(
_attachments = [] if attachments is MISSING else [a._json for a in attachments]

return dict(
content=_content,
tts=_tts,
embeds=_embeds,
allowed_mentions=_allowed_mentions,
components=_components,
attachments=_attachments,
flags=_ephemeral,
)
return payload

async def edit(
self,
content: Optional[str] = MISSING,
*,
tts: Optional[bool] = MISSING,
# file: Optional[FileIO] = None,
attachments: Optional[List[Attachment]] = MISSING,
embeds: Optional[Union[Embed, List[Embed]]] = MISSING,
allowed_mentions: Optional[MessageInteraction] = MISSING,
message_reference: Optional[MessageReference] = MISSING,
Expand All @@ -217,22 +218,33 @@ async def edit(
payload["content"] = _content
_tts: bool = False if tts is MISSING else tts
payload["tts"] = _tts
# _file = None if file is None else file

if self.message.embeds is not None or embeds is not MISSING:
if embeds is MISSING:
embeds = self.message.embeds
_embeds: list = (
[]
if not embeds
else (
[embed._json for embed in embeds]
if isinstance(embeds, list)
else [embeds._json]
)
([embed._json for embed in embeds] if isinstance(embeds, list) else [embeds._json])
if embeds
else []
)

payload["embeds"] = _embeds

if self.message.attachments is not None or attachments is not MISSING:
if attachments is MISSING:
attachments = self.message.attachments
_attachments: list = (
(
[attachment._json for attachment in attachments]
if isinstance(attachments, list)
else [attachments._json]
)
if attachments
else []
)

payload["attachments"] = _attachments

_allowed_mentions: dict = {} if allowed_mentions is MISSING else allowed_mentions
_message_reference: dict = {} if message_reference is MISSING else message_reference._json

Expand Down
2 changes: 2 additions & 0 deletions interactions/client/context.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ class _Context(ClientSerializerMixin):
components: Optional[
Union[ActionRow, Button, SelectMenu, List[ActionRow], List[Button], List[SelectMenu]]
] = ...,
attachments: Optional[List["Attachment"]] = ..., # noqa
ephemeral: Optional[bool] = ...
) -> dict: ...
async def edit(
Expand All @@ -66,6 +67,7 @@ class _Context(ClientSerializerMixin):
embeds: Optional[Union[Embed, List[Embed]]] = ...,
allowed_mentions: Optional[MessageInteraction] = ...,
message_reference: Optional[MessageReference] = ...,
attachments: Optional[List["Attachment"]] = MISSING, # noqa
components: Optional[
Union[ActionRow, Button, SelectMenu, List[ActionRow], List[Button], List[SelectMenu]]
] = ...
Expand Down

0 comments on commit c2b8cd6

Please sign in to comment.