# " Made by @Mega_Evolutions for Ultroid "
# < https://github.com/TeamUltroid/Ultroid >

"""
✘ **USES :**
> Get Imformation About How Long A Message Is Old.

✘ **Commands :**
>>> `{i}when <as reply to message>`
"""

import asyncio
from telethon import events
from telethon.errors.rpcerrorlist import YouBlockedUserError
from telethon.tl.functions.account import UpdateNotifySettingsRequest

from . import *


chat1 = "@CalsiBot" # [Heroku]


@ultroid_cmd(pattern="when")
async def get_a_link_tg(getLink):
    'Get Direct Links!'
    if getLink.fwd_from:
        return
    reply = await getLink.get_reply_message()
    if not (reply):
        return await eod(
            getLink, "Reply to a Message."
        )
    eris = await eor(getLink, "• • •")
    xlink = "{}"
    d_link = await gimme(reply, chat1)
    if not d_link.startswith('https://'):
        return await eris.edit(xlink.format(d_link))
    else:
        return await eris.edit(d_link)


async def gimme(file, chat):
    async with ultroid.conversation(chat, timeout=2) as conv:
        try:
            ans = conv.wait_event(events.NewMessage(
                incoming=True, from_users=chat,
            ))
            fwd_msg = await file.forward_to(chat)
            await fwd_msg.reply('/when')
            res = await ans
        except YouBlockedUserError:
            return f"Sar, Unblock {chat} ..."
        except asyncio.TimeoutError:
            return "Bot is Down, Try again in some time ..."
        except Exception as ex:
            return str(ex)
    await ultroid.send_read_acknowledge(chat)
    return res.message.message