Mayuri-Chan / pyrofork

Pyrogram fork with Adjustable web page preview, Quote Reply, Story & Topics Support, Mongodb session storage, and much more. go to https://pyrofork.wulan17.top/ for documentation.
https://pyrofork.wulan17.top
GNU Lesser General Public License v3.0
162 stars 78 forks source link

KeyError in get_peer_by_username function #64

Open troublescope opened 7 months ago

troublescope commented 7 months ago

Checklist

Description

When attempting to resolve a peer by username, the function get_peer_by_username in the storage.py module is raising a KeyError due to the absence of the key "last_update_on" in the returned result. This issue likely stems from incomplete data or an unexpected format in the database.

Steps to reproduce

  1. Attempt to resolve a peer by username.
  2. Encounter a KeyError due to the absence of "last_update_on" key in the returned result.

Code example

from pyrogram import Client
from typing import Union

async def get_chat() -> None:
    async with Client("my_account") as app:
        try:
            chat: Union[None, dict] = await app.get_chat("pir11")
            if chat:
                print(chat)
            else:
                print("Chat not found.")
        except Exception as e:
            print("An error occurred:", e)

def main() -> None:
    get_chat()

if __name__ == "__main__":
    main()

Logs

Traceback (most recent call last):
  File "<string>", line 1, in tmp
  File "/home/kai/.cache/pypoetry/virtualenvs/caligo-gKLP72oz-py3.9/lib/python3.9/site-packages/pyrogram/methods/users/get_users.py", line 60, in get_users
    user_ids = await asyncio.gather(*[self.resolve_peer(i) for i in user_ids])
  File "/home/kai/.cache/pypoetry/virtualenvs/caligo-gKLP72oz-py3.9/lib/python3.9/site-packages/pyrogram/methods/advanced/resolve_peer.py", line 84, in resolve_peer
    return await self.storage.get_peer_by_username(peer_id)
  File "caligo/core/database/storage.py", line 142, in get_peer_by_username
    if abs(time.time() - res["last_update_on"]) > self.USERNAME_TTL:
KeyError: 'last_update_on'