# written by @Mega_Evolutions



"""
Rose!

CMD: {i}find <reply/user_id>
"""

from asyncio import sleep, TimeoutError

from telethon.errors.rpcerrorlist import YouBlockedUserError

from . import get_string, LOGS, ultroid_cmd


CHAT = "MissRose_bot"

@ultroid_cmd(
    pattern="find( (.*)|$)",
)
async def rose_bot(e):
    args = e.pattern_match.group(2)
    reply = await e.get_reply_message()
    if args:
        try:
            user_id = await e.client.parse_id(args)
        except ValueError:
            user_id = args
    elif reply:
        user_id = reply.sender_id
    else:
        return await e.eor("Use this command with reply or give Username/id...")

    lol = await e.eor(get_string("com_1"))
    try:
        async with e.client.conversation(CHAT, total_timeout=15) as conv:
            msg = await conv.send_message(f"/id {user_id}")
            response = await conv.get_response()
            if response:
                await lol.edit(response.text)
    except YouBlockedUserError:
        return await lol.edit(f"Please unblock @{CHAT} and try again.")
    except TimeoutError:
        await lol.edit("Bot didn't respond in time.")
    except Exception as ex:
        LOGS.exception(ex)
        await lol.edit(f"Error: {ex}")
    finally:
        await sleep(2)
        await e.client.send_read_acknowledge(CHAT)