pyrofork icon indicating copy to clipboard operation
pyrofork copied to clipboard

KeyError in get_peer_by_username function

Open troublescope opened this issue 10 months ago • 0 comments

Checklist

  • [X] I am sure the error is coming from Pyrofork's code and not elsewhere
  • [X] I have searched in the issue tracker for similar bug reports, including closed ones
  • [X] I ran pip3 install -U git+https://github.com/Mayuri-Chan/pyrofork and reproduced the issue using the latest development version

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'

troublescope avatar Apr 15 '24 14:04 troublescope