Discord Bot Deployment Guide
Deploy your Discord bot to keep it running 24/7 in production.
Hosting Options
Free Tier Platforms
Replit
- Easy setup with web IDE
- Free tier available
- Good for small bots
- Auto-sleeps on inactivity
Railway
- 500 hours free per month
- Easy GitHub integration
- Supports Docker
- Automatic deployments
Render
- Free tier with limitations
- Auto-deploy from Git
- Sleeps after inactivity
- Great for testing
Paid Platforms
DigitalOcean
- $5/month droplet
- Full control
- Reliable uptime
- Scalable
AWS EC2
- Flexible pricing
- Enterprise-grade
- Many configuration options
- Free tier for 12 months
Basic Deployment Steps
1. Prepare Your Bot
# Add start script to package.json{ "scripts": { "start": "node index.js" }}
# Create .gitignorenode_modules/.env*.log2. Environment Variables
Never commit secrets! Use environment variables:
// Load from .env in developmentrequire('dotenv').config();
// Access in codeconst token = process.env.DISCORD_TOKEN;const dbUrl = process.env.DATABASE_URL;3. Process Management
Use PM2 to keep bot running:
npm install -g pm2
# Start botpm2 start index.js --name discord-bot
# Auto-restart on rebootpm2 startuppm2 save
# Monitorpm2 monit
# View logspm2 logs discord-botDocker Deployment
Create Dockerfile:
FROM node:18-alpine
WORKDIR /app
COPY package*.json ./RUN npm ci --only=production
COPY . .
CMD ["node", "index.js"]Create docker-compose.yml:
version: '3.8'services: bot: build: . restart: unless-stopped environment: - DISCORD_TOKEN=${DISCORD_TOKEN} - DATABASE_URL=${DATABASE_URL} volumes: - ./data:/app/dataRun:
docker-compose up -dHealth Checks
Add a simple HTTP server for health monitoring:
const express = require('express');const app = express();
app.get('/health', (req, res) => { res.json({ status: 'healthy', uptime: process.uptime(), botReady: client.isReady() });});
app.listen(3000);Logging
Implement proper logging:
const winston = require('winston');
const logger = winston.createLogger({ level: 'info', format: winston.format.json(), transports: [ new winston.transports.File({ filename: 'error.log', level: 'error' }), new winston.transports.File({ filename: 'combined.log' }) ]});
if (process.env.NODE_ENV !== 'production') { logger.add(new winston.transports.Console({ format: winston.format.simple() }));}Monitoring
Basic Uptime Monitoring
Use services like:
- UptimeRobot (free)
- Pingdom
- StatusCake
Application Monitoring
Consider:
- Sentry for error tracking
- Datadog for metrics
- New Relic for APM
Scaling Considerations
Sharding for large bots (2500+ servers):
const { ShardingManager } = require('discord.js');
const manager = new ShardingManager('./bot.js', { token: process.env.DISCORD_TOKEN, totalShards: 'auto'});
manager.spawn();Backup Strategy
- Regular database backups
- Store backups off-site
- Test restore procedures
- Keep configuration in version control
Security Checklist
- ✅ Never commit tokens or secrets
- ✅ Use environment variables
- ✅ Keep dependencies updated
- ✅ Implement rate limiting
- ✅ Validate all user input
- ✅ Use HTTPS for webhooks
- ✅ Regular security audits
Common Deployment Issues
Bot goes offline
- Check process manager logs
- Verify network connectivity
- Check rate limits
- Monitor resource usage
High memory usage
- Review event listeners
- Check for memory leaks
- Implement proper cleanup
- Consider sharding
Slow response times
- Add caching layer
- Optimize database queries
- Use connection pooling
- Profile slow operations