import asyncio
import logging
import os
import sys
from pathlib import Path

# 添加项目路径到sys.path
sys.path.append(str(Path(__file__).parent))

from tu_bot.bot import TuBot

# 配置日志
logging.basicConfig(
    level=logging.INFO,
    format='%(asctime)s - %(name)s - %(levelname)s - %(message)s',
    handlers=[
        logging.FileHandler('bot.log'),
        logging.StreamHandler()
    ]
)

logger = logging.getLogger(__name__)

async def main():
    """主函数 - 启动Tu Bot"""
    try:
        # 读取token文件
        token_path = os.getenv('TOKEN_PATH', '/home/will/docker/tokens.txt')
        
        if not os.path.exists(token_path):
            logger.error(f"Token文件不存在: {token_path}")
            return
        
        # 初始化并启动Tu Bot
        bot = TuBot(token_path)
        await bot.start()
        
    except Exception as e:
        logger.error(f"启动失败: {e}")
        raise

if __name__ == "__main__":
    try:
        asyncio.run(main())
    except KeyboardInterrupt:
        logger.info("Bot已手动停止")
    except Exception as e:
        logger.error(f"运行错误: {e}")
        sys.exit(1)