mirror of
https://github.com/LightZirconite/Microsoft-Rewards-Bot.git
synced 2026-01-09 17:06:15 +00:00
Refactor bot startup process to auto-create configuration files, enhance update system, and add historical stats endpoints
- Updated README.md to reflect new bot setup and configuration process. - Removed outdated installer README and integrated update logic directly into the bot. - Implemented smart update for example configuration files, ensuring user files are not overwritten. - Added FileBootstrap class to handle automatic creation of configuration files on first run. - Enhanced BotController to manage stop requests and ensure graceful shutdown. - Introduced new stats management features, including historical stats and activity breakdown endpoints. - Updated API routes to include new statistics retrieval functionalities.
This commit is contained in:
@@ -1,176 +0,0 @@
|
||||
# Update System
|
||||
|
||||
## Overview
|
||||
|
||||
The bot uses a **simplified GitHub API-based update system** that:
|
||||
- ✅ Downloads latest code as ZIP archive
|
||||
- ✅ No Git required
|
||||
- ✅ No merge conflicts
|
||||
- ✅ Preserves user files automatically
|
||||
- ✅ Automatic dependency installation
|
||||
- ✅ TypeScript rebuild
|
||||
|
||||
## How It Works
|
||||
|
||||
1. **Automatic Updates**: If enabled in `config.jsonc`, the bot checks for updates on every startup
|
||||
2. **Download**: Latest code is downloaded as ZIP from GitHub
|
||||
3. **Protection**: User files (accounts, config, sessions) are backed up
|
||||
4. **Update**: Code files are replaced selectively
|
||||
5. **Restore**: Protected files are restored
|
||||
6. **Install**: Dependencies are installed (`npm ci`)
|
||||
7. **Build**: TypeScript is compiled
|
||||
8. **Restart**: Bot restarts automatically with new version
|
||||
|
||||
## Configuration
|
||||
|
||||
In `src/config.jsonc`:
|
||||
|
||||
```jsonc
|
||||
{
|
||||
"update": {
|
||||
"enabled": true, // Enable/disable updates
|
||||
"autoUpdateAccounts": false, // Protect accounts files (recommended: false)
|
||||
"autoUpdateConfig": false // Protect config.jsonc (recommended: false)
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
## Protected Files
|
||||
|
||||
These files are **always protected** (never overwritten):
|
||||
- `sessions/` - Browser session data
|
||||
- `.playwright-chromium-installed` - Browser installation marker
|
||||
|
||||
These files are **conditionally protected** (based on config):
|
||||
- `src/accounts.jsonc` - Protected unless `autoUpdateAccounts: true`
|
||||
- `src/accounts.json` - Protected unless `autoUpdateAccounts: true`
|
||||
- `src/config.jsonc` - Protected unless `autoUpdateConfig: true`
|
||||
|
||||
## Manual Update
|
||||
|
||||
Run the update manually:
|
||||
|
||||
```bash
|
||||
node scripts/installer/update.mjs
|
||||
```
|
||||
|
||||
## Update Detection
|
||||
|
||||
The bot uses marker files to prevent restart loops:
|
||||
|
||||
- `.update-happened` - Created when files are actually updated
|
||||
- `.update-restart-count` - Tracks restart attempts (max 3)
|
||||
|
||||
If no updates are available, **no marker is created** and the bot won't restart.
|
||||
|
||||
## Troubleshooting
|
||||
|
||||
### Updates disabled
|
||||
```
|
||||
⚠️ Updates are disabled in config.jsonc
|
||||
```
|
||||
→ Set `update.enabled: true` in `src/config.jsonc`
|
||||
|
||||
### Download failed
|
||||
```
|
||||
❌ Download failed: [error]
|
||||
```
|
||||
→ Check your internet connection
|
||||
→ Verify GitHub is accessible
|
||||
|
||||
### Extraction failed
|
||||
```
|
||||
❌ Extraction failed: [error]
|
||||
```
|
||||
→ Ensure you have one of: `unzip`, `tar`, or PowerShell (Windows)
|
||||
|
||||
### Build failed
|
||||
```
|
||||
⚠️ Update completed with build warnings
|
||||
```
|
||||
→ Check TypeScript errors above
|
||||
→ May still work, but review errors
|
||||
|
||||
## Architecture
|
||||
|
||||
### File Structure
|
||||
```
|
||||
scripts/installer/
|
||||
├── update.mjs # Main update script (auto-updater)
|
||||
├── setup.mjs # Initial setup wizard
|
||||
└── README.md # This file
|
||||
```
|
||||
|
||||
### Update Flow
|
||||
```
|
||||
Start
|
||||
↓
|
||||
Check config (enabled?)
|
||||
↓
|
||||
Read user preferences (autoUpdate flags)
|
||||
↓
|
||||
Backup protected files
|
||||
↓
|
||||
Download ZIP from GitHub
|
||||
↓
|
||||
Extract archive
|
||||
↓
|
||||
Copy files selectively (skip protected)
|
||||
↓
|
||||
Restore protected files
|
||||
↓
|
||||
Cleanup temporary files
|
||||
↓
|
||||
Create marker (.update-happened) if files changed
|
||||
↓
|
||||
Install dependencies (npm ci)
|
||||
↓
|
||||
Build TypeScript
|
||||
↓
|
||||
Exit (bot auto-restarts if marker exists)
|
||||
```
|
||||
|
||||
## Previous System
|
||||
|
||||
The old update system (799 lines) supported two methods:
|
||||
- Git method (required Git, had merge conflicts)
|
||||
- GitHub API method
|
||||
|
||||
**New system**: Only GitHub API method (simpler, more reliable)
|
||||
|
||||
## Anti-Loop Protection
|
||||
|
||||
The bot has built-in protection against infinite restart loops:
|
||||
|
||||
1. **Marker detection**: Bot only restarts if `.update-happened` exists
|
||||
2. **Restart counter**: Max 3 restart attempts (`.update-restart-count`)
|
||||
3. **Counter cleanup**: Removed after successful run without updates
|
||||
4. **No-update detection**: Marker NOT created if already up to date
|
||||
|
||||
This ensures the bot never gets stuck in an infinite update loop.
|
||||
|
||||
## Dependencies
|
||||
|
||||
No external dependencies required! The update system uses only Node.js built-in modules:
|
||||
- `node:child_process` - Run shell commands
|
||||
- `node:fs` - File system operations
|
||||
- `node:https` - Download files
|
||||
- `node:path` - Path manipulation
|
||||
|
||||
## Exit Codes
|
||||
|
||||
- `0` - Success (updated or already up to date)
|
||||
- `1` - Error (download failed, extraction failed, etc.)
|
||||
|
||||
## NPM Scripts
|
||||
|
||||
- `npm run start` - Start bot (runs update check first if enabled)
|
||||
- `npm run dev` - Start in dev mode (skips update check)
|
||||
- `npm run build` - Build TypeScript manually
|
||||
|
||||
## Version Info
|
||||
|
||||
- Current version: **v2** (GitHub API only)
|
||||
- Previous version: v1 (Dual Git/GitHub API)
|
||||
- Lines of code: **468** (down from 799)
|
||||
- Complexity: **Simple** (down from Complex)
|
||||
@@ -264,85 +264,208 @@ function getUpdateMode(configData) {
|
||||
// =============================================================================
|
||||
|
||||
/**
|
||||
* Check if update is available by comparing versions
|
||||
* Uses GitHub API directly (no CDN cache issues, always fresh data)
|
||||
* Rate limit: 60 requests/hour (sufficient for bot updates)
|
||||
* Download a file from GitHub raw URL
|
||||
*/
|
||||
async function checkVersion() {
|
||||
try {
|
||||
// Read local version
|
||||
const localPkgPath = join(process.cwd(), 'package.json')
|
||||
if (!existsSync(localPkgPath)) {
|
||||
console.log('⚠️ Could not find local package.json')
|
||||
return { updateAvailable: false, localVersion: 'unknown', remoteVersion: 'unknown' }
|
||||
}
|
||||
async function downloadFromGitHub(url, dest) {
|
||||
console.log(`📥 Downloading: ${url}`)
|
||||
|
||||
const localPkg = JSON.parse(readFileSync(localPkgPath, 'utf8'))
|
||||
const localVersion = localPkg.version
|
||||
return new Promise((resolve, reject) => {
|
||||
const file = createWriteStream(dest)
|
||||
|
||||
// Fetch remote version from GitHub API (no cache)
|
||||
const repoOwner = 'LightZirconite'
|
||||
const repoName = 'Microsoft-Rewards-Bot'
|
||||
const branch = 'main'
|
||||
|
||||
console.log('🔍 Checking for updates...')
|
||||
console.log(` Local: ${localVersion}`)
|
||||
|
||||
// Use GitHub API directly - no CDN cache, always fresh
|
||||
const apiUrl = `https://api.github.com/repos/${repoOwner}/${repoName}/contents/package.json?ref=${branch}`
|
||||
|
||||
return new Promise((resolve) => {
|
||||
const options = {
|
||||
headers: {
|
||||
'User-Agent': 'Microsoft-Rewards-Bot-Updater',
|
||||
'Accept': 'application/vnd.github.v3.raw', // Returns raw file content
|
||||
'Cache-Control': 'no-cache'
|
||||
}
|
||||
httpsGet(url, {
|
||||
headers: {
|
||||
'User-Agent': 'Microsoft-Rewards-Bot-Updater',
|
||||
'Cache-Control': 'no-cache'
|
||||
}
|
||||
}, (response) => {
|
||||
// Handle redirects
|
||||
if (response.statusCode === 302 || response.statusCode === 301) {
|
||||
file.close()
|
||||
rmSync(dest, { force: true })
|
||||
downloadFromGitHub(response.headers.location, dest).then(resolve).catch(reject)
|
||||
return
|
||||
}
|
||||
|
||||
const request = httpsGet(apiUrl, options, (res) => {
|
||||
if (res.statusCode !== 200) {
|
||||
console.log(` ⚠️ GitHub API returned HTTP ${res.statusCode}`)
|
||||
if (res.statusCode === 403) {
|
||||
console.log(' ℹ️ Rate limit may be exceeded (60/hour). Try again later.')
|
||||
}
|
||||
resolve({ updateAvailable: false, localVersion, remoteVersion: 'unknown' })
|
||||
return
|
||||
}
|
||||
if (response.statusCode !== 200) {
|
||||
file.close()
|
||||
rmSync(dest, { force: true })
|
||||
reject(new Error(`HTTP ${response.statusCode}: ${response.statusMessage}`))
|
||||
return
|
||||
}
|
||||
|
||||
let data = ''
|
||||
res.on('data', chunk => data += chunk)
|
||||
res.on('end', () => {
|
||||
try {
|
||||
const remotePkg = JSON.parse(data)
|
||||
const remoteVersion = remotePkg.version
|
||||
console.log(` Remote: ${remoteVersion}`)
|
||||
|
||||
// Any difference triggers update (upgrade or downgrade)
|
||||
const updateAvailable = localVersion !== remoteVersion
|
||||
resolve({ updateAvailable, localVersion, remoteVersion })
|
||||
} catch (err) {
|
||||
console.log(` ⚠️ Could not parse remote package.json: ${err.message}`)
|
||||
resolve({ updateAvailable: false, localVersion, remoteVersion: 'unknown' })
|
||||
}
|
||||
})
|
||||
})
|
||||
|
||||
request.on('error', (err) => {
|
||||
console.log(` ⚠️ Network error: ${err.message}`)
|
||||
resolve({ updateAvailable: false, localVersion, remoteVersion: 'unknown' })
|
||||
})
|
||||
|
||||
request.setTimeout(10000, () => {
|
||||
request.destroy()
|
||||
console.log(' ⚠️ Request timeout (10s)')
|
||||
resolve({ updateAvailable: false, localVersion, remoteVersion: 'unknown' })
|
||||
response.pipe(file)
|
||||
file.on('finish', () => {
|
||||
file.close()
|
||||
resolve()
|
||||
})
|
||||
}).on('error', (err) => {
|
||||
file.close()
|
||||
rmSync(dest, { force: true })
|
||||
reject(err)
|
||||
})
|
||||
} catch (err) {
|
||||
console.log(`⚠️ Version check failed: ${err.message}`)
|
||||
})
|
||||
}
|
||||
|
||||
/**
|
||||
* Smart update for config/accounts example files
|
||||
* Only updates if GitHub version has changed AND local user file matches old example
|
||||
*/
|
||||
async function smartUpdateExampleFiles(configData) {
|
||||
const files = []
|
||||
|
||||
// Check which files to update based on config
|
||||
if (configData?.update?.autoUpdateConfig === true) {
|
||||
files.push({
|
||||
example: 'src/config.example.jsonc',
|
||||
target: 'src/config.jsonc',
|
||||
name: 'Configuration',
|
||||
githubUrl: 'https://raw.githubusercontent.com/LightZirconite/Microsoft-Rewards-Bot/refs/heads/main/src/config.example.jsonc'
|
||||
})
|
||||
}
|
||||
|
||||
if (configData?.update?.autoUpdateAccounts === true) {
|
||||
files.push({
|
||||
example: 'src/accounts.example.jsonc',
|
||||
target: 'src/accounts.jsonc',
|
||||
name: 'Accounts',
|
||||
githubUrl: 'https://raw.githubusercontent.com/LightZirconite/Microsoft-Rewards-Bot/refs/heads/main/src/accounts.example.jsonc'
|
||||
})
|
||||
}
|
||||
|
||||
if (files.length === 0) {
|
||||
return // Nothing to update
|
||||
}
|
||||
|
||||
console.log('\n🔧 Checking for example file updates...')
|
||||
|
||||
for (const file of files) {
|
||||
try {
|
||||
const examplePath = join(process.cwd(), file.example)
|
||||
const targetPath = join(process.cwd(), file.target)
|
||||
const tempPath = join(process.cwd(), `.update-${file.example.split('/').pop()}`)
|
||||
|
||||
// Download latest version from GitHub
|
||||
await downloadFromGitHub(file.githubUrl, tempPath)
|
||||
|
||||
// Read all versions
|
||||
const githubContent = readFileSync(tempPath, 'utf8')
|
||||
const localExampleContent = existsSync(examplePath) ? readFileSync(examplePath, 'utf8') : ''
|
||||
const userContent = existsSync(targetPath) ? readFileSync(targetPath, 'utf8') : ''
|
||||
|
||||
// Check if GitHub version is different from local example
|
||||
if (githubContent === localExampleContent) {
|
||||
console.log(`✓ ${file.name}: No changes detected`)
|
||||
rmSync(tempPath, { force: true })
|
||||
continue
|
||||
}
|
||||
|
||||
// GitHub version is different - check if user has modified their file
|
||||
if (userContent === localExampleContent) {
|
||||
// User hasn't modified their file - safe to update
|
||||
console.log(`📝 ${file.name}: Updating to latest version...`)
|
||||
|
||||
// Update example file
|
||||
writeFileSync(examplePath, githubContent)
|
||||
|
||||
// Update user file (since they haven't customized it)
|
||||
writeFileSync(targetPath, githubContent)
|
||||
|
||||
console.log(`✅ ${file.name}: Updated successfully`)
|
||||
} else {
|
||||
// User has customized their file - DO NOT overwrite
|
||||
console.log(`⚠️ ${file.name}: User has custom changes, skipping auto-update`)
|
||||
console.log(` → Update available in: ${file.example}`)
|
||||
console.log(` → To disable this check: set "update.autoUpdate${file.name === 'Configuration' ? 'Config' : 'Accounts'}" to false`)
|
||||
|
||||
// Still update the example file for reference
|
||||
writeFileSync(examplePath, githubContent)
|
||||
}
|
||||
|
||||
// Clean up temp file
|
||||
rmSync(tempPath, { force: true })
|
||||
|
||||
} catch (error) {
|
||||
console.error(`❌ Failed to update ${file.name}: ${error.message}`)
|
||||
// Continue with other files
|
||||
}
|
||||
}
|
||||
|
||||
console.log('')
|
||||
}
|
||||
try {
|
||||
// Read local version
|
||||
const localPkgPath = join(process.cwd(), 'package.json')
|
||||
if (!existsSync(localPkgPath)) {
|
||||
console.log('⚠️ Could not find local package.json')
|
||||
return { updateAvailable: false, localVersion: 'unknown', remoteVersion: 'unknown' }
|
||||
}
|
||||
|
||||
const localPkg = JSON.parse(readFileSync(localPkgPath, 'utf8'))
|
||||
const localVersion = localPkg.version
|
||||
|
||||
// Fetch remote version from GitHub API (no cache)
|
||||
const repoOwner = 'LightZirconite'
|
||||
const repoName = 'Microsoft-Rewards-Bot'
|
||||
const branch = 'main'
|
||||
|
||||
console.log('🔍 Checking for updates...')
|
||||
console.log(` Local: ${localVersion}`)
|
||||
|
||||
// Use GitHub API directly - no CDN cache, always fresh
|
||||
const apiUrl = `https://api.github.com/repos/${repoOwner}/${repoName}/contents/package.json?ref=${branch}`
|
||||
|
||||
return new Promise((resolve) => {
|
||||
const options = {
|
||||
headers: {
|
||||
'User-Agent': 'Microsoft-Rewards-Bot-Updater',
|
||||
'Accept': 'application/vnd.github.v3.raw', // Returns raw file content
|
||||
'Cache-Control': 'no-cache'
|
||||
}
|
||||
}
|
||||
|
||||
const request = httpsGet(apiUrl, options, (res) => {
|
||||
if (res.statusCode !== 200) {
|
||||
console.log(` ⚠️ GitHub API returned HTTP ${res.statusCode}`)
|
||||
if (res.statusCode === 403) {
|
||||
console.log(' ℹ️ Rate limit may be exceeded (60/hour). Try again later.')
|
||||
}
|
||||
resolve({ updateAvailable: false, localVersion, remoteVersion: 'unknown' })
|
||||
return
|
||||
}
|
||||
|
||||
let data = ''
|
||||
res.on('data', chunk => data += chunk)
|
||||
res.on('end', () => {
|
||||
try {
|
||||
const remotePkg = JSON.parse(data)
|
||||
const remoteVersion = remotePkg.version
|
||||
console.log(` Remote: ${remoteVersion}`)
|
||||
|
||||
// Any difference triggers update (upgrade or downgrade)
|
||||
const updateAvailable = localVersion !== remoteVersion
|
||||
resolve({ updateAvailable, localVersion, remoteVersion })
|
||||
} catch (err) {
|
||||
console.log(` ⚠️ Could not parse remote package.json: ${err.message}`)
|
||||
resolve({ updateAvailable: false, localVersion, remoteVersion: 'unknown' })
|
||||
}
|
||||
})
|
||||
})
|
||||
|
||||
request.on('error', (err) => {
|
||||
console.log(` ⚠️ Network error: ${err.message}`)
|
||||
resolve({ updateAvailable: false, localVersion, remoteVersion: 'unknown' })
|
||||
})
|
||||
|
||||
request.setTimeout(10000, () => {
|
||||
request.destroy()
|
||||
console.log(' ⚠️ Request timeout (10s)')
|
||||
resolve({ updateAvailable: false, localVersion, remoteVersion: 'unknown' })
|
||||
})
|
||||
})
|
||||
} catch (err) {
|
||||
console.log(`⚠️ Version check failed: ${err.message}`)
|
||||
return { updateAvailable: false, localVersion: 'unknown', remoteVersion: 'unknown' }
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -615,6 +738,9 @@ async function performUpdate() {
|
||||
|
||||
process.stdout.write(' ✓\n')
|
||||
|
||||
// Step 10.5: Smart update example files (config/accounts) if enabled
|
||||
await smartUpdateExampleFiles(configData)
|
||||
|
||||
// Step 11: Verify integrity (check if critical files exist AND were recently updated)
|
||||
process.stdout.write('🔍 Verifying integrity...')
|
||||
const criticalPaths = [
|
||||
|
||||
Reference in New Issue
Block a user