Fix setattr for events.NewMessage/custom.Message

This commit is contained in:
Lonami Exo 2018-06-03 13:00:07 +02:00
parent 0a3151175d
commit b2ed6caff4
2 changed files with 11 additions and 12 deletions

View File

@ -129,6 +129,8 @@ class NewMessage(EventBuilder):
available members and methods.
"""
def __init__(self, message):
# Having to override __setattr__ makes things... complicated
self.__dict__['_init'] = False
if not message.out and isinstance(message.to_id, types.PeerUser):
# Incoming message (e.g. from a bot) has to_id=us, and
# from_id=bot (the actual "chat" from an user's perspective).
@ -145,9 +147,13 @@ class NewMessage(EventBuilder):
super()._set_client(client)
self.message = custom.Message(
client, self.message, self._entities, None)
self._init = True
def __getattr__(self, item):
return getattr(self.message, item)
def __setattr__(self, name, value):
return setattr(self.original_message, name, value)
if self._init:
setattr(self.__dict__['message'], name, value)
else:
super().__setattr__(name, value)

View File

@ -19,7 +19,10 @@ class Message:
in the original :tl:`Message`.
"""
def __init__(self, client, original, entities, input_chat):
self.__dict__['_init'] = False
# Share the original dictionary. Modifications to this
# object should also be reflected in the original one.
# This way there's no need to worry about get/setattr.
self.__dict__ = original.__dict__
self.original_message = original
self.stringify = self.original_message.stringify
self.to_dict = self.original_message.to_dict
@ -43,7 +46,6 @@ class Message:
elif fwd.channel_id:
self._fwd_from_entity = entities.get(get_peer_id(
types.PeerChannel(fwd.channel_id)))
self._init = True
def __new__(cls, client, original, entities, input_chat):
if isinstance(original, types.Message):
@ -53,15 +55,6 @@ class Message:
else:
return cls
def __getattr__(self, item):
return getattr(self.original_message, item)
def __setattr__(self, name, value):
if not self._init or name in self.__dict__:
self.__dict__[name] = value
else:
setattr(self.original_message, name, value)
def __str__(self):
return str(self.original_message)