#!/bin/bash

# Discord Bot 启动脚本

echo "🚀 Discord Bot 启动脚本"
echo "========================"

# 检查Python版本
python3 --version

# 检查必要文件
echo "📋 检查必要文件..."
if [ ! -f "../tokens.txt" ]; then
    echo "❌ 错误: 找不到 ~/docker/tokens.txt 文件"
    echo "   请确保token文件存在并包含Discord Bot tokens"
    exit 1
fi

if [ ! -f "requirements.txt" ]; then
    echo "❌ 错误: 找不到 requirements.txt 文件"
    exit 1
fi

# 创建虚拟环境(可选)
if [ "$1" = "--venv" ]; then
    echo "🐍 创建Python虚拟环境..."
    python3 -m venv venv
    source venv/bin/activate
    echo "✅ 虚拟环境已激活"
fi

# 安装依赖
echo "📦 安装Python依赖..."
pip install -r requirements.txt

# 初始化数据库
echo "🗄️ 初始化数据库..."
python3 init_db.py

# 运行测试(可选)
if [ "$1" = "--test" ] || [ "$2" = "--test" ]; then
    echo "🧪 运行功能测试..."
    python3 test_bot.py
fi

# 询问是否启动bot
echo ""
read -p "是否启动Discord Bot? (y/n): " -n 1 -r
echo
if [[ $REPLY =~ ^[Yy]$ ]]; then
    echo "🤖 启动Discord Bot..."
    python3 main.py
else
    echo "👋 启动脚本完成，手动运行: python3 main.py"
fi