import asyncio
from nonebot import get_driver
from nonebot_plugin_alconna import UniMessage
from konabot.common.subscribe import register_poster_info, PosterInfo, broadcast


CHANNEL_STARTUP = "启动通知"


register_poster_info(
    CHANNEL_STARTUP,
    PosterInfo(
        aliases=set(),
        description="当 Bot 重启时告知",
    ),
)

driver = get_driver()


@driver.on_startup
async def _():
    # 要尽量保证接受讯息的服务存在
    # 所以在这里我们要等待一定时间后再发信
    async def task():
        while True:
            if len(driver.bots) >= 1:
                break
            await asyncio.sleep(15)

        # 在这个时候，需求的 bot 已经上线，再等待一小会
        await asyncio.sleep(3)
        await broadcast(CHANNEL_STARTUP, UniMessage.text("此方 BOT 重启好了"))

    asyncio.create_task(task())
