Add new is_bot method to check if the logged-in user is a bot

This commit is contained in:
Lonami Exo 2018-12-24 17:32:34 +01:00
parent ab557a8cef
commit f16ed8235c
3 changed files with 13 additions and 0 deletions

View File

@ -351,6 +351,7 @@ class AuthMethods(MessageParseMethods, UserMethods):
Returns the input user parameter. Returns the input user parameter.
""" """
self._bot = bool(user.bot)
self._self_input_peer = utils.get_input_peer(user, allow_self=False) self._self_input_peer = utils.get_input_peer(user, allow_self=False)
self._authorized = True self._authorized = True
@ -413,6 +414,7 @@ class AuthMethods(MessageParseMethods, UserMethods):
except errors.RPCError: except errors.RPCError:
return False return False
self._bot = None
self._self_input_peer = None self._self_input_peer = None
self._authorized = False self._authorized = False
self._state = types.updates.State(0, 0, datetime.datetime.now(), 0, 0) self._state = types.updates.State(0, 0, datetime.datetime.now(), 0, 0)

View File

@ -289,6 +289,7 @@ class TelegramBaseClient(abc.ABC):
# Sometimes we need to know who we are, cache the self peer # Sometimes we need to know who we are, cache the self peer
self._self_input_peer = None self._self_input_peer = None
self._bot = None
# endregion # endregion

View File

@ -110,6 +110,7 @@ class UserMethods(TelegramBaseClient):
me = (await self( me = (await self(
functions.users.GetUsersRequest([types.InputUserSelf()])))[0] functions.users.GetUsersRequest([types.InputUserSelf()])))[0]
self._bot = me.bot
if not self._self_input_peer: if not self._self_input_peer:
self._self_input_peer = utils.get_input_peer( self._self_input_peer = utils.get_input_peer(
me, allow_self=False me, allow_self=False
@ -119,6 +120,15 @@ class UserMethods(TelegramBaseClient):
except errors.UnauthorizedError: except errors.UnauthorizedError:
return None return None
async def is_bot(self):
"""
Return ``True`` if the signed-in user is a bot, ``False`` otherwise.
"""
if self._bot is None:
self._bot = (await self.get_me()).bot
return self._bot
async def is_user_authorized(self): async def is_user_authorized(self):
""" """
Returns ``True`` if the user is authorized. Returns ``True`` if the user is authorized.