hikariatama / Hikka

The most fresh and updateable developer-oriented Telegram userbot
https://hikka.pw
GNU Affero General Public License v3.0
315 stars 506 forks source link

Could not find input entity due to get_perms_cached #113

Closed Shrimp6387 closed 12 months ago

Shrimp6387 commented 1 year ago

https://github.com/hikariatama/Hikka/blob/ce1f24f03313f8500de671815dde065fc8d86897/hikka/tl_cache.py#L225

Expected behavior Check if the User is actually an Entity or an Int.

Screenshots If applicable, add screenshots to help explain your problem.

Additional context Something like that would fix it.

user = (
    await self._client.get_entity(user)
    if user and isinstance(user, int)
    else user
)
hikariatama commented 12 months ago

If the user is unknown to telethon, you won't be able to change he's permissions whatsoever. If you want to avoid this error, you should fetch entity with await message.get_sender() beforehand

anon97945 commented 12 months ago

i do.

my code currently looks like this below, as u can see, i'm sending an User Entity already but it will refetch it from the entity again. you should not refetch the user entity if i already send an entity.

user = await message.get_sender()

if isinstance(user, User)
and (perms := await self.apo_lib.utils.is_member(chat, user))
and perms.is_admin:

This is my is_memberwhich calls your get_perms_cached

async def is_member(
    self,
    chat: EntityLike,
    user: Optional[EntityLike] = None,
    exp: Optional[int] = 5,
    force: Optional[bool] = False,
) -> bool:
    """
    Checks if a user is a member of a chat
    :param entity: Chat ID or Chat Entity
    :param user: User ID or User Entity
    :param exp: The max time of cached results in seconds
    :param force: Whether to force a refresh of the cache
    :return: perms if user is a member of the chat, None otherwise
    """
    try:
        return await self._client.get_perms_cached(
            chat,
            user,
            exp=exp,
            force=force,
        )
    except UserNotParticipantError:
        return None
hikariatama commented 12 months ago

If you acquired this entity legitimately, refetch will not harm you. It will just take the value from cache. If the entity was constructed manually \ tampered with, it will be re-fetched from Telegram servers.