mirror of
https://github.com/LonamiWebs/Telethon.git
synced 2025-01-26 17:24:38 +03:00
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:
parent
a2bd5c09ff
commit
5ca1edb228
|
@ -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(
|
||||
|
|
Loading…
Reference in New Issue
Block a user