# CLAUDE.md - PetBot Development Guide This file documents the development patterns, conventions, and best practices established for the PetBot project during AI-assisted development. ## Project Overview PetBot is a Discord/IRC Pokemon-style pet collecting bot with web interface. The project consists of: - IRC bot with modular command system - Web server with unified navigation and interactive interfaces - SQLite database with comprehensive pet, player, and game state management - Team builder with drag-and-drop functionality and PIN verification ## Development Workflow ### 1. Planning and Task Management - Use TodoWrite tool for complex multi-step tasks (3+ steps) - Break down large features into specific, actionable items - Mark todos as `in_progress` before starting work - Complete todos immediately after finishing tasks - Don't batch completions - update status in real-time ### 2. Code Analysis and Search - Use Task tool for keyword searches and file discovery - Use Grep tool for specific code patterns and function definitions - Use Read tool for examining specific file content - Use Glob tool for finding files by name patterns ### 3. Testing and Validation - Always run `python3 -c "import webserver; print('✅ syntax check')"` after webserver changes - Test database operations with simple validation scripts - Check IRC bot functionality with `python3 run_bot_debug.py` - Verify web interface functionality through browser testing ## Project Structure ``` /home/megaproxy/petbot/ ├── src/ # Core system components │ ├── database.py # Database operations and schema │ ├── game_engine.py # Game mechanics and weather system │ └── bot.py # IRC bot core functionality ├── modules/ # Command modules (modular architecture) │ ├── base_module.py # Abstract base class for all modules │ ├── core_commands.py # Basic bot commands (!start, !help, !stats) │ ├── exploration.py # !explore, !travel, !weather commands │ ├── battle_system.py # !battle, !attack, !moves, !flee commands │ ├── pet_management.py # !pets, !activate, !deactivate commands │ ├── inventory.py # !inventory, !use commands (redirects to web) │ ├── gym_battles.py # !gym commands and gym mechanics │ ├── achievements.py # Achievement tracking and validation │ ├── admin.py # Administrative commands │ └── team_builder.py # Team builder module (web-only) ├── webserver.py # Web server with unified templates ├── run_bot_debug.py # Bot startup and debug mode ├── help.html # Static help documentation ├── CHANGELOG.md # Version history and feature tracking └── README.md # Project documentation ``` ## Coding Conventions ### 1. Module Architecture - All command modules inherit from `BaseModule` - Implement `get_commands()` and `handle_command()` methods - Use `self.normalize_input()` for case-insensitive command processing - Use `self.send_message()` for IRC responses - Use `await self.require_player()` for player validation ### 2. Database Operations - All database methods are async - Use proper error handling with try/catch blocks - Always use parameterized queries to prevent SQL injection - Implement proper transaction handling for complex operations - Use `team_order` column for numbered team slots (1-6) ### 3. Web Interface Development - Use unified template system with `get_page_template()` - Implement CSS variables for consistent theming - Add proper navigation bar to all pages - Use center alignment: `max-width: 1200px; margin: 0 auto` - Implement drag-and-drop with proper event handlers - Add double-click backup methods for accessibility ### 4. Security Practices - Use PIN verification for sensitive operations (team changes) - Send PINs via IRC private messages - Implement proper session validation - Never log or expose sensitive data - Use parameterized database queries ## Key Development Patterns ### 1. Command Redirection Pattern ```python async def cmd_inventory(self, channel, nickname): """Redirect player to their web profile for inventory management""" player = await self.require_player(channel, nickname) if not player: return # Redirect to web interface for better experience self.send_message(channel, f"🎒 {nickname}: View your complete inventory at: http://petz.rdx4.com/player/{nickname}#inventory") ``` ### 2. State Management Pattern ```python async def cmd_explore(self, channel, nickname): # Check if player has an active encounter that must be resolved first if player["id"] in self.bot.active_encounters: current_encounter = self.bot.active_encounters[player["id"]] self.send_message(channel, f"{nickname}: You must resolve your active encounter first!") return ``` ### 3. Drag-and-Drop Implementation Pattern ```javascript // Initialize when DOM is ready document.addEventListener('DOMContentLoaded', function() { initializeTeamBuilder(); }); function initializeTeamBuilder() { // Initialize drag and drop initializeDragAndDrop(); // Initialize double-click backup initializeDoubleClick(); // Update save button state updateSaveButton(); } ``` ### 4. PIN Verification Pattern ```python async def saveTeam(): # Send team data to server response = await fetch('/teambuilder/{nickname}/save', { method: 'POST', headers: { 'Content-Type': 'application/json' }, body: JSON.stringify(teamData) }); # PIN sent via IRC, show verification interface if (result.success) { document.getElementById('pin-section').style.display = 'block'; } ``` ## Database Schema Conventions ### 1. Player Management - `players` table with basic player information - `pets` table with `team_order` column for numbered slots (1-6) - `player_items` table for inventory management - `pending_team_changes` table for PIN verification ### 2. Game State Management - Use `is_active` boolean for team membership - Use `team_order` (1-6) for slot positioning - Store encounter state in bot memory, not database - Use timestamps for PIN expiration ## Web Interface Conventions ### 1. Unified Template System ```python def get_page_template(self, title, content, active_page): return f"""