Browse Source

Add support for sending multiple embeds

pull/7069/merge
Aomi Vel 4 years ago
committed by GitHub
parent
commit
47e6a754e4
No known key found for this signature in database GPG Key ID: 4AEE18F83AFDEB23
  1. 39
      discord/abc.py
  2. 10
      discord/http.py
  3. 36
      discord/message.py

39
discord/abc.py

@ -1158,7 +1158,7 @@ class Messageable(Protocol):
content: Optional[str] = ...,
*,
tts: bool = ...,
embed: Embed = ...,
embeds: List[Embed] = ...,
files: List[File] = ...,
delete_after: int = ...,
nonce: Union[str, int] = ...,
@ -1169,9 +1169,9 @@ class Messageable(Protocol):
) -> Message:
...
async def send(self, content=None, *, tts=False, embed=None, file=None,
files=None, delete_after=None, nonce=None,
allowed_mentions=None, reference=None,
async def send(self, content=None, *, tts=False, embed=None, embeds=None,
file=None, files=None, delete_after=None,
nonce=None, allowed_mentions=None, reference=None,
mention_author=None, view=None):
"""|coro|
@ -1185,9 +1185,11 @@ class Messageable(Protocol):
single :class:`~discord.File` object. To upload multiple files, the ``files``
parameter should be used with a :class:`list` of :class:`~discord.File` objects.
**Specifying both parameters will lead to an exception**.
If the ``embed`` parameter is provided, it must be of type :class:`~discord.Embed` and
it must be a rich embed type.
To upload a single embed, the ``embed`` parameter should be used with a
single :class:`~discord.Embed` object. To upload multiple embeds, the ``embeds``
parameter should be used with a :class:`list` of :class:`~discord.Embed` objects.
**Specifying both parameters will lead to an exception**.
Parameters
------------
@ -1232,6 +1234,8 @@ class Messageable(Protocol):
.. versionadded:: 1.6
view: :class:`discord.ui.View`
A Discord UI View to add to the message.
embeds: List[:class:`~discord.Embed`]
A list of embeds to upload. Must be a maximum of 10.
.. versionadded:: 2.0
@ -1244,6 +1248,7 @@ class Messageable(Protocol):
~discord.InvalidArgument
The ``files`` list is not of the appropriate size,
you specified both ``file`` and ``files``,
or you specified both ``embed`` and ``embeds``,
or the ``reference`` object is not a :class:`~discord.Message`
or :class:`~discord.MessageReference`.
@ -1256,8 +1261,17 @@ class Messageable(Protocol):
channel = await self._get_channel()
state = self._state
content = str(content) if content is not None else None
if embed is not None and embeds is not None:
raise InvalidArgument('cannot pass both embed and embeds parameter to send()')
if embed is not None:
embed = embed.to_dict()
elif embeds is not None:
if len(embeds) > 10:
raise InvalidArgument('embeds parameter must be a list of up to 10 elements')
embeds = [embed.to_dict() for embed in embeds]
if allowed_mentions is not None:
if state.allowed_mentions is not None:
@ -1294,8 +1308,8 @@ class Messageable(Protocol):
try:
data = await state.http.send_files(channel.id, files=[file], allowed_mentions=allowed_mentions,
content=content, tts=tts, embed=embed, nonce=nonce,
message_reference=reference, components=components)
content=content, tts=tts, embed=embed, embeds=embeds,
nonce=nonce, message_reference=reference, components=components)
finally:
file.close()
@ -1307,14 +1321,15 @@ class Messageable(Protocol):
try:
data = await state.http.send_files(channel.id, files=files, content=content, tts=tts,
embed=embed, nonce=nonce, allowed_mentions=allowed_mentions,
message_reference=reference, components=components)
embed=embed, embeds=embeds, nonce=nonce,
allowed_mentions=allowed_mentions, message_reference=reference,
components=components)
finally:
for f in files:
f.close()
else:
data = await state.http.send_message(channel.id, content, tts=tts, embed=embed,
nonce=nonce, allowed_mentions=allowed_mentions,
embeds=embeds, nonce=nonce, allowed_mentions=allowed_mentions,
message_reference=reference, components=components)
ret = state.create_message(channel=channel, data=data)

10
discord/http.py

@ -412,6 +412,7 @@ class HTTPClient:
*,
tts: bool = False,
embed: Optional[embed.Embed] = None,
embeds: Optional[List[embed.Embed]] = None,
nonce: Optional[str] = None,
allowed_mentions: bool = None,
message_reference: bool = None,
@ -427,7 +428,10 @@ class HTTPClient:
payload['tts'] = True
if embed:
payload['embed'] = embed
payload['embeds'] = [embed]
if embeds:
payload['embeds'] = embeds
if nonce:
payload['nonce'] = nonce
@ -466,7 +470,7 @@ class HTTPClient:
if content:
payload['content'] = content
if embed:
payload['embed'] = embed
payload['embeds'] = [embed]
if embeds:
payload['embeds'] = embeds
if nonce:
@ -510,6 +514,7 @@ class HTTPClient:
content: Optional[str] = None,
tts: bool = False,
embed: Optional[embed.Embed] = None,
embeds: Optional[List[embed.Embed]] = None,
nonce: Optional[str] = None,
allowed_mentions: Optional[message.AllowedMentions] = None,
message_reference: Optional[message.MessageReference] = None,
@ -522,6 +527,7 @@ class HTTPClient:
content=content,
tts=tts,
embed=embed,
embeds=embeds,
nonce=nonce,
allowed_mentions=allowed_mentions,
message_reference=message_reference,

36
discord/message.py

@ -1110,7 +1110,17 @@ class Message(Hashable):
...
@overload
async def edit(self) -> None:
async def edit(
self,
*,
content: Optional[str] = ...,
embeds: Optional[List[Embed]] = ...,
attachments: List[Attachment] = ...,
suppress: bool = ...,
delete_after: Optional[float] = ...,
allowed_mentions: Optional[AllowedMentions] = ...,
view: Optional[View] = ...,
) -> None:
...
async def edit(self, **fields) -> None:
@ -1155,6 +1165,10 @@ class Message(Hashable):
view: Optional[:class:`~discord.ui.View`]
The updated view to update this message with. If ``None`` is passed then
the view is removed.
embeds: Optional[List[:class:`Embeds`]]
The new embeds to replace the original with.
Could be ``None`` to remove the embeds.
Must be a maximum of 10.
.. versionadded:: 2.0
@ -1165,6 +1179,8 @@ class Message(Hashable):
Forbidden
Tried to suppress a message without permissions or
edited a message's content or embed that isn't yours.
~discord.InvalidArgument
You specified both ``embed`` and ``embeds``
"""
try:
@ -1176,12 +1192,24 @@ class Message(Hashable):
fields['content'] = str(content)
try:
embed = fields['embed']
fields['embed']
fields['embeds']
except KeyError:
pass
else:
if embed is not None:
fields['embed'] = embed.to_dict()
raise InvalidArgument('cannot pass both embed and embeds parameter to edit()')
try:
embeds = fields.pop('embeds')
except KeyError:
pass
else:
fields['embeds'] = [embed.to_dict() for embed in embeds]
try:
embed = fields.pop('embed')
except KeyError:
pass
else:
fields['embeds'] = [embed.to_dict()]
try:
suppress = fields.pop('suppress')

Loading…
Cancel
Save