From e088fc3a4e6835939562b27a7cbe248761b9809c Mon Sep 17 00:00:00 2001 From: Lonami Exo Date: Sat, 10 Mar 2018 12:13:17 +0100 Subject: [PATCH] Add extra safety checks when getting peer ID --- telethon/utils.py | 24 ++++++++++++++++++++++++ 1 file changed, 24 insertions(+) diff --git a/telethon/utils.py b/telethon/utils.py index a9311521..8e37714e 100644 --- a/telethon/utils.py +++ b/telethon/utils.py @@ -381,6 +381,17 @@ def parse_username(username): return None, False +def _fix_peer_id(peer_id): + """ + Fixes the peer ID for chats and channels, in case the users + mix marking the ID with the ``Peer()`` constructors. + """ + peer_id = abs(peer_id) + if str(peer_id).startswith('100'): + peer_id = str(peer_id)[3:] + return int(peer_id) + + def get_peer_id(peer): """ Finds the ID of the given peer, and converts it to the "bot api" format @@ -408,6 +419,10 @@ def get_peer_id(peer): if isinstance(peer, (PeerUser, InputPeerUser)): return peer.user_id elif isinstance(peer, (PeerChat, InputPeerChat)): + # Check in case the user mixed things up to avoid blowing up + if not (0 < peer.chat_id <= 0x7fffffff): + peer.chat_id = _fix_peer_id(peer.chat_id) + return -peer.chat_id elif isinstance(peer, (PeerChannel, InputPeerChannel, ChannelFull)): if isinstance(peer, ChannelFull): @@ -416,6 +431,15 @@ def get_peer_id(peer): i = peer.id else: i = peer.channel_id + + # Check in case the user mixed things up to avoid blowing up + if not (0 < i <= 0x7fffffff): + i = _fix_peer_id(i) + if isinstance(peer, ChannelFull): + peer.id = i + else: + peer.channel_id = i + # Concat -100 through math tricks, .to_supergroup() on Madeline # IDs will be strictly positive -> log works return -(i + pow(10, math.floor(math.log10(i) + 3)))