Files
blog/write-server/diagnose-bot.sh
T

109 lines
3.2 KiB
Bash
Raw Normal View History

2026-06-24 13:52:16 +08:00
#!/bin/bash
# 诊断 Bot 启动问题
echo ""
echo "==================================="
echo " Bot 启动诊断"
echo "==================================="
echo ""
# 1. 检查 instrumentation 文件
echo "1. 检查 instrumentation 文件..."
if [ -f "src/instrumentation.ts" ]; then
echo "✓ src/instrumentation.ts 存在"
elif [ -f "src/instrumentation.js" ]; then
echo "✓ src/instrumentation.js 存在"
elif [ -f "instrumentation.ts" ]; then
echo "✓ instrumentation.ts 存在(根目录)"
elif [ -f "instrumentation.js" ]; then
echo "✓ instrumentation.js 存在(根目录)"
else
echo "✗ 未找到 instrumentation 文件!"
echo " 应该在: src/instrumentation.ts 或 instrumentation.ts"
exit 1
fi
# 2. 检查 register 函数导出
echo ""
echo "2. 检查 register 函数..."
if grep -q "export async function register" src/instrumentation.ts 2>/dev/null || \
grep -q "export async function register" src/instrumentation.js 2>/dev/null || \
grep -q "export async function register" instrumentation.ts 2>/dev/null || \
grep -q "export async function register" instrumentation.js 2>/dev/null; then
echo "✓ register 函数已导出"
else
echo "✗ 未找到 export async function register() 导出"
fi
# 3. 检查 next.config.ts
echo ""
echo "3. 检查 next.config.ts..."
if grep -q "instrumentationHook" next.config.ts 2>/dev/null; then
echo "⚠ 发现 instrumentationHook 配置(Next.js 16 不需要,应该移除)"
else
echo "✓ 配置文件正常"
fi
# 4. 检查 bot 入口文件
echo ""
echo "4. 检查 bot 启动文件..."
if [ -f "src/lib/bot/index.ts" ]; then
echo "✓ src/lib/bot/index.ts 存在"
if grep -q "export async function startBot" src/lib/bot/index.ts; then
echo "✓ startBot 函数已导出"
else
echo "✗ 未找到 startBot 导出"
fi
else
echo "✗ src/lib/bot/index.ts 不存在"
fi
# 5. 检查环境变量
echo ""
echo "5. 检查环境变量..."
if [ -f ".env" ]; then
if grep -q "TG_BOT_TOKEN=" .env && ! grep -q "TG_BOT_TOKEN=$" .env; then
echo "✓ TG_BOT_TOKEN 已配置"
else
echo "⚠ TG_BOT_TOKEN 未配置或为空"
fi
if grep -q "TG_ALLOWED_CHAT_IDS=" .env && ! grep -q "TG_ALLOWED_CHAT_IDS=$" .env; then
echo "✓ TG_ALLOWED_CHAT_IDS 已配置"
else
echo "⚠ TG_ALLOWED_CHAT_IDS 未配置或为空"
fi
if grep -q "ARTALK_SERVER=" .env && ! grep -q "ARTALK_SERVER=$" .env; then
echo "✓ ARTALK_SERVER 已配置"
else
echo "⚠ ARTALK_SERVER 未配置或为空"
fi
else
echo "⚠ .env 文件不存在"
fi
# 6. 检查容器状态
echo ""
echo "6. 检查容器状态..."
if command -v docker &> /dev/null; then
if docker compose ps 2>/dev/null | grep -q "Up"; then
echo "✓ 容器正在运行"
echo ""
echo "7. 检查容器日志..."
echo "--- 最近的日志 ---"
docker compose logs --tail 30 2>&1 | grep -E "\[instrumentation\]|\[bot\]|\[comment-notify\]|error" || echo "未找到相关日志"
else
echo "✗ 容器未运行"
fi
else
echo "⚠ docker 命令不可用"
fi
echo ""
echo "==================================="
echo " 诊断完成"
echo "==================================="
echo ""