Setting up your .env file
Create .env file
Create a
.env file in the root of your project directory (same level as package.json).Add required variables
Add the required environment variables to your
.env file. See the sections below for details on each variable.Required environment variables
These variables are essential for the bot to function properly.Your Discord bot token from the Discord Developer Portal.This token authenticates your bot with Discord’s API and is used in How to get it:
src/index.js:45 to log in:- Go to the Discord Developer Portal
- Select your application
- Navigate to the “Bot” tab
- Click “Reset Token” or “Copy” to get your token
MongoDB connection string for database persistence.The bot connects to MongoDB in Format:Or for MongoDB Atlas:
src/index.js:11 using Mongoose:The bot uses MongoDB to store user data (AniList profiles), guild configurations, and other persistent data. See the MongoDB setup guide for detailed instructions.
Optional environment variables
These variables are optional and enable additional features.Your Discord application’s client ID.Used for generating invite links and OAuth2 authorization. While not strictly required for the bot to run, it’s useful for:
- Generating proper invite URLs
- Application command registration
- OAuth2 flows
- Go to the Discord Developer Portal
- Select your application
- Copy the “Application ID” from the General Information tab
A test guild (server) ID for faster command registration during development.When developing, registering commands to a specific guild is much faster (instant) than registering globally (can take up to 1 hour).How to get it:
- Enable Developer Mode in Discord (User Settings > Advanced > Developer Mode)
- Right-click on your test server
- Click “Copy ID”
Only use this during development. For production, register commands globally by omitting this variable.
Discord channel ID where the bot will send log messages (joins, leaves, moderation events).If configured, the bot can send automated logs for:
- Member joins and leaves
- Moderation actions (kicks, bans, mutes)
- Other guild events
Discord channel ID where the bot will send welcome messages for new members.The bot can generate custom Canvas-based welcome cards with user avatars and server information.
Example .env file
Environment variable reference
| Variable | Type | Required | Description |
|---|---|---|---|
TOKEN | string | Yes | Discord bot token for authentication |
MONGO_URI | string | Yes | MongoDB connection string |
CLIENT_ID | string | No | Discord application client ID |
GUILD_ID | string | No | Test guild ID for development |
LOG_CHANNEL_ID | string | No | Channel ID for bot logs |
WELCOME_CHANNEL_ID | string | No | Channel ID for welcome messages |
Security best practices
- Never commit
.env: Always add.envto your.gitignorefile - Rotate tokens regularly: If you suspect a token leak, regenerate it immediately
- Use environment-specific files: Maintain separate
.env.developmentand.env.productionfiles - Limit access: Only share environment variables with trusted team members
- Use secret management: For production deployments, consider using services like:
- GitHub Secrets (for CI/CD)
- Heroku Config Vars
- Railway Environment Variables
- Docker secrets
- AWS Secrets Manager
- Azure Key Vault
Loading environment variables
Yato uses thedotenv package to load environment variables. This happens automatically at the start of src/index.js:2:
.env file into process.env, making them accessible throughout the application.
Troubleshooting
Bot won’t start
If the bot fails to start, check:- Missing TOKEN: Ensure
TOKENis set in your.envfile - Invalid TOKEN: Verify the token is correct and hasn’t been regenerated
- .env location: Ensure
.envis in the project root, not in subdirectories - Syntax errors: Check for missing quotes or special characters
MongoDB connection fails
If MongoDB connection fails:- Check MONGO_URI format: Ensure the connection string is valid
- Verify credentials: Double-check username and password
- Network access: For MongoDB Atlas, add your IP to the IP whitelist
- Database exists: Ensure the database name in the URI exists or can be created