From b1836c55771112543c9383525bcb6a63ebacd936 Mon Sep 17 00:00:00 2001 From: Rapptz Date: Mon, 28 Jun 2021 00:33:59 -0400 Subject: [PATCH] Rework Message.edit implementation --- discord/message.py | 110 +++++++++++++++++++-------------------------- 1 file changed, 46 insertions(+), 64 deletions(-) diff --git a/discord/message.py b/discord/message.py index fde50e305..3f5df3bd8 100644 --- a/discord/message.py +++ b/discord/message.py @@ -29,7 +29,7 @@ import datetime import re import io 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 .reaction import Reaction @@ -42,7 +42,7 @@ from .embeds import Embed from .member import Member from .flags import MessageFlags from .file import File -from .utils import escape_mentions +from .utils import escape_mentions, MISSING from .guild import Guild from .mixins import Hashable from .sticker import Sticker @@ -1114,7 +1114,7 @@ class Message(Hashable): self, *, content: Optional[str] = ..., - embeds: Optional[List[Embed]] = ..., + embeds: List[Embed] = ..., attachments: List[Attachment] = ..., suppress: bool = ..., delete_after: Optional[float] = ..., @@ -1123,7 +1123,17 @@ class Message(Hashable): ) -> 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| Edits the message. @@ -1141,6 +1151,11 @@ class Message(Hashable): embed: Optional[:class:`Embed`] The new embed to replace the original with. 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`] A list of attachments to keep in the message. If ``[]`` is passed then all attachments are removed. @@ -1165,12 +1180,6 @@ 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 Raises ------- @@ -1183,79 +1192,52 @@ class Message(Hashable): You specified both ``embed`` and ``embeds`` """ - try: - content = fields['content'] - except KeyError: - pass - else: + payload: Dict[str, Any] = {} + if content is not MISSING: if content is not None: - fields['content'] = str(content) + payload['content'] = str(content) + else: + payload['content'] = None - try: - fields['embed'] - fields['embeds'] - except KeyError: - pass - else: + if embed is not MISSING and embeds is not MISSING: 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') - except KeyError: - pass - else: + if embed is not MISSING: + if embed is None: + payload['embeds'] = [] + 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.suppress_embeds = suppress - fields['flags'] = flags.value + payload['flags'] = flags.value - delete_after = fields.pop('delete_after', None) - try: - allowed_mentions = fields.pop('allowed_mentions') - except KeyError: + if allowed_mentions is MISSING: 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: if 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: - allowed_mentions = allowed_mentions.to_dict() - fields['allowed_mentions'] = allowed_mentions + payload['allowed_mentions'] = allowed_mentions.to_dict() - try: - attachments = fields.pop('attachments') - except KeyError: - pass - else: - fields['attachments'] = [a.to_dict() for a in attachments] + if attachments is not MISSING: + payload['attachments'] = [a.to_dict() for a in attachments] - try: - view = fields.pop('view') - except KeyError: - # To check for the view afterwards - view = None - else: + if view is not MISSING: self._state.prevent_view_updates_for(self.id) if view: - fields['components'] = view.to_components() + payload['components'] = view.to_components() else: - fields['components'] = [] + payload['components'] = [] - if fields: - data = await self._state.http.edit_message(self.channel.id, self.id, **fields) + if payload: + data = await self._state.http.edit_message(self.channel.id, self.id, **payload) self._update(data) if view and not view.is_finished():