Skip to content

Commit 911f412

Browse files
committed
docs: replace textual database documentation with Mermaid ER diagram
1 parent 22faefb commit 911f412

4 files changed

Lines changed: 262 additions & 4 deletions

File tree

docs/DEVELOPMENT.md

Lines changed: 213 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,213 @@
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+
```

docs/github-dependabot.md

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,36 +7,43 @@ Dependabot automatically keeps your dependencies up-to-date by creating pull req
77
The configuration in `.github/dependabot.yml` covers three ecosystems:
88

99
### 1. **npm Dependencies** (Weekly)
10+
1011
- Checks for updates every Monday at 3:00 AM UTC
1112
- Limits to 5 open PRs at a time
1213
- Groups development and runtime dependencies separately
1314
- Auto-rebases branches
1415
- Targets: TypeScript, ESLint, Bun, Discord.js, and other npm packages
1516

1617
### 2. **GitHub Actions** (Weekly)
18+
1719
- Checks for updates every Monday at 4:00 AM UTC
1820
- Auto-rebases branches
1921
- Keeps CI/CD workflows up-to-date
2022

2123
### 3. **Docker** (Weekly)
24+
2225
- Checks for base image updates every Monday at 5:00 AM UTC
2326
- Watches the `debian:13-slim` base image
2427
- Auto-rebases branches
2528

2629
## Managing Dependabot PRs
2730

2831
### Enable Dependabot
32+
2933
Dependabot is enabled by default for all public repositories. For private repos:
34+
3035
1. Go to **Settings → Code security & analysis**
3136
2. Enable **Dependabot version updates** and **Dependabot alerts**
3237

3338
### Review & Merge
39+
3440
1. **Actions tab** → View Dependabot PRs
3541
2. Review the changelog and dependencies
3642
3. Run tests (GitHub Actions will run automatically)
3743
4. Approve and merge when confident
3844

3945
### Commands
46+
4047
Add these comments to Dependabot PRs:
4148

4249
```
@@ -49,12 +56,15 @@ Add these comments to Dependabot PRs:
4956
```
5057

5158
### Configure Intervals
59+
5260
Change update frequency by modifying the `schedule.interval` in `.github/dependabot.yml`:
61+
5362
- `daily` - Check every day
5463
- `weekly` - Check every week (default)
5564
- `monthly` - Check every month
5665

5766
### Disable for Specific Packages
67+
5868
Add to your `package.json`:
5969

6070
```json
@@ -66,10 +76,11 @@ Add to your `package.json`:
6676
```
6777

6878
Or in `.github/dependabot.yml`:
79+
6980
```yaml
7081
ignore:
71-
- dependency-name: "package-name"
72-
- dependency-name: "another-package"
82+
- dependency-name: 'package-name'
83+
- dependency-name: 'another-package'
7384
```
7485
7586
## Security Updates
@@ -92,15 +103,18 @@ Dependabot will **always** create PRs for security vulnerabilities regardless of
92103
## Troubleshooting
93104
94105
**Dependabot not creating PRs:**
106+
95107
- Check it's enabled in Settings
96108
- Verify `.github/dependabot.yml` syntax
97109
- Wait up to 24 hours for first run
98110

99111
**Too many PRs:**
112+
100113
- Reduce `open-pull-requests-limit` in `.github/dependabot.yml`
101114
- Change `interval` to `monthly` instead of `weekly`
102115

103116
**Want to skip an update:**
117+
104118
- Comment `@dependabot ignore this dependency` on the PR
105119
- Or add to `ignore` list in `.github/dependabot.yml`
106120

docs/github-deployment.md

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ This workflow automatically builds and publishes Docker images to GitHub Contain
2323
Images are published to: `ghcr.io/YOUR_USERNAME/idle-code-redeemer-bot`
2424

2525
Example tags:
26+
2627
- `main-sha123456` - Commit SHA
2728
- `v2.0.0` - Semantic version
2829
- `2.0` - Major.minor version
@@ -61,7 +62,7 @@ deploy:
6162
runs-on: ubuntu-latest
6263
needs: build
6364
if: github.event_name == 'push' && github.ref == 'refs/heads/main'
64-
65+
6566
steps:
6667
- name: Deploy via SSH
6768
uses: appleboy/ssh-action@master
@@ -76,6 +77,7 @@ deploy:
7677
```
7778
7879
**Setup Required:**
80+
7981
1. Generate SSH key: `ssh-keygen -t ed25519 -f deploy_key`
8082
2. Add public key to server's `~/.ssh/authorized_keys`
8183
3. Add secrets to GitHub:
@@ -90,7 +92,7 @@ deploy:
9092
name: Deploy to Kubernetes
9193
runs-on: ubuntu-latest
9294
needs: build
93-
95+
9496
steps:
9597
- name: Deploy to K8s
9698
uses: azure/k8s-deploy@v4
@@ -104,6 +106,7 @@ deploy:
104106
### Option 3: Deploy to Container Orchestration Service
105107

106108
Use services like:
109+
107110
- **AWS ECS**: Use `aws-actions/amazon-ecs-deploy-task-definition`
108111
- **Azure Container Instances**: Use `azure/aci-deploy@v1`
109112
- **DigitalOcean App Platform**: Use `digitalocean/app_action@v1.1.0`
@@ -128,6 +131,7 @@ git push origin v2.0.0
128131
```
129132

130133
The workflow will automatically build and tag the image as:
134+
131135
- `ghcr.io/username/idle-code-redeemer-bot:v2.0.0`
132136
- `ghcr.io/username/idle-code-redeemer-bot:2.0.0`
133137
- `ghcr.io/username/idle-code-redeemer-bot:2.0`
@@ -141,14 +145,17 @@ The workflow will automatically build and tag the image as:
141145
## Troubleshooting
142146

143147
**Build fails with "permission denied"**
148+
144149
- Check Dockerfile permissions
145150
- Ensure all required files are committed to git
146151

147152
**Image not published to registry**
153+
148154
- Confirm you're on `main` branch or have a version tag
149155
- Check that GITHUB_TOKEN secret is available (automatic)
150156

151157
**Deployment not triggering**
158+
152159
- Verify the branch/tag matches the workflow triggers
153160
- Check the workflow file is in `.github/workflows/` directory
154161

0 commit comments

Comments
 (0)