This guide will get your Discord Prediction Market Bot running with the complete architecture we've built, including dependency injection, logging, error handling, validation, security, and rate limiting.
- Python 3.8+ installed
- Discord Developer Account
- Supabase Account (for database)
- Basic terminal/command line knowledge
First, let's install all required packages:
# Install Python dependencies
pip install -r requirements.txt
# If you don't have pip, install it first:
# python -m ensurepip --upgradeYour project should have this structure:
discord-bot/
โโโ bot.py # Main bot entry point
โโโ main.py # New main entry point (we'll create this)
โโโ requirements.txt # Dependencies
โโโ .env # Your environment variables
โโโ config/ # Configuration management
โ โโโ __init__.py
โ โโโ settings.py # Settings classes
โ โโโ validation.py # Config validation
โโโ core/ # Core architecture components
โ โโโ __init__.py
โ โโโ container.py # Dependency injection
โ โโโ logging_manager.py # Structured logging
โ โโโ error_handler.py # Error handling
โ โโโ exceptions.py # Custom exceptions
โ โโโ validation.py # Input validation
โ โโโ rate_limiter.py # Rate limiting
โ โโโ security.py # Security middleware
โโโ cogs/ # Discord command modules
โ โโโ __init__.py
โ โโโ economy/ # Economy commands
โโโ database/ # Database layer
โโโ models/ # Data models
โโโ helpers/ # Utility functions
โโโ logs/ # Log files (auto-created)
Copy .env.example to .env and fill in your values:
cp .env.example .envEdit .env with your actual values:
# Discord Bot Token (get from Discord Developer Portal)
DISCORD_TOKEN=your_actual_discord_bot_token_here
# Database (get from Supabase Dashboard)
DATABASE_URL=postgresql://postgres.your-ref:your-password@aws-0-us-east-2.pooler.supabase.com:6543/postgres
DATABASE_SUPABASE_URL=https://your-project-ref.supabase.co
DATABASE_SUPABASE_PUBLISHABLE_KEY=sb_publishable_your_key_here
DATABASE_SUPABASE_SECRET_KEY=sb_secret_your_key_here
# Environment
ENVIRONMENT=development
DEBUG=true
LOG_LEVEL=DEBUGLet's create a new main entry point that properly initializes our architecture:
๐ฏ Step 5: Create a Simple Test Command
Let's create a simple economy command that demonstrates all our architecture features:
For now, let's create a simple database setup script:#
Before running the bot, let's test that everything is working:
# Run the test setup script
python scripts/test_setup.pyThis will test:
- โ Configuration loading
- โ Logging system with correlation IDs
- โ Dependency injection container
- โ Rate limiting
- โ Error handling
- โ Security manager
Now let's run the bot with our new architecture:
# Run the bot
python main.pyYou should see output like:
๐ฏ Discord Prediction Market Bot
==================================================
๐ Loading configuration...
โ
Configuration loaded and validated
๐ Initializing logging system...
๐๏ธ Setting up dependency injection container...
๐ค Creating bot instance...
๐ Starting bot...
[2024-01-20 10:30:15] [INFO ] [MAIN_STARTUP] Main:main:45 - ๐ Starting application
[2024-01-20 10:30:15] [INFO ] [STARTUP] PredictionMarketBot:setup_hook:67 - ๐ Starting bot setup...
โ
All services initialized
โ
All cogs loaded
โ
Slash commands synced
๐ Bot setup completed successfully!
๐ค YourBotName is ready!
Once your bot is running, test these slash commands in Discord:
- Tests the complete architecture
- Shows correlation ID tracking
- Demonstrates rate limiting
- Shows structured logging
- Tests error handling
- Intentionally triggers errors
- Tests error handling system
- Shows user-friendly error messages
- Demonstrates error logging
- Generates different log levels
- Shows structured logging
- Demonstrates correlation IDs
- Shows health of all systems
- Displays service status
- Shows error statistics
# View real-time logs
tail -f logs/discord.log
# View structured JSON logs
cat logs/discord.log | jq '.'Use the /system-status command in Discord to see:
- Service health
- Error counts
- System status
ENVIRONMENT=development
DEBUG=true
LOG_LEVEL=DEBUG
LOG_CONSOLE_COLORS=true
DISCORD_SYNC_COMMANDS=true
# Rate limiting (relaxed for development)
RATE_LIMIT_USER_REQUESTS_PER_MINUTE=30
RATE_LIMIT_USER_BETS_PER_MINUTE=10ENVIRONMENT=production
DEBUG=false
LOG_LEVEL=INFO
LOG_JSON_FORMAT=true
LOG_FILE_ENABLED=true
# Rate limiting (strict for production)
RATE_LIMIT_USER_REQUESTS_PER_MINUTE=10
RATE_LIMIT_USER_BETS_PER_MINUTE=5- Check your
.envfile exists - Verify all required variables are set
- Run
python scripts/validate_config.py
- Check your Discord bot token in
.env - Ensure the token is correct and not expired
- Verify bot permissions in Discord Developer Portal
- Run
pip install -r requirements.txt - Check Python version (3.8+ required)
- Verify project structure matches the guide
- Check Supabase credentials in
.env - Verify database URL format
- Test connection with
python test_db_connection.py
Enable debug mode for detailed logging:
DEBUG=true
LOG_LEVEL=DEBUGEvery operation gets a unique correlation ID. Use this to track issues:
- Find the correlation ID in error messages
- Search logs for that ID:
grep "CORRELATION_ID" logs/discord.log - See the complete flow of that operation
Now that your bot is running with the complete architecture:
- Add More Commands: Create new cogs in
cogs/directory - Database Integration: Set up Supabase and add database operations
- Custom Validation: Add business logic validation rules
- Monitoring: Set up log aggregation and monitoring
- Testing: Add unit tests for your commands
- Deployment: Deploy to a cloud service
โ
Dependency Injection: Clean service management
โ
Structured Logging: JSON logs with correlation IDs
โ
Error Handling: User-friendly error messages
โ
Input Validation: Pydantic models and custom rules
โ
Rate Limiting: Prevent abuse and spam
โ
Security: Input sanitization and validation
โ
Configuration: Environment-based settings
โ
Monitoring: Health checks and error statistics
If you want to skip the individual steps, use the quick start script:
# This will validate everything and start the bot
python quick_start.pyThis script will:
- โ Check Python version
- โ Verify .env file exists
- โ Validate Discord token
- โ Install dependencies if needed
- โ Run setup validation
- โ Test architecture components
- โ Start the bot
To check your setup manually:
# Validate your setup
python scripts/validate_setup.py
# Test the architecture
python scripts/test_setup.py
# Start the bot
python main.pyYour Discord bot now has enterprise-grade architecture with:
โ
Dependency Injection: Clean service management and testability
โ
Structured Logging: JSON logs with correlation ID tracking
โ
Error Handling: User-friendly messages with unique error IDs
โ
Input Validation: Pydantic models with custom validation rules
โ
Rate Limiting: Prevent abuse with configurable limits
โ
Security: Input sanitization and injection protection
โ
Configuration: Environment-based settings with validation
โ
Monitoring: Health checks and comprehensive error statistics
- Make Changes: Edit code in your IDE
- Test Locally: Use test commands to verify functionality
- Check Logs: Monitor
logs/discord.logfor issues - Debug: Use correlation IDs to trace problems
- Deploy: Your architecture is production-ready
- Logs: Structured JSON logs with correlation IDs
- Errors: Unique error IDs for tracking issues
- Health:
/system-statuscommand shows system health - Rate Limits: Built-in protection against abuse
- Security: Automatic input sanitization
The bot is ready for real-world use and can be easily extended with new features!