A Discord bot featuring a sophisticated prediction market system that integrates with multiple token economies. The system follows a service-oriented architecture with clear separation of concerns.
┌─────────────────┐ ┌─────────────────┐ ┌─────────────────┐
│ Discord │ │ FoxBot UI │ │ Business Logic │
│ Interactions │────▶│ Layer │────▶│ Services │
└─────────────────┘ └─────────────────┘ └─────────────────┘
│ │
│ │
┌─────────────────┐ ┌─────────────────┐
│ Transfer & │ │ Database │
│ Economy Service │◀───▶│ Layer │
└─────────────────┘ └─────────────────┘
FoxBot/
├── bot.py # Main entry point and initialization
├── cogs/
│ ├── prediction_market.py # Command handlers and coordination
│ ├── local_economy.py # Local economy command handlers
│ ├── hackathon_economy.py # External economy integration
│ └── views/ # UI Components
│ ├── prediction_market_views.py # Market display
│ ├── betting_views.py # Betting interface
│ └── resolution_views.py # Resolution interface
├── services/
│ ├── transfer_service.py # Central token movement handler
│ ├── local_points.py # Local economy service
│ └── prediction_market.py # Market business logic
└── database/
├── models.py # SQLAlchemy models
└── database.py # Database connection handling
- Purpose: Handle Discord commands and coordinate between UI and services
- Responsibilities:
- Command registration and handling
- Input validation
- User permission checks
- Error handling and user feedback
- Coordination between UI and services
- Purpose: Manage UI components and user interactions
- Responsibilities:
- Market display and updates
- Betting interface
- Resolution interface
- Auto-updating displays
- User interaction handling
- Purpose: Implement business logic and manage data operations
- Components:
-
Transfer Service
- Central authority for token movements
- Validates and executes transfers
- Maintains transaction history
-
Prediction Market Service
- Market creation and management
- Bet placement and resolution
- Price calculations
- Liquidity management
-
Economy Services
- Token balance management
- Economy-specific operations
- Integration with external systems
-
- Purpose: Data persistence and model definitions
- Components:
- SQLAlchemy models
- Database connection management
- Transaction handling
User ─┐
│ ┌───────────┐ ┌──────────┐ ┌─────────────┐
├───▶│ View │───▶│ Cog │───▶│ PM Service │
│ └───────────┘ └──────────┘ └─────────────┘
│ │
│ ┌───────────┐ ┌──────────┐ │
└───▶│ Transfer │◀───│ Database │◀─────────┘
│ Service │ │ Layer │
└───────────┘ └──────────┘
Creator ──┐
│ ┌───────────┐ ┌──────────┐ ┌─────────────┐
├───▶│ View │───▶│ Cog │───▶│ PM Service │
│ └───────────┘ └──────────┘ └─────────────┘
│ │
│ ┌───────────┐ ┌──────────┐ │
└───▶│ Transfer │◀───│ Database │◀─────────┘
│ Service │ │ Layer │
└───────────┘ └──────────┘
-
Separation of Concerns
- UI logic stays in views
- Business logic in services
- Database operations in models
- Command handling in cogs
-
Error Handling
- Comprehensive try/except blocks
- Proper error logging
- User-friendly error messages
- Transaction rollback on failure
-
Type Safety
- Use type hints
- Validate inputs
- Document return types
- Use SQLAlchemy 2.0 typing
-
Session Management
- Use async sessions
- Proper transaction handling
- Session cleanup
- Error recovery
-
Query Optimization
- Use eager loading when appropriate
- Minimize database calls
- Index frequently queried fields
- Monitor query performance
-
View Management
- Timeout handling
- Resource cleanup
- Auto-update management
- Error recovery
-
User Experience
- Clear feedback
- Consistent styling
- Responsive updates
- Intuitive interactions
- Plan the feature across all layers
- Update models if needed
- Implement service layer logic
- Create or update UI components
- Add command handlers
- Update documentation
- Unit tests for services
- Integration tests for cogs
- UI component testing
- Database operation testing
- Error handling verification
- Clear method documentation
- Type hints and return types
- Example usage
- Error scenarios
- Architecture updates
- All token movements MUST go through Transfer Service
- Always use proper error handling
- Maintain separation of concerns
- Document all changes
- Test thoroughly
- Follow established patterns