|
| 1 | +# Development Guide |
| 2 | + |
| 3 | +## Quick Start |
| 4 | + |
| 5 | +**⚠️ IMPORTANT: Always use Mise. Never use npm or bun run directly.** |
| 6 | + |
| 7 | +```bash |
| 8 | +# Install dependencies |
| 9 | +mise run install |
| 10 | + |
| 11 | +# Start development server with auto-rebuild |
| 12 | +mise run dev |
| 13 | + |
| 14 | +# View all available tasks |
| 15 | +mise tasks |
| 16 | +``` |
| 17 | + |
| 18 | +### If You Don't Have Mise Installed |
| 19 | + |
| 20 | +```bash |
| 21 | +# Install Mise (macOS/Linux) |
| 22 | +curl https://mise.jdx.dev/install.sh | sh |
| 23 | + |
| 24 | +# Or with Homebrew |
| 25 | +brew install mise |
| 26 | + |
| 27 | +# Then follow "Quick Start" above |
| 28 | +``` |
| 29 | + |
| 30 | +## Project Structure |
| 31 | + |
| 32 | +``` |
| 33 | +src/bot/ |
| 34 | +├── bot.ts # Main Discord client & events |
| 35 | +├── api/ |
| 36 | +│ └── idleChampionsApi.ts # Game API client (query-param based) |
| 37 | +├── commands/ |
| 38 | +│ ├── setup.ts # Save user credentials |
| 39 | +│ ├── redeem.ts # Manual code redemption |
| 40 | +│ ├── inventory.ts # Show account info |
| 41 | +│ ├── open.ts # Open chests |
| 42 | +│ ├── blacksmith.ts # Upgrade heroes |
| 43 | +│ ├── codes.ts # Show code history |
| 44 | +│ ├── makepublic.ts # Share codes with other users |
| 45 | +│ ├── backfill.ts # Recover missed codes from history |
| 46 | +│ └── help.ts # Command help |
| 47 | +├── database/ |
| 48 | +│ ├── db.ts # SQLite connection & queries |
| 49 | +│ ├── userManager.ts # User credentials storage |
| 50 | +│ ├── codeManager.ts # Code tracking & history |
| 51 | +│ └── backfillManager.ts # Backfill operations & locking |
| 52 | +├── handlers/ |
| 53 | +│ ├── codeScanner.ts # Message code detection |
| 54 | +│ └── backfillHandler.ts # Message history scanning & redemption |
| 55 | +└── utils/ |
| 56 | + └── debugLogger.ts # Response logging & cleanup |
| 57 | +
|
| 58 | +lib/ |
| 59 | +└── *.d.ts # Type definitions from game API |
| 60 | +``` |
| 61 | + |
| 62 | +## Key Technologies |
| 63 | + |
| 64 | +- **Mise** - Task runner and tool version manager (MANDATORY) |
| 65 | +- **Bun 1.3.9** - JavaScript runtime (3-4x faster than Node.js) |
| 66 | +- **discord.js 14.26** - Discord bot framework |
| 67 | +- **sqlite3** - Local database |
| 68 | +- **node-fetch** - HTTP client (for game API) |
| 69 | +- **TypeScript** - Type-safe development |
| 70 | + |
| 71 | +## Important Notes |
| 72 | + |
| 73 | +### SSL Certificate Issue |
| 74 | + |
| 75 | +The Idle Champions API server has an expired SSL certificate. Always start the bot with: |
| 76 | + |
| 77 | +```bash |
| 78 | +NODE_TLS_REJECT_UNAUTHORIZED=0 |
| 79 | +``` |
| 80 | + |
| 81 | +### Instance ID Problem |
| 82 | + |
| 83 | +When calling game APIs (redeem, open chests, blacksmith), you must: |
| 84 | + |
| 85 | +1. Fetch fresh user details via `getUserDetails()` |
| 86 | +2. Extract `instance_id` from `details.instance_id` |
| 87 | +3. Pass it to the API call |
| 88 | + |
| 89 | +This prevents "Outdated instance id" errors from the server. |
| 90 | + |
| 91 | +### API Pattern |
| 92 | + |
| 93 | +All game API calls use URL query parameters, not JSON body: |
| 94 | + |
| 95 | +``` |
| 96 | +POST /~idledragons/post.php?call=redeemcoupon&user_id=X&hash=Y&instance_id=Z&code=ABC |
| 97 | +``` |
| 98 | + |
| 99 | +## Building |
| 100 | + |
| 101 | +Use Mise for all build tasks: |
| 102 | + |
| 103 | +```bash |
| 104 | +# Build the project |
| 105 | +mise run build |
| 106 | + |
| 107 | +# Watch for changes and rebuild |
| 108 | +mise run watch |
| 109 | +``` |
| 110 | + |
| 111 | +## Common Tasks |
| 112 | + |
| 113 | +All tasks are run through Mise. Use `mise tasks` to see all available commands: |
| 114 | + |
| 115 | +```bash |
| 116 | +mise run install # Install dependencies |
| 117 | +mise run dev # Start development server |
| 118 | +mise run build # Build TypeScript |
| 119 | +mise run watch # Watch & rebuild on changes |
| 120 | +mise run lint # Check code quality |
| 121 | +mise run lint:fix # Auto-fix linting issues |
| 122 | +mise run audit # Check for vulnerabilities |
| 123 | +mise run clean # Clean build artifacts |
| 124 | +``` |
| 125 | + |
| 126 | +## Database |
| 127 | + |
| 128 | +SQLite database (`./data/idle.db`) with the following structure: |
| 129 | + |
| 130 | +```mermaid |
| 131 | +erDiagram |
| 132 | + USERS ||--o{ REDEEMED_CODES : "has" |
| 133 | + USERS ||--o{ PENDING_CODES : "has" |
| 134 | + USERS ||--o{ AUDIT_LOG : "generates" |
| 135 | + |
| 136 | + USERS { |
| 137 | + string discord_id PK |
| 138 | + int user_id "Idle Champions user ID" |
| 139 | + string user_hash "Idle Champions auth token" |
| 140 | + string server "game server URL" |
| 141 | + string instance_id "deprecated" |
| 142 | + datetime created_at |
| 143 | + datetime updated_at |
| 144 | + } |
| 145 | + |
| 146 | + REDEEMED_CODES { |
| 147 | + int id PK |
| 148 | + string code |
| 149 | + string discord_id FK |
| 150 | + string status |
| 151 | + json loot |
| 152 | + datetime timestamp |
| 153 | + } |
| 154 | + |
| 155 | + PENDING_CODES { |
| 156 | + int id PK |
| 157 | + string code |
| 158 | + string discord_id FK |
| 159 | + datetime added_at |
| 160 | + } |
| 161 | + |
| 162 | + AUDIT_LOG { |
| 163 | + int id PK |
| 164 | + string discord_id FK |
| 165 | + string action |
| 166 | + json details |
| 167 | + datetime timestamp |
| 168 | + } |
| 169 | + |
| 170 | + BACKFILL_OPERATIONS { |
| 171 | + int id PK |
| 172 | + string initiated_by "user ID or 'system'" |
| 173 | + datetime started_at |
| 174 | + datetime completed_at |
| 175 | + int codes_found |
| 176 | + int codes_redeemed |
| 177 | + string status "in_progress, completed, failed" |
| 178 | + } |
| 179 | +``` |
| 180 | + |
| 181 | +## Testing Commands |
| 182 | + |
| 183 | +``` |
| 184 | +/setup user_id:123456 user_hash:abc123def456... |
| 185 | +
|
| 186 | +/redeem code:TESTCODE |
| 187 | +
|
| 188 | +/inventory |
| 189 | +
|
| 190 | +/open chest_type:Gold count:5 |
| 191 | +
|
| 192 | +/blacksmith contract_type:Small hero_id:1 count:3 |
| 193 | +
|
| 194 | +/help |
| 195 | +``` |
| 196 | + |
| 197 | +## Debugging |
| 198 | + |
| 199 | +The bot saves API responses to `debug/` folder automatically: |
| 200 | + |
| 201 | +- Files older than 1 hour are deleted |
| 202 | +- Useful for troubleshooting API issues |
| 203 | +- Format: `endpoint_YYYY-MM-DDTHH-mm-ss-SSSZ.json` |
| 204 | + |
| 205 | +## Environment Variables |
| 206 | + |
| 207 | +```bash |
| 208 | +DISCORD_TOKEN # Bot token from Discord Developer Portal |
| 209 | +DISCORD_GUILD_ID # Server ID (for guild-specific commands) |
| 210 | +DISCORD_CHANNEL_ID # Channel ID (for auto code scanning) |
| 211 | +DB_PATH # Database file path (default: ./data/idle.db) |
| 212 | +NODE_ENV # development or production |
| 213 | +``` |
0 commit comments