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
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:
self._input_chat = self._client._entity_cache[self._chat_peer]
except KeyError:
@ -62,7 +62,7 @@ class ChatGetter(abc.ABC):
Returns `input_chat`, but will make an API call to find the
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:
# The chat may be recent, look in dialogs
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.
"""
if self._reply_message is None:
if self._reply_message is None and self._client:
if not self.reply_to_msg_id:
return None
@ -614,6 +614,7 @@ class Message(ChatGetter, SenderGetter, TLObject, abc.ABC):
`telethon.client.messages.MessageMethods.send_message`
with ``entity`` already set.
"""
if self._client:
return await self._client.send_message(
await self.get_input_chat(), *args, **kwargs)
@ -623,6 +624,7 @@ class Message(ChatGetter, SenderGetter, TLObject, abc.ABC):
`telethon.client.messages.MessageMethods.send_message`
with both ``entity`` and ``reply_to`` already set.
"""
if self._client:
kwargs['reply_to'] = self.id
return await self._client.send_message(
await self.get_input_chat(), *args, **kwargs)
@ -637,6 +639,7 @@ class Message(ChatGetter, SenderGetter, TLObject, abc.ABC):
this `forward_to` method. Use a
`telethon.client.telegramclient.TelegramClient` instance directly.
"""
if self._client:
kwargs['messages'] = self.id
kwargs['from_peer'] = await self.get_input_chat()
return await self._client.forward_messages(*args, **kwargs)
@ -662,7 +665,7 @@ class Message(ChatGetter, SenderGetter, TLObject, abc.ABC):
This is generally the most desired and convenient behaviour,
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
if 'link_preview' not in kwargs:
@ -688,6 +691,7 @@ class Message(ChatGetter, SenderGetter, TLObject, abc.ABC):
this `delete` method. Use a
`telethon.client.telegramclient.TelegramClient` instance directly.
"""
if self._client:
return await self._client.delete_messages(
await self.get_input_chat(), [self.id],
*args, **kwargs
@ -699,6 +703,7 @@ class Message(ChatGetter, SenderGetter, TLObject, abc.ABC):
for `telethon.client.downloads.DownloadMethods.download_media`
with the ``message`` already set.
"""
if self._client:
return await self._client.download_media(self, *args, **kwargs)
async def click(self, i=None, j=None,
@ -750,6 +755,9 @@ class Message(ChatGetter, SenderGetter, TLObject, abc.ABC):
that if the message does not have this data, it will
``raise DataInvalidError``.
"""
if not self._client:
return
if data:
if not await self.get_input_chat():
return None
@ -804,6 +812,9 @@ class Message(ChatGetter, SenderGetter, TLObject, abc.ABC):
Re-fetches this message to reload the sender and chat entities,
along with their input versions.
"""
if not self._client:
return
try:
chat = await self.get_input_chat() if self.is_channel else None
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.
"""
if isinstance(self.reply_markup, (
if self._client and isinstance(self.reply_markup, (
types.ReplyInlineMarkup, types.ReplyKeyboardMarkup)):
self._buttons = [[
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
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)):
return None

View File

@ -51,7 +51,7 @@ class SenderGetter(abc.ABC):
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.
"""
if self._input_sender is None and self._sender_id:
if self._input_sender is None and self._sender_id and self._client:
try:
self._input_sender = self._client.session\
.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
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()
return self._input_sender