Add missing checks in the message for is client None

This commit is contained in:
Lonami Exo 2019-05-02 18:44:28 +02:00
parent b58c0d3071
commit 1a00de6494
3 changed files with 32 additions and 21 deletions

View File

@ -49,7 +49,7 @@ class ChatGetter(abc.ABC):
Note that this might not be available if the library doesn't Note that this might not be available if the library doesn't
have enough information available. have enough information available.
""" """
if self._input_chat is None and self._chat_peer: if self._input_chat is None and self._chat_peer and self._client:
try: try:
self._input_chat = self._client._entity_cache[self._chat_peer] self._input_chat = self._client._entity_cache[self._chat_peer]
except KeyError: except KeyError:
@ -62,7 +62,7 @@ class ChatGetter(abc.ABC):
Returns `input_chat`, but will make an API call to find the Returns `input_chat`, but will make an API call to find the
input chat unless it's already cached. input chat unless it's already cached.
""" """
if self.input_chat is None and self.chat_id: if self.input_chat is None and self.chat_id and self._client:
try: try:
# The chat may be recent, look in dialogs # The chat may be recent, look in dialogs
target = self.chat_id target = self.chat_id

View File

@ -586,7 +586,7 @@ class Message(ChatGetter, SenderGetter, TLObject, abc.ABC):
The result will be cached after its first use. The result will be cached after its first use.
""" """
if self._reply_message is None: if self._reply_message is None and self._client:
if not self.reply_to_msg_id: if not self.reply_to_msg_id:
return None return None
@ -614,8 +614,9 @@ class Message(ChatGetter, SenderGetter, TLObject, abc.ABC):
`telethon.client.messages.MessageMethods.send_message` `telethon.client.messages.MessageMethods.send_message`
with ``entity`` already set. with ``entity`` already set.
""" """
return await self._client.send_message( if self._client:
await self.get_input_chat(), *args, **kwargs) return await self._client.send_message(
await self.get_input_chat(), *args, **kwargs)
async def reply(self, *args, **kwargs): async def reply(self, *args, **kwargs):
""" """
@ -623,9 +624,10 @@ class Message(ChatGetter, SenderGetter, TLObject, abc.ABC):
`telethon.client.messages.MessageMethods.send_message` `telethon.client.messages.MessageMethods.send_message`
with both ``entity`` and ``reply_to`` already set. with both ``entity`` and ``reply_to`` already set.
""" """
kwargs['reply_to'] = self.id if self._client:
return await self._client.send_message( kwargs['reply_to'] = self.id
await self.get_input_chat(), *args, **kwargs) return await self._client.send_message(
await self.get_input_chat(), *args, **kwargs)
async def forward_to(self, *args, **kwargs): async def forward_to(self, *args, **kwargs):
""" """
@ -637,9 +639,10 @@ class Message(ChatGetter, SenderGetter, TLObject, abc.ABC):
this `forward_to` method. Use a this `forward_to` method. Use a
`telethon.client.telegramclient.TelegramClient` instance directly. `telethon.client.telegramclient.TelegramClient` instance directly.
""" """
kwargs['messages'] = self.id if self._client:
kwargs['from_peer'] = await self.get_input_chat() kwargs['messages'] = self.id
return await self._client.forward_messages(*args, **kwargs) kwargs['from_peer'] = await self.get_input_chat()
return await self._client.forward_messages(*args, **kwargs)
async def edit(self, *args, **kwargs): async def edit(self, *args, **kwargs):
""" """
@ -662,7 +665,7 @@ class Message(ChatGetter, SenderGetter, TLObject, abc.ABC):
This is generally the most desired and convenient behaviour, This is generally the most desired and convenient behaviour,
and will work for link previews and message buttons. and will work for link previews and message buttons.
""" """
if self.fwd_from or not self.out: if self.fwd_from or not self.out or not self._client:
return None # We assume self.out was patched for our chat return None # We assume self.out was patched for our chat
if 'link_preview' not in kwargs: if 'link_preview' not in kwargs:
@ -688,10 +691,11 @@ class Message(ChatGetter, SenderGetter, TLObject, abc.ABC):
this `delete` method. Use a this `delete` method. Use a
`telethon.client.telegramclient.TelegramClient` instance directly. `telethon.client.telegramclient.TelegramClient` instance directly.
""" """
return await self._client.delete_messages( if self._client:
await self.get_input_chat(), [self.id], return await self._client.delete_messages(
*args, **kwargs await self.get_input_chat(), [self.id],
) *args, **kwargs
)
async def download_media(self, *args, **kwargs): async def download_media(self, *args, **kwargs):
""" """
@ -699,7 +703,8 @@ class Message(ChatGetter, SenderGetter, TLObject, abc.ABC):
for `telethon.client.downloads.DownloadMethods.download_media` for `telethon.client.downloads.DownloadMethods.download_media`
with the ``message`` already set. with the ``message`` already set.
""" """
return await self._client.download_media(self, *args, **kwargs) if self._client:
return await self._client.download_media(self, *args, **kwargs)
async def click(self, i=None, j=None, async def click(self, i=None, j=None,
*, text=None, filter=None, data=None): *, text=None, filter=None, data=None):
@ -750,6 +755,9 @@ class Message(ChatGetter, SenderGetter, TLObject, abc.ABC):
that if the message does not have this data, it will that if the message does not have this data, it will
``raise DataInvalidError``. ``raise DataInvalidError``.
""" """
if not self._client:
return
if data: if data:
if not await self.get_input_chat(): if not await self.get_input_chat():
return None return None
@ -804,6 +812,9 @@ class Message(ChatGetter, SenderGetter, TLObject, abc.ABC):
Re-fetches this message to reload the sender and chat entities, Re-fetches this message to reload the sender and chat entities,
along with their input versions. along with their input versions.
""" """
if not self._client:
return
try: try:
chat = await self.get_input_chat() if self.is_channel else None chat = await self.get_input_chat() if self.is_channel else None
msg = await self._client.get_messages(chat, ids=self.id) msg = await self._client.get_messages(chat, ids=self.id)
@ -828,7 +839,7 @@ class Message(ChatGetter, SenderGetter, TLObject, abc.ABC):
""" """
Helper methods to set the buttons given the input sender and chat. Helper methods to set the buttons given the input sender and chat.
""" """
if isinstance(self.reply_markup, ( if self._client and isinstance(self.reply_markup, (
types.ReplyInlineMarkup, types.ReplyKeyboardMarkup)): types.ReplyInlineMarkup, types.ReplyKeyboardMarkup)):
self._buttons = [[ self._buttons = [[
MessageButton(self._client, button, chat, bot, self.id) MessageButton(self._client, button, chat, bot, self.id)
@ -844,7 +855,7 @@ class Message(ChatGetter, SenderGetter, TLObject, abc.ABC):
to know what bot we want to start. Raises ``ValueError`` if the bot to know what bot we want to start. Raises ``ValueError`` if the bot
cannot be found but is needed. Returns ``None`` if it's not needed. cannot be found but is needed. Returns ``None`` if it's not needed.
""" """
if not isinstance(self.reply_markup, ( if self._client and not isinstance(self.reply_markup, (
types.ReplyInlineMarkup, types.ReplyKeyboardMarkup)): types.ReplyInlineMarkup, types.ReplyKeyboardMarkup)):
return None return None

View File

@ -51,7 +51,7 @@ class SenderGetter(abc.ABC):
Note that this might not be available if the library can't Note that this might not be available if the library can't
find the input chat, or if the message a broadcast on a channel. find the input chat, or if the message a broadcast on a channel.
""" """
if self._input_sender is None and self._sender_id: if self._input_sender is None and self._sender_id and self._client:
try: try:
self._input_sender = self._client.session\ self._input_sender = self._client.session\
.get_input_entity(self._sender_id) .get_input_entity(self._sender_id)
@ -64,7 +64,7 @@ class SenderGetter(abc.ABC):
Returns `input_sender`, but will make an API call to find the Returns `input_sender`, but will make an API call to find the
input sender unless it's already cached. input sender unless it's already cached.
""" """
if self.input_sender is None and self._sender_id: if self.input_sender is None and self._sender_id and self._client:
await self._refetch_sender() await self._refetch_sender()
return self._input_sender return self._input_sender