Ensure member key is not overwritten by author key in MESSAGE_UPDATE

This also coerces the older message to take the member data from the
newer message so the types are not incompatible.

Fix #5999
This commit is contained in:
Rapptz 2020-11-23 06:02:27 -05:00
parent 7a3a571e0a
commit f174365d33
2 changed files with 17 additions and 7 deletions

View File

@ -243,11 +243,15 @@ class MessageReference:
def flatten_handlers(cls):
prefix = len('_handle_')
cls._HANDLERS = {
key[prefix:]: value
handlers = [
(key[prefix:], value)
for key, value in cls.__dict__.items()
if key.startswith('_handle_')
}
if key.startswith('_handle_') and key != '_handle_member'
]
# store _handle_member last
handlers.append(('member', cls._handle_member))
cls._HANDLERS = handlers
cls._CACHED_SLOTS = [
attr for attr in cls.__slots__ if attr.startswith('_cs_')
]
@ -452,10 +456,13 @@ class Message(Hashable):
return reaction
def _update(self, data):
handlers = self._HANDLERS
for key, value in data.items():
# In an update scheme, 'author' key has to be handled before 'member'
# otherwise they overwrite each other which is undesirable.
# Since there's no good way to do this we have to iterate over every
# handler rather than iterating over the keys which is a little slower
for key, handler in self._HANDLERS:
try:
handler = handlers[key]
value = data[key]
except KeyError:
continue
else:

View File

@ -526,6 +526,9 @@ class ConnectionState:
raw.cached_message = older_message
self.dispatch('raw_message_edit', raw)
message._update(data)
# Coerce the `after` parameter to take the new updated Member
# ref: #5999
older_message.author = message.author
self.dispatch('message_edit', older_message, message)
else:
self.dispatch('raw_message_edit', raw)