mirror of
https://github.com/LonamiWebs/Telethon.git
synced 2025-01-27 09:44:35 +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'
|
raise ValueError('No user has "{}" as username'
|
||||||
.format(username)) from e
|
.format(username)) from e
|
||||||
|
|
||||||
for entity in itertools.chain(result.users, result.chats):
|
try:
|
||||||
if getattr(entity, 'username', None) or '' \
|
pid = utils.get_peer_id(result.peer, add_mark=False)
|
||||||
.lower() == username:
|
if isinstance(result.peer, types.PeerUser):
|
||||||
return entity
|
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:
|
try:
|
||||||
# Nobody with this username, maybe it's an exact name/title
|
# Nobody with this username, maybe it's an exact name/title
|
||||||
return await self.get_entity(
|
return await self.get_entity(
|
||||||
|
|
Loading…
Reference in New Issue
Block a user