Use custom.Message in events

This commit is contained in:
Lonami Exo 2018-05-31 13:30:22 +02:00
parent b241d80958
commit 9e4854fcce
5 changed files with 22 additions and 12 deletions

View File

@ -1,6 +1,6 @@
from .common import EventBuilder, EventCommon, name_inner_event from .common import EventBuilder, EventCommon, name_inner_event
from .. import utils from .. import utils
from ..tl import types, functions from ..tl import types, functions, custom
@name_inner_event @name_inner_event
@ -158,6 +158,12 @@ class ChatAction(EventBuilder):
self.new_title = new_title self.new_title = new_title
self.unpin = unpin self.unpin = unpin
def _set_client(self, client):
super()._set_client(client)
if self.action_message:
self.action_message = custom.Message(
client, self.action_message, self._entities, None)
def respond(self, *args, **kwargs): def respond(self, *args, **kwargs):
""" """
Responds to the chat action message (not as a reply). Shorthand for Responds to the chat action message (not as a reply). Shorthand for

View File

@ -103,6 +103,12 @@ class EventCommon(abc.ABC):
) )
self.is_channel = isinstance(chat_peer, types.PeerChannel) self.is_channel = isinstance(chat_peer, types.PeerChannel)
def _set_client(self, client):
"""
Setter so subclasses can act accordingly when the client is set.
"""
self._client = client
def _get_entity(self, msg_id, entity_id, chat=None): def _get_entity(self, msg_id, entity_id, chat=None):
""" """
Helper function to call :tl:`GetMessages` on the give msg_id and Helper function to call :tl:`GetMessages` on the give msg_id and

View File

@ -100,16 +100,9 @@ class MessageRead(EventBuilder):
chat = self.input_chat chat = self.input_chat
if not chat: if not chat:
self._messages = [] self._messages = []
elif isinstance(chat, types.InputPeerChannel):
self._messages =\
self._client(functions.channels.GetMessagesRequest(
chat, self._message_ids
)).messages
else: else:
self._messages =\ self._messages = self._client.get_messages(
self._client(functions.messages.GetMessagesRequest( chat, ids=self._message_ids)
self._message_ids
)).messages
return self._messages return self._messages

View File

@ -3,7 +3,7 @@ import re
from .common import EventBuilder, EventCommon, name_inner_event from .common import EventBuilder, EventCommon, name_inner_event
from .. import utils from .. import utils
from ..extensions import markdown from ..extensions import markdown
from ..tl import types, functions from ..tl import types, functions, custom
@name_inner_event @name_inner_event
@ -154,6 +154,11 @@ class NewMessage(EventBuilder):
self.is_reply = bool(message.reply_to_msg_id) self.is_reply = bool(message.reply_to_msg_id)
self._reply_message = None self._reply_message = None
def _set_client(self, client):
super()._set_client(client)
self.message = custom.Message(
client, self.message, self._entities, None)
def respond(self, *args, **kwargs): def respond(self, *args, **kwargs):
""" """
Responds to the message (not as a reply). Shorthand for Responds to the message (not as a reply). Shorthand for

View File

@ -2416,7 +2416,7 @@ class TelegramClient(TelegramBareClient):
for builder, callback in self._event_builders: for builder, callback in self._event_builders:
event = builder.build(update) event = builder.build(update)
if event: if event:
event._client = self event._set_client(self)
event.original_update = update event.original_update = update
try: try:
callback(event) callback(event)