stats-tracker/README.md

84 lines
2.4 KiB
Markdown

# Stats Tracker 📊
A Python script that tracks Minecraft server player stats and posts them to Discord via webhook.
## Setup
### 1. Install Dependencies
```bash
pip install -r requirements.txt
```
### 2. Configure Environment Variables
Copy the example file and fill in your credentials:
```bash
cp .env.example .env
```
Edit `.env` and add:
- `DISCORD_WEBHOOK_URL`: Your Discord webhook URL
- `API_KEY`: Your DonutSMP API key (get it with `/api` in-game)
### 3. Add Players to Track
Edit `players.json` and add the usernames you want to track:
```json
{
"players": ["player1", "player2", "player3"]
}
```
## Running
```bash
python main.py
```
The script will:
- ✅ Fetch stats every 2 minutes
- ✅ Create a new Discord message on first run
- ✅ Edit the same message on subsequent updates (no spam!)
-**Only update the message if stats actually change** (saves API calls)
- ✅ Display stats grouped by type across all players
- ✅ Detect who is currently online using the lookup API
- ✅ Show online location state icons per player
- ✅ Show: Money, Playtime, Kills, Deaths, Mobs Killed, Blocks Placed/Broken, Shards
## How It Works
1. **First Run**: Posts a new message with all player stats grouped by type
2. **Subsequent Runs**:
- Compares current stats with cached stats
- Only edits the Discord message if something changed
- Skips updates if stats are unchanged (saves Discord API calls)
3. **No Message Found**: If `message_id.txt` is deleted or the Discord message is removed, it will create a new one
4. **Error Handling**: If a player doesn't exist or API fails, it shows "❌" for that player
## Files Created
- `message_id.txt` - Stores the Discord message ID for edits
- `stats_cache.json` - Caches previous stats to detect changes
## Notes
- The message ID is automatically saved to prevent creating duplicate messages
- Stats are cached to avoid unnecessary Discord updates
- If you want to reset and create a new message, delete `message_id.txt` and `stats_cache.json`
- The script handles network errors gracefully and will retry every 2 minutes
- Press `Ctrl+C` to stop the script
## Online Detection Icons
The tracker uses `https://api.donutsmp.net/v1/lookup/<username>` to detect online state and location:
- `:sleeping:` = `Limbo` (AFK)
- `:yellow_circle:` = `Spawn`
- `:green_circle:` = `Overworld` (also used for `Nether` and `End`)
- `:black_circle:` = Offline