Update get_raw_peer_id and get_peer_id

This commit is contained in:
KurimuzonAkuma 2023-12-01 17:24:55 +03:00
parent 97aeee2316
commit a4b92aabd1
5 changed files with 13 additions and 28 deletions

View File

@ -79,7 +79,7 @@ class GetPinnedStories:
chats = {i.id: i for i in r.chats}
if isinstance(peer, raw.types.InputPeerChannel):
peer_id = utils.get_input_peer_id(peer)
peer_id = utils.get_raw_peer_id(peer)
if peer_id not in r.chats:
channel = await self.invoke(raw.functions.channels.GetChannels(id=[peer]))
chats.update({peer_id: channel.chats[0]})

View File

@ -193,9 +193,9 @@ class Story(Object, Update):
peer_id = r[0].id
users.update({i.id: i for i in r})
elif isinstance(peer, raw.types.InputPeerUser):
peer_id = utils.get_input_peer_id(peer)
peer_id = utils.get_raw_peer_id(peer)
elif isinstance(peer, raw.types.InputPeerChannel):
peer_id = utils.get_input_peer_id(peer)
peer_id = utils.get_raw_peer_id(peer)
if peer_id not in chats:
r = await client.invoke(raw.functions.channels.GetChannels(id=[peer]))
chats.update({peer_id: r.chats[0]})

View File

@ -67,8 +67,6 @@ class StoryDeleted(Object, Update):
r = await client.invoke(raw.functions.users.GetUsers(id=[raw.types.InputPeerSelf()]))
peer_id = r[0].id
users.update({i.id: i for i in r})
elif isinstance(peer, (raw.types.InputPeerUser, raw.types.InputPeerChannel)):
peer_id = utils.get_input_peer_id(peer)
else:
peer_id = utils.get_raw_peer_id(peer)

View File

@ -83,8 +83,6 @@ class StorySkipped(Object, Update):
r = await client.invoke(raw.functions.users.GetUsers(id=[raw.types.InputPeerSelf()]))
peer_id = r[0].id
users.update({i.id: i for i in r})
elif isinstance(peer, (raw.types.InputPeerUser, raw.types.InputPeerChannel)):
peer_id = utils.get_input_peer_id(peer)
else:
peer_id = utils.get_raw_peer_id(peer)

View File

@ -268,42 +268,29 @@ MAX_USER_ID_OLD = 2147483647
MAX_USER_ID = 999999999999
def get_raw_peer_id(peer: raw.base.Peer) -> Optional[int]:
def get_raw_peer_id(peer: Union[raw.base.Peer, raw.base.InputPeer]) -> Optional[int]:
"""Get the raw peer id from a Peer object"""
if isinstance(peer, raw.types.PeerUser):
if isinstance(peer, (raw.types.PeerUser, raw.types.InputPeerUser)):
return peer.user_id
if isinstance(peer, raw.types.PeerChat):
if isinstance(peer, (raw.types.PeerChat, raw.types.InputPeerChat)):
return peer.chat_id
if isinstance(peer, raw.types.PeerChannel):
return peer.channel_id
return None
def get_input_peer_id(peer: raw.base.InputPeer) -> Optional[int]:
"""Get the raw peer id from a InputPeer object"""
if isinstance(peer, raw.types.InputPeerUser):
return peer.user_id
if isinstance(peer, raw.types.InputPeerChat):
return peer.chat_id
if isinstance(peer, raw.types.InputPeerChannel):
if isinstance(peer, (raw.types.PeerChannel, raw.types.InputPeerChannel)):
return peer.channel_id
return None
def get_peer_id(peer: raw.base.Peer) -> int:
def get_peer_id(peer: Union[raw.base.Peer, raw.base.InputPeer]) -> int:
"""Get the non-raw peer id from a Peer object"""
if isinstance(peer, raw.types.PeerUser):
if isinstance(peer, (raw.types.PeerUser, raw.types.InputPeerUser)):
return peer.user_id
if isinstance(peer, raw.types.PeerChat):
if isinstance(peer, (raw.types.PeerChat, raw.types.InputPeerChat)):
return -peer.chat_id
if isinstance(peer, raw.types.PeerChannel):
if isinstance(peer, (raw.types.PeerChannel, raw.types.InputPeerChannel)):
return MAX_CHANNEL_ID - peer.channel_id
raise ValueError(f"Peer type invalid: {peer}")
@ -321,6 +308,7 @@ def get_peer_type(peer_id: int) -> str:
raise ValueError(f"Peer id invalid: {peer_id}")
def get_reply_to(
reply_to_message_id: Optional[int] = None,
message_thread_id: Optional[int] = None,
@ -346,6 +334,7 @@ def get_reply_to(
return None
def get_channel_id(peer_id: int) -> int:
return MAX_CHANNEL_ID - peer_id