-
-
Notifications
You must be signed in to change notification settings - Fork 0
Development Setup
Set up your local development environment for contributing to the SourceXchange Sync Bot.
Before you begin, ensure you have:
- Node.js 18 or higher (Download)
- Git (Download)
- A code editor (VS Code recommended)
- Discord Bot Token (for testing)
- SourceXchange API Token (for testing)
- Go to sourcexchange-sync-bot
- Click the Fork button in the top-right corner
- Select your GitHub account
git clone https://github.com/YOUR_USERNAME/sourcexchange-sync-bot.git
cd sourcexchange-sync-botgit remote add upstream https://github.com/KingIronMan2011/sourcexchange-sync-bot.git
git remote -vYou should see:
origin https://github.com/YOUR_USERNAME/sourcexchange-sync-bot.git (fetch)
origin https://github.com/YOUR_USERNAME/sourcexchange-sync-bot.git (push)
upstream https://github.com/KingIronMan2011/sourcexchange-sync-bot.git (fetch)
upstream https://github.com/KingIronMan2011/sourcexchange-sync-bot.git (push)
npm installCreate a .env file for development:
cp .env.example .envEdit .env with your development credentials:
# Discord Bot Configuration
DISCORD_TOKEN=your_development_bot_token
CLIENT_ID=your_development_application_id
# SourceXchange API Configuration
API_BASE_URL=https://sourcexchange.net/api/v1
API_TOKEN=your_development_api_token
PRODUCT_ID=your_test_product_id
# Discord Role Configuration
DISCORD_ROLE_ID=your_test_role_id
# Environment
NODE_ENV=developmentStart the bot in development mode:
npm startFor automatic restarts on file changes, use nodemon:
npm install -g nodemon
nodemon bot.js# Format all files
npm run format
# Check formatting without changes
npm run format:check# Run ESLint
npm run lint
# Auto-fix issues
npm run lint:fixAlways run both checks:
npm run format
npm run lint:fixsourcexchange-sync-bot/
├── .github/
│ ├── FUNDING.yml # GitHub Sponsors configuration
│ └── workflows/
│ └── ci.yml # GitHub Actions CI/CD
├── bot.js # Main bot file
├── .env.example # Environment variables template
├── .env # Your local config (gitignored)
├── .gitignore # Git ignore rules
├── .prettierrc # Prettier configuration
├── eslint.config.js # ESLint configuration
├── LICENSE.md # MIT License
├── package.json # Dependencies and scripts
└── README.md # Project overview
Main bot logic containing:
- Discord client initialization
- Slash command definitions
- API integration functions
- Command handlers
Key Functions:
-
apiGet(path)- Fetch data from SourceXchange API -
getUserProductAccesses(discordId)- Get user's purchased products -
deployCommands()- Register slash commands with Discord
Environment configuration:
- Discord bot credentials
- SourceXchange API settings
- Feature flags
Project metadata and scripts:
-
npm start- Run the bot -
npm run format- Format code -
npm run lint- Check code quality
git checkout -b feature/your-feature-nameBranch naming conventions:
-
feature/- New features -
fix/- Bug fixes -
docs/- Documentation updates -
refactor/- Code refactoring
Examples:
git checkout -b feature/add-multi-product-support
git checkout -b fix/role-hierarchy-check
git checkout -b docs/update-installation-guideEdit the code in your editor. For VS Code users:
Recommended Extensions:
- ESLint
- Prettier - Code formatter
- GitLens
-
Start your test bot:
npm start
-
Test in your development Discord server:
- Run
/productscommand - Run
/synccommand - Verify error handling
- Check edge cases
- Run
-
Review console output for errors
npm run format
npm run lint:fixgit add .
git commit -m "feat: add multi-product support"Commit Message Format:
<type>: <description>
[optional body]
Types:
-
feat:- New feature -
fix:- Bug fix -
docs:- Documentation only -
style:- Code style (formatting, no logic change) -
refactor:- Code refactoring -
test:- Adding tests -
chore:- Maintenance tasks
Examples:
git commit -m "feat: add support for multiple products"
git commit -m "fix: handle missing role gracefully"
git commit -m "docs: update API integration guide"git push origin feature/your-feature-name- Go to your fork on GitHub
- Click "Compare & pull request"
- Fill out the PR template
- Submit the pull request
# Fetch latest changes
git fetch upstream
# Switch to main branch
git checkout main
# Merge upstream changes
git merge upstream/main
# Push to your fork
git push origin mainIf main has changed since you created your branch:
git checkout feature/your-feature-name
git rebase mainResolve any conflicts, then:
git push origin feature/your-feature-name --forceBefore submitting a PR, test:
-
/productscommand works -
/synccommand works - Role is added when user has product
- Role is removed when user doesn't have product
- Error messages are clear
- Bot handles missing permissions gracefully
- Bot handles API errors gracefully
- Logging is appropriate (not too verbose)
- User purchases product on SourceXchange
- User links Discord account
- User runs
/sync - Verify role is granted
- User doesn't own product
- User runs
/sync - Verify appropriate message
- Verify no role granted
- User has role
- User's product access expires
- User runs
/sync - Verify role is removed
- User hasn't linked Discord
- User runs
/productsor/sync - Verify clear error message
- Bot doesn't have "Manage Roles"
- User runs
/sync - Verify graceful error handling
Set in .env:
NODE_ENV=developmentThis enables:
- API request/response logging
- Detailed error messages
- Command execution traces
Add console logs at key points:
// API calls
console.log("Fetching product accesses for user:", discordId);
// Role management
console.log("Current roles:", member.roles.cache.map(r => r.name));
console.log("Target role:", role.name);
console.log("Has product:", hasProduct);
console.log("Has role:", hasRole);
// Error handling
catch (error) {
console.error("Full error:", error);
console.error("Error message:", error.message);
console.error("Error stack:", error.stack);
}Create .vscode/launch.json:
{
"version": "0.2.0",
"configurations": [
{
"type": "node",
"request": "launch",
"name": "Launch Bot",
"skipFiles": ["<node_internals>/**"],
"program": "${workspaceFolder}/bot.js",
"envFile": "${workspaceFolder}/.env"
}
]
}Set breakpoints and press F5 to debug.
- Create a new Discord server for testing
- Invite your development bot
- Create test roles
- Test in isolation from production
- Create test products on SourceXchange
- Use test product IDs in development
- Avoid affecting production users
- Keep
.envin.gitignore - Use separate development tokens
- Regenerate tokens if exposed
- Use environment variables in CI/CD
The project uses GitHub Actions for automated checks.
Located in .github/workflows/ci.yml:
- ✅ Prettier formatting check
- ✅ ESLint validation
- Runs on: push, pull requests
Run the same checks locally:
npm run format:check
npm run lintFix any issues before pushing.
Install PM2:
npm install -g pm2Start bot:
pm2 start bot.js --name sourcexchange-bot
pm2 save
pm2 startupMonitor:
pm2 logs sourcexchange-bot
pm2 statusCreate /etc/systemd/system/sourcexchange-bot.service:
[Unit]
Description=SourceXchange Sync Bot
After=network.target
[Service]
Type=simple
User=root
WorkingDirectory=/path/to/sourcexchange-sync-bot
Environment=NODE_ENV=production
ExecStart=/usr/bin/node bot.js
Restart=always
RestartSec=10
[Install]
WantedBy=multi-user.targetEnable and start:
sudo systemctl enable --now sourcexchange-bot
sudo systemctl status sourcexchange-botView logs:
sudo journalctl -u sourcexchange-bot -fCreate Dockerfile:
FROM node:24-alpine
WORKDIR /
COPY package*.json ./
RUN npm ci --only=production
COPY . .
CMD ["node", "bot.js"]Build and run:
docker build -t sourcexchange-bot .
docker run -d --name sourcexchange-bot --env-file .env sourcexchange-bot- Contributing - Contribution guidelines
- Configuration - Configuration reference
- Troubleshooting - Debug common issues