Petbot/install_prerequisites_simple.sh
megaproxy 915aa00bea Implement comprehensive rate limiting system and item spawn configuration
Major Features Added:
- Complete token bucket rate limiting for IRC commands and web interface
- Per-user rate tracking with category-based limits (Basic, Gameplay, Management, Admin, Web)
- Admin commands for rate limit management (\!rate_stats, \!rate_user, \!rate_unban, \!rate_reset)
- Automatic violation tracking and temporary bans with cleanup
- Global item spawn multiplier system with 75% spawn rate reduction
- Central admin configuration system (config.py)
- One-command bot startup script (start_petbot.sh)

Rate Limiting:
- Token bucket algorithm with burst capacity and refill rates
- Category limits: Basic (20/min), Gameplay (10/min), Management (5/min), Web (60/min)
- Graceful violation handling with user-friendly error messages
- Admin exemption and override capabilities
- Background cleanup of old violations and expired bans

Item Spawn System:
- Added global_spawn_multiplier to config/items.json for easy adjustment
- Reduced all individual spawn rates by 75% (multiplied by 0.25)
- Admins can fine-tune both global multiplier and individual item rates
- Game engine integration applies multiplier to all spawn calculations

Infrastructure:
- Single admin user configuration in config.py
- Enhanced startup script with dependency management and verification
- Updated documentation and help system with rate limiting guide
- Comprehensive test suite for rate limiting functionality

Security:
- Rate limiting protects against command spam and abuse
- IP-based tracking for web interface requests
- Proper error handling and status codes (429 for rate limits)

🤖 Generated with [Claude Code](https://claude.ai/code)

Co-Authored-By: Claude <noreply@anthropic.com>
2025-07-15 20:10:43 +00:00

200 lines
No EOL
5.1 KiB
Bash
Executable file

#!/bin/bash
# PetBot Prerequisites Installation Script (Simple - fixes externally-managed-environment)
set -e # Exit on any error
echo "============================================================"
echo "🐾 PetBot Prerequisites Installation (Simple Fix)"
echo "============================================================"
echo
# Colors
GREEN='\033[0;32m'
RED='\033[0;31m'
YELLOW='\033[0;33m'
NC='\033[0m'
print_success() { echo -e "${GREEN}${NC} $1"; }
print_error() { echo -e "${RED}${NC} $1"; }
print_warning() { echo -e "${YELLOW}⚠️${NC} $1"; }
echo "🔍 Checking Python and venv..."
# Check Python
if ! command -v python3 &> /dev/null; then
print_error "Python 3 is not installed"
exit 1
fi
python_version=$(python3 --version)
print_success "Python available: $python_version"
# Check venv
if ! python3 -c "import venv" 2>/dev/null; then
print_error "python3-venv is not available"
echo "Install with: sudo apt install python3-venv"
exit 1
fi
print_success "venv module is available"
echo
echo "🔧 Setting up virtual environment..."
# Create virtual environment if it doesn't exist
if [ ! -d "venv" ]; then
python3 -m venv venv
print_success "Virtual environment created"
else
print_success "Virtual environment already exists"
fi
# Activate virtual environment
source venv/bin/activate
print_success "Virtual environment activated"
echo
echo "📦 Installing packages in virtual environment..."
# Upgrade pip
pip install --upgrade pip
# Install packages
if [ -f "requirements.txt" ]; then
pip install -r requirements.txt
print_success "Installed from requirements.txt"
else
# Install individual packages
pip install "irc>=20.3.0" "aiosqlite>=0.19.0" "python-dotenv>=1.0.0"
print_success "Installed individual packages"
fi
echo
echo "🔍 Verifying installation..."
# Test imports
python -c "import irc, aiosqlite, dotenv, asyncio, sqlite3; print('All modules imported successfully')"
print_success "All required modules are available"
echo
echo "🔧 Creating directories..."
# Create required directories
mkdir -p data backups logs
print_success "Created required directories"
echo
echo "🔧 Creating helper scripts..."
# Create activation script
cat > activate_petbot.sh << 'EOF'
#!/bin/bash
echo "🐾 Activating PetBot virtual environment..."
if [ -d "venv" ]; then
source venv/bin/activate
echo "✅ Virtual environment activated"
echo "🚀 You can now run:"
echo " python run_bot_with_reconnect.py"
echo " python test_backup_simple.py"
echo " python test_reconnection.py"
echo ""
echo "💡 To deactivate: deactivate"
else
echo "❌ Virtual environment not found"
fi
EOF
# Create run script
cat > run_petbot.sh << 'EOF'
#!/bin/bash
if [ -d "venv" ]; then
source venv/bin/activate
echo "🚀 Starting PetBot with auto-reconnect..."
python run_bot_with_reconnect.py
else
echo "❌ Virtual environment not found"
echo "Run: ./install_prerequisites_simple.sh"
fi
EOF
# Create debug run script
cat > run_petbot_debug.sh << 'EOF'
#!/bin/bash
if [ -d "venv" ]; then
source venv/bin/activate
echo "🚀 Starting PetBot in debug mode..."
python run_bot_debug.py
else
echo "❌ Virtual environment not found"
echo "Run: ./install_prerequisites_simple.sh"
fi
EOF
# Create test script
cat > test_petbot.sh << 'EOF'
#!/bin/bash
if [ -d "venv" ]; then
source venv/bin/activate
echo "🧪 Running PetBot tests..."
echo "1. Testing backup system..."
python test_backup_simple.py
echo
echo "2. Testing reconnection system..."
python test_reconnection.py
else
echo "❌ Virtual environment not found"
echo "Run: ./install_prerequisites_simple.sh"
fi
EOF
# Make scripts executable
chmod +x activate_petbot.sh run_petbot.sh run_petbot_debug.sh test_petbot.sh
print_success "Created helper scripts"
echo
echo "🧪 Testing installation..."
# Test basic imports
python -c "
import sys, os
sys.path.append('.')
from src.database import Database
from src.irc_connection_manager import IRCConnectionManager
from src.backup_manager import BackupManager
print('✅ All project modules imported successfully')
"
print_success "Installation test completed"
# Deactivate for clean finish
deactivate
echo
echo "🎉 Installation completed successfully!"
echo
echo "📋 How to use PetBot:"
echo
echo "🔧 Method 1: Using wrapper scripts (recommended)"
echo " ./run_petbot.sh # Start bot with auto-reconnect"
echo " ./run_petbot_debug.sh # Start bot in debug mode"
echo " ./test_petbot.sh # Run tests"
echo
echo "🔧 Method 2: Manual activation"
echo " source venv/bin/activate"
echo " python run_bot_with_reconnect.py"
echo " deactivate # when done"
echo
echo "🔧 Method 3: Using activation helper"
echo " ./activate_petbot.sh"
echo " # Then run commands normally"
echo
echo "🌐 Web Interface:"
echo " http://localhost:8080 # Access after starting bot"
echo
echo "⚠️ Important:"
echo " - Always use the wrapper scripts OR activate venv first"
echo " - The virtual environment is in the 'venv' directory"
echo " - This fixes the externally-managed-environment issue"
echo
echo "✅ Ready to run PetBot!"