Return the correct entity from .get_entity(username)

The precedence of the or operator made the check succeed always
out of pure luck, since `''.lower()` would never be the chosen
username, but a present username is truthy.

This presumably worked because Telegram only returns one result
from the call, or puts the right entity the first one.
This commit is contained in:
Lonami Exo 2018-08-26 12:00:17 +02:00
parent a2bd5c09ff
commit 5ca1edb228

View File

@ -381,10 +381,14 @@ class UserMethods(TelegramBaseClient):
raise ValueError('No user has "{}" as username'
.format(username)) from e
for entity in itertools.chain(result.users, result.chats):
if getattr(entity, 'username', None) or '' \
.lower() == username:
return entity
try:
pid = utils.get_peer_id(result.peer, add_mark=False)
if isinstance(result.peer, types.PeerUser):
return next(x for x in result.users if x.id == pid)
else:
return next(x for x in result.chats if x.id == pid)
except StopIteration:
pass
try:
# Nobody with this username, maybe it's an exact name/title
return await self.get_entity(