Browse Source

Rework Message.edit implementation

pull/7122/head
Rapptz 4 years ago
parent
commit
b1836c5577
  1. 110
      discord/message.py

110
discord/message.py

@ -29,7 +29,7 @@ import datetime
import re import re
import io import io
from os import PathLike from os import PathLike
from typing import TYPE_CHECKING, Union, List, Optional, Any, Callable, Tuple, ClassVar, Optional, overload from typing import Dict, TYPE_CHECKING, Union, List, Optional, Any, Callable, Tuple, ClassVar, Optional, overload
from . import utils from . import utils
from .reaction import Reaction from .reaction import Reaction
@ -42,7 +42,7 @@ from .embeds import Embed
from .member import Member from .member import Member
from .flags import MessageFlags from .flags import MessageFlags
from .file import File from .file import File
from .utils import escape_mentions from .utils import escape_mentions, MISSING
from .guild import Guild from .guild import Guild
from .mixins import Hashable from .mixins import Hashable
from .sticker import Sticker from .sticker import Sticker
@ -1114,7 +1114,7 @@ class Message(Hashable):
self, self,
*, *,
content: Optional[str] = ..., content: Optional[str] = ...,
embeds: Optional[List[Embed]] = ..., embeds: List[Embed] = ...,
attachments: List[Attachment] = ..., attachments: List[Attachment] = ...,
suppress: bool = ..., suppress: bool = ...,
delete_after: Optional[float] = ..., delete_after: Optional[float] = ...,
@ -1123,7 +1123,17 @@ class Message(Hashable):
) -> None: ) -> None:
... ...
async def edit(self, **fields) -> None: async def edit(
self,
content: Optional[str] = MISSING,
embed: Optional[Embed] = MISSING,
embeds: List[Embed] = MISSING,
attachments: List[Attachment] = MISSING,
suppress: bool = MISSING,
delete_after: Optional[float] = None,
allowed_mentions: Optional[AllowedMentions] = MISSING,
view: Optional[View] = MISSING,
) -> None:
"""|coro| """|coro|
Edits the message. Edits the message.
@ -1141,6 +1151,11 @@ class Message(Hashable):
embed: Optional[:class:`Embed`] embed: Optional[:class:`Embed`]
The new embed to replace the original with. The new embed to replace the original with.
Could be ``None`` to remove the embed. Could be ``None`` to remove the embed.
embeds: List[:class:`Embeds`]
The new embeds to replace the original with. Must be a maximum of 10.
To remove all embeds ``[]`` should be passed.
.. versionadded:: 2.0
attachments: List[:class:`Attachment`] attachments: List[:class:`Attachment`]
A list of attachments to keep in the message. If ``[]`` is passed A list of attachments to keep in the message. If ``[]`` is passed
then all attachments are removed. then all attachments are removed.
@ -1165,12 +1180,6 @@ class Message(Hashable):
view: Optional[:class:`~discord.ui.View`] view: Optional[:class:`~discord.ui.View`]
The updated view to update this message with. If ``None`` is passed then The updated view to update this message with. If ``None`` is passed then
the view is removed. 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
Raises Raises
------- -------
@ -1183,79 +1192,52 @@ class Message(Hashable):
You specified both ``embed`` and ``embeds`` You specified both ``embed`` and ``embeds``
""" """
try: payload: Dict[str, Any] = {}
content = fields['content'] if content is not MISSING:
except KeyError:
pass
else:
if content is not None: if content is not None:
fields['content'] = str(content) payload['content'] = str(content)
else:
payload['content'] = None
try: if embed is not MISSING and embeds is not MISSING:
fields['embed']
fields['embeds']
except KeyError:
pass
else:
raise InvalidArgument('cannot pass both embed and embeds parameter to edit()') 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: if embed is not MISSING:
suppress = fields.pop('suppress') if embed is None:
except KeyError: payload['embeds'] = []
pass else:
else: payload['embeds'] = [embed.to_dict()]
elif embeds is not MISSING:
payload['embeds'] = [e.to_dict() for e in embeds]
if suppress is not MISSING:
flags = MessageFlags._from_value(self.flags.value) flags = MessageFlags._from_value(self.flags.value)
flags.suppress_embeds = suppress flags.suppress_embeds = suppress
fields['flags'] = flags.value payload['flags'] = flags.value
delete_after = fields.pop('delete_after', None)
try: if allowed_mentions is MISSING:
allowed_mentions = fields.pop('allowed_mentions')
except KeyError:
if self._state.allowed_mentions is not None and self.author.id == self._state.self_id: if self._state.allowed_mentions is not None and self.author.id == self._state.self_id:
fields['allowed_mentions'] = self._state.allowed_mentions.to_dict() payload['allowed_mentions'] = self._state.allowed_mentions.to_dict()
else: else:
if allowed_mentions is not None: if allowed_mentions is not None:
if self._state.allowed_mentions is not None: if self._state.allowed_mentions is not None:
allowed_mentions = self._state.allowed_mentions.merge(allowed_mentions).to_dict() payload['allowed_mentions'] = self._state.allowed_mentions.merge(allowed_mentions).to_dict()
else: else:
allowed_mentions = allowed_mentions.to_dict() payload['allowed_mentions'] = allowed_mentions.to_dict()
fields['allowed_mentions'] = allowed_mentions
try: if attachments is not MISSING:
attachments = fields.pop('attachments') payload['attachments'] = [a.to_dict() for a in attachments]
except KeyError:
pass
else:
fields['attachments'] = [a.to_dict() for a in attachments]
try: if view is not MISSING:
view = fields.pop('view')
except KeyError:
# To check for the view afterwards
view = None
else:
self._state.prevent_view_updates_for(self.id) self._state.prevent_view_updates_for(self.id)
if view: if view:
fields['components'] = view.to_components() payload['components'] = view.to_components()
else: else:
fields['components'] = [] payload['components'] = []
if fields: if payload:
data = await self._state.http.edit_message(self.channel.id, self.id, **fields) data = await self._state.http.edit_message(self.channel.id, self.id, **payload)
self._update(data) self._update(data)
if view and not view.is_finished(): if view and not view.is_finished():

Loading…
Cancel
Save