Browse Source

Parse full message for raw message edit event

pull/10082/head
Soheab 3 months ago
committed by GitHub
parent
commit
ed95f2f106
No known key found for this signature in database GPG Key ID: B5690EEEBB952194
  1. 18
      discord/raw_models.py
  2. 18
      discord/state.py
  3. 3
      discord/types/gateway.py

18
discord/raw_models.py

@ -166,20 +166,22 @@ class RawMessageUpdateEvent(_RawReprMixin):
cached_message: Optional[:class:`Message`] cached_message: Optional[:class:`Message`]
The cached message, if found in the internal message cache. Represents the message before The cached message, if found in the internal message cache. Represents the message before
it is modified by the data in :attr:`RawMessageUpdateEvent.data`. it is modified by the data in :attr:`RawMessageUpdateEvent.data`.
message: :class:`Message`
The updated message.
.. versionadded:: 2.5
""" """
__slots__ = ('message_id', 'channel_id', 'guild_id', 'data', 'cached_message') __slots__ = ('message_id', 'channel_id', 'guild_id', 'data', 'cached_message', 'message')
def __init__(self, data: MessageUpdateEvent) -> None: def __init__(self, data: MessageUpdateEvent, message: Message) -> None:
self.message_id: int = int(data['id']) self.message_id: int = message.id
self.channel_id: int = int(data['channel_id']) self.channel_id: int = message.channel.id
self.data: MessageUpdateEvent = data self.data: MessageUpdateEvent = data
self.message: Message = message
self.cached_message: Optional[Message] = None self.cached_message: Optional[Message] = None
try: self.guild_id: Optional[int] = message.guild.id if message.guild else None
self.guild_id: Optional[int] = int(data['guild_id'])
except KeyError:
self.guild_id: Optional[int] = None
class RawReactionActionEvent(_RawReprMixin): class RawReactionActionEvent(_RawReprMixin):

18
discord/state.py

@ -690,17 +690,21 @@ class ConnectionState(Generic[ClientT]):
self._messages.remove(msg) # type: ignore self._messages.remove(msg) # type: ignore
def parse_message_update(self, data: gw.MessageUpdateEvent) -> None: def parse_message_update(self, data: gw.MessageUpdateEvent) -> None:
raw = RawMessageUpdateEvent(data) channel, _ = self._get_guild_channel(data)
message = self._get_message(raw.message_id) # channel would be the correct type here
if message is not None: updated_message = Message(channel=channel, data=data, state=self) # type: ignore
older_message = copy.copy(message)
raw = RawMessageUpdateEvent(data=data, message=updated_message)
cached_message = self._get_message(updated_message.id)
if cached_message is not None:
older_message = copy.copy(cached_message)
raw.cached_message = older_message raw.cached_message = older_message
self.dispatch('raw_message_edit', raw) self.dispatch('raw_message_edit', raw)
message._update(data) cached_message._update(data)
# Coerce the `after` parameter to take the new updated Member # Coerce the `after` parameter to take the new updated Member
# ref: #5999 # ref: #5999
older_message.author = message.author older_message.author = updated_message.author
self.dispatch('message_edit', older_message, message) self.dispatch('message_edit', older_message, updated_message)
else: else:
self.dispatch('raw_message_edit', raw) self.dispatch('raw_message_edit', raw)

3
discord/types/gateway.py

@ -92,8 +92,7 @@ class MessageDeleteBulkEvent(TypedDict):
guild_id: NotRequired[Snowflake] guild_id: NotRequired[Snowflake]
class MessageUpdateEvent(Message): MessageUpdateEvent = MessageCreateEvent
channel_id: Snowflake
class MessageReactionAddEvent(TypedDict): class MessageReactionAddEvent(TypedDict):

Loading…
Cancel
Save