Discord Bot Setup Guide
This guide walks you through setting up a Discord application and bot for FightRise.
Prerequisites
Before you begin, make sure you have:
- Discord account with verified email address
- Administrator access to a Discord server (for testing the bot)
- Text editor for managing
.envfiles - Node.js 18+ installed (for running the bot locally)
Estimated time: 15-20 minutes
Related Documentation
| Document | Description |
|---|---|
| Discord Setup (this doc) | Discord bot configuration |
| Start.gg Setup | Start.gg API setup |
| Tunnel Setup | Cloudflare Tunnel for OAuth |
| Architecture | System architecture overview |
Table of Contents
- Creating a Discord Application
- Configuring the Bot
- Gateway Intents
- OAuth2 Configuration
- Generating the Bot Invite URL
- Environment Variables
- Security Best Practices
Creating a Discord Application
Step 1: Access the Developer Portal
- Go to the Discord Developer Portal
- Sign in with your Discord account
- Click the "New Application" button in the top right
Step 2: Create the Application
- Enter a name for your application (e.g., "FightRise Bot" or "FightRise Tournament")
- Agree to the Discord Developer Terms of Service
- Click "Create"
Step 3: Note Your Application Credentials
On the General Information tab, you'll find:
- Application ID - This is your
DISCORD_CLIENT_ID - Public Key - Used for interaction verification (not needed for this bot)
Configuring the Bot
Step 1: Create the Bot User
- Navigate to the "Bot" tab in the left sidebar
- A bot user is created automatically with your application
- You can customize the bot's username and avatar here
Step 2: Get the Bot Token
- Under the "Token" section, click "Reset Token"
- Confirm the action (this will invalidate any previous token)
- Copy the token immediately - you cannot view it again
- This is your
DISCORD_TOKENenvironment variable
WARNING: Never share your bot token. Anyone with this token can control your bot. If your token is compromised, regenerate it immediately.
Step 3: Configure Bot Settings
Under the "Bot" section, configure these settings:
| Setting | Recommended | Description |
|---|---|---|
| Public Bot | ✅ Enabled | Allows others to invite your bot |
| Requires OAuth2 Code Grant | ❌ Disabled | Not needed for FightRise |
| Presence Intent | ❌ Disabled | Not needed |
| Server Members Intent | ❌ Disabled | Not needed currently |
| Message Content Intent | ✅ Enabled | Required for reading messages in threads |
Gateway Intents
Gateway Intents control what events your bot receives from Discord. FightRise uses these intents:
Required Intents
| Intent | Privileged | Why It's Needed |
|---|---|---|
Guilds |
No | Access to guild/server information |
GuildMessages |
No | Receive message events in guild channels |
MessageContent |
Yes | Read message content in match threads |
Enabling Privileged Intents
The Message Content Intent is privileged and must be explicitly enabled:
- Go to Bot tab in Developer Portal
- Scroll to "Privileged Gateway Intents"
- Enable "Message Content Intent"
- Save changes
Note: If your bot is in 100+ servers, Discord may require verification to use privileged intents.
Intent Configuration in Code
The bot is configured with these intents in apps/bot/src/index.ts:
import { Client, GatewayIntentBits } from 'discord.js';
const client = new Client({
intents: [
GatewayIntentBits.Guilds,
GatewayIntentBits.GuildMessages,
GatewayIntentBits.MessageContent,
],
});
OAuth2 Configuration
OAuth2 is used for two purposes:
- Bot invites - Adding the bot to servers
- User authentication - Web portal sign-in via Discord
OAuth2 Flow Diagram
sequenceDiagram
participant U as User
participant F as FightRise Web
participant D as Discord
U->>F: Click "Sign in with Discord"
F->>D: Redirect to Discord authorize
D->>U: Show permission prompt
U->>D: Approve permissions
D->>F: Redirect with auth code
F->>D: Exchange code for tokens
D->>F: Access token + refresh token
F->>U: User signed in
Required OAuth2 Scopes
| Scope | Purpose |
|---|---|
bot |
Adds the bot user to a guild with permissions |
applications.commands |
Enables slash commands |
identify |
Web portal: Access user's Discord ID and username |
guilds |
Web portal: Access user's guild list |
Configuring OAuth2 for Web Portal
- Go to OAuth2 → General in Developer Portal
- Add your redirect URIs:
- User authentication (web portal):
- Development (local only):
http://localhost:3000/api/auth/callback/discord - Development (with tunnel):
https://fightrise-dev.yourdomain.com/api/auth/callback/discord - Production:
https://your-domain.com/api/auth/callback/discord
- Development (local only):
- Bot installation callback:
- Development (local only):
http://localhost:3000/api/auth/callback/bot - Development (with tunnel):
https://fightrise-dev.yourdomain.com/api/auth/callback/bot - Production:
https://your-domain.com/api/auth/callback/bot
- Development (local only):
- User authentication (web portal):
- Note your Client Secret (click "Reset Secret" if needed)
- This is your
DISCORD_CLIENT_SECRET
- This is your
Note: OAuth callbacks require publicly accessible URLs. For local development with OAuth, use Cloudflare Tunnel to expose your local server.
Generating the Bot Invite URL
Using Discord's URL Generator
Go to OAuth2 → URL Generator
Select these Scopes:
- ✅
bot - ✅
applications.commands
- ✅
Select these Bot Permissions:
Required Permissions
| Permission | Bit | Why It's Needed |
|---|---|---|
| Send Messages | 2048 |
Post in channels |
| Send Messages in Threads | 274877906944 |
Post in match threads |
| Create Public Threads | 34359738368 |
Create match threads |
| Create Private Threads | 68719476736 |
Optional: private match threads |
| Manage Threads | 17179869184 |
Archive threads after matches |
| Embed Links | 16384 |
Rich embeds for match info |
| Read Message History | 65536 |
Access thread history |
| Mention Everyone | 131072 |
Ping players for matches |
| Use Application Commands | 2147483648 |
Slash commands |
| View Channels | 1024 |
Access channel list |
Recommended Permission Integer
For FightRise, use this permissions integer: 397284690944
This includes all required permissions listed above.
Generated Invite URL Format
https://discord.com/api/oauth2/authorize?client_id=YOUR_CLIENT_ID&permissions=397284690944&scope=bot%20applications.commands
Replace YOUR_CLIENT_ID with your actual Application ID.
Alternative: Minimal Permissions
If you want to start with minimal permissions and add more later:
Minimal permissions integer: 277025508352
This covers basic messaging and thread creation.
Environment Variables
Add these to your .env file:
# Discord Bot Configuration
DISCORD_TOKEN="your-bot-token-from-bot-tab"
DISCORD_CLIENT_ID="your-application-id-from-general-info"
DISCORD_CLIENT_SECRET="your-client-secret-from-oauth2"
# OAuth Redirect URIs
DISCORD_REDIRECT_URI="http://localhost:3000/api/auth/callback/discord"
DISCORD_BOT_REDIRECT_URI="http://localhost:3000/api/auth/callback/bot"
Where to Find Each Value
| Variable | Location in Developer Portal |
|---|---|
DISCORD_TOKEN |
Bot → Token → Reset Token |
DISCORD_CLIENT_ID |
General Information → Application ID |
DISCORD_CLIENT_SECRET |
OAuth2 → General → Client Secret |
DISCORD_BOT_REDIRECT_URI |
OAuth2 → General → Redirect URIs (add new) |
Security Best Practices
Token Security
Never commit tokens to version control
- Use
.envfiles and ensure.envis in.gitignore
- Use
Use environment variables
- Never hardcode tokens in source code
Limit token access
- Only share tokens with trusted team members through secure channels
Token Rotation
Regular token rotation reduces the risk of compromised credentials.
Rotation Schedule:
| Token Type | Recommended Interval |
|---|---|
| Bot Token | Every 90 days |
| Client Secret | Every 90 days |
When to Rotate Immediately:
- A team member with token access leaves the project
- You suspect the token may have been exposed
- After a security incident
- Token appears in logs, error messages, or version control
How to Rotate:
- Bot Token: Go to Bot tab → Click "Reset Token" → Update
.env→ Restart bot - Client Secret: Go to OAuth2 → Click "Reset Secret" → Update
.env→ Restart web app
Best Practice: Create the new token and verify it works before revoking the old one (where possible).
Permission Principles
Request minimum permissions
- Only enable permissions your bot actually uses
Disable unused intents
- Reduces data your bot receives and improves privacy
Audit permissions regularly
- Review and remove permissions no longer needed
OAuth2 Security
Validate redirect URIs
- Only add URIs you control to the OAuth2 settings
Use HTTPS in production (mandatory)
- Never use HTTP redirect URIs in production
- Exception:
http://localhostis allowed for local development only
Validate state parameter
- Implement CSRF protection in OAuth flows
- NextAuth.js handles this automatically
Production Secrets Management
For production deployments, use a dedicated secrets manager instead of .env files:
| Service | Best For | Key Features |
|---|---|---|
| AWS Secrets Manager | AWS-hosted apps | Automatic rotation, IAM integration |
| HashiCorp Vault | Multi-cloud, K8s | Open-source, highly configurable |
| Azure Key Vault | Azure-hosted apps | Azure AD integration |
| Google Secret Manager | GCP-hosted apps | IAM integration, versioning |
| Railway/Render Secrets | Smaller deployments | Built-in, easy setup |
Benefits over .env files:
- Automatic token rotation
- Audit logging (who accessed what, when)
- Encryption at rest
- Fine-grained access control
- Version history and rollback
Troubleshooting
Bot Not Responding to Commands
- Check the bot is online - Look for green status indicator
- Verify commands are deployed - Run
npm run deploy -- --filter=@fightrise/bot - Check bot has permissions - Ensure bot role has required permissions in the channel
- Check intents are enabled - Verify privileged intents in Developer Portal
"Missing Access" or Permission Errors
- Re-invite the bot with updated permissions
- Check role hierarchy - Bot's role must be above roles it manages
- Check channel permissions - Channel-specific overrides may block the bot
OAuth Login Fails
- Verify redirect URI matches exactly - Including trailing slashes
- Check client secret - Ensure it hasn't been rotated
- Check scopes - Ensure
identifyscope is included
Additional Resources
Next Steps
Now that Discord is configured:
- Complete Start.gg Setup if you haven't already
- Run
npm installat the repository root - Copy
.env.exampleto.envand add your credentials - Run
npm run dev -- --filter=@fightrise/botto start the bot - Invite the bot to your test server using the generated URL
- Test with
/tournament setupcommand
See the GitHub Issues to track progress or find ways to contribute.