diff --git a/telethon/client/auth.py b/telethon/client/auth.py index d143dd0e..18670c79 100644 --- a/telethon/client/auth.py +++ b/telethon/client/auth.py @@ -137,7 +137,7 @@ class AuthMethods(MessageParseMethods, UserMethods): async def _start( self, phone, password, bot_token, force_sms, code_callback, first_name, last_name, max_attempts): - if not self.is_connected(): + if not self.is_connected: await self.connect() if await self.is_user_authorized(): diff --git a/telethon/client/telegrambaseclient.py b/telethon/client/telegrambaseclient.py index e200a0e9..8de83724 100644 --- a/telethon/client/telegrambaseclient.py +++ b/telethon/client/telegrambaseclient.py @@ -432,6 +432,7 @@ class TelegramBaseClient(abc.ABC): self._updates_handle = self._loop.create_task(self._update_loop()) + @property def is_connected(self: 'TelegramClient') -> bool: """ Returns ``True`` if the user has connected. @@ -441,11 +442,11 @@ class TelegramBaseClient(abc.ABC): Example .. code-block:: python - while client.is_connected(): + while client.is_connected: await asyncio.sleep(1) """ sender = getattr(self, '_sender', None) - return sender and sender.is_connected() + return sender and sender.is_connected def disconnect(self: 'TelegramClient'): """ diff --git a/telethon/client/updates.py b/telethon/client/updates.py index bda6bac1..6a501e1d 100644 --- a/telethon/client/updates.py +++ b/telethon/client/updates.py @@ -323,7 +323,7 @@ class UpdateMethods(UserMethods): async def _update_loop(self: 'TelegramClient'): # Pings' ID don't really need to be secure, just "random" rnd = lambda: random.randrange(-2**63, 2**63) - while self.is_connected(): + while self.is_connected: try: await asyncio.wait_for( self.disconnected, timeout=60, loop=self._loop @@ -425,7 +425,7 @@ class UpdateMethods(UserMethods): ) break except Exception as e: - if not isinstance(e, asyncio.CancelledError) or self.is_connected(): + if not isinstance(e, asyncio.CancelledError) or self.is_connected: name = getattr(callback, '__name__', repr(callback)) self._log[__name__].exception('Unhandled exception on %s', name) diff --git a/telethon_examples/gui.py b/telethon_examples/gui.py index 949d1eb9..140cd248 100644 --- a/telethon_examples/gui.py +++ b/telethon_examples/gui.py @@ -228,7 +228,7 @@ class App(tkinter.Tk): """ Sends a message. Does nothing if the client is not connected. """ - if not self.cl.is_connected(): + if not self.cl.is_connected: return # The user needs to configure a chat where the message should be sent.