Skip to main content
Yato uses environment variables to configure the bot’s core functionality, including Discord authentication, database connections, and optional features.

Setting up your .env file

1

Create .env file

Create a .env file in the root of your project directory (same level as package.json).
touch .env
2

Add required variables

Add the required environment variables to your .env file. See the sections below for details on each variable.
3

Secure your .env file

Ensure your .env file is listed in .gitignore to prevent committing sensitive tokens to version control.
Never commit your .env file or expose your bot token publicly. If your token is compromised, regenerate it immediately in the Discord Developer Portal.

Required environment variables

These variables are essential for the bot to function properly.
TOKEN
string
required
Your Discord bot token from the Discord Developer Portal.This token authenticates your bot with Discord’s API and is used in src/index.js:45 to log in:
client.login(process.env.TOKEN);
How to get it:
  1. Go to the Discord Developer Portal
  2. Select your application
  3. Navigate to the “Bot” tab
  4. Click “Reset Token” or “Copy” to get your token
MONGO_URI
string
required
MongoDB connection string for database persistence.The bot connects to MongoDB in src/index.js:11 using Mongoose:
await mongoose.connect(process.env.MONGO_URI, { 
  useNewUrlParser: true, 
  useUnifiedTopology: true 
})
Format:
mongodb://username:password@host:port/database
Or for MongoDB Atlas:
mongodb+srv://username:[email protected]/database?retryWrites=true&w=majority
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.
CLIENT_ID
string
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
How to get it:
  1. Go to the Discord Developer Portal
  2. Select your application
  3. Copy the “Application ID” from the General Information tab
GUILD_ID
string
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:
  1. Enable Developer Mode in Discord (User Settings > Advanced > Developer Mode)
  2. Right-click on your test server
  3. Click “Copy ID”
Only use this during development. For production, register commands globally by omitting this variable.
LOG_CHANNEL_ID
string
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
WELCOME_CHANNEL_ID
string
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

# Required
TOKEN=your_discord_bot_token_here
MONGO_URI=mongodb://localhost:27017/yato-dev

# Optional - Development
CLIENT_ID=123456789012345678
GUILD_ID=987654321098765432

# Optional - Features
LOG_CHANNEL_ID=111111111111111111
WELCOME_CHANNEL_ID=222222222222222222

Environment variable reference

VariableTypeRequiredDescription
TOKENstringYesDiscord bot token for authentication
MONGO_URIstringYesMongoDB connection string
CLIENT_IDstringNoDiscord application client ID
GUILD_IDstringNoTest guild ID for development
LOG_CHANNEL_IDstringNoChannel ID for bot logs
WELCOME_CHANNEL_IDstringNoChannel ID for welcome messages

Security best practices

Follow these security guidelines to protect your bot and data:
  • Never commit .env: Always add .env to your .gitignore file
  • Rotate tokens regularly: If you suspect a token leak, regenerate it immediately
  • Use environment-specific files: Maintain separate .env.development and .env.production files
  • 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 the dotenv package to load environment variables. This happens automatically at the start of src/index.js:2:
require('dotenv').config();
This loads all variables from your .env file into process.env, making them accessible throughout the application.

Troubleshooting

Bot won’t start

If the bot fails to start, check:
  1. Missing TOKEN: Ensure TOKEN is set in your .env file
  2. Invalid TOKEN: Verify the token is correct and hasn’t been regenerated
  3. .env location: Ensure .env is in the project root, not in subdirectories
  4. Syntax errors: Check for missing quotes or special characters

MongoDB connection fails

If MongoDB connection fails:
  1. Check MONGO_URI format: Ensure the connection string is valid
  2. Verify credentials: Double-check username and password
  3. Network access: For MongoDB Atlas, add your IP to the IP whitelist
  4. Database exists: Ensure the database name in the URI exists or can be created
See the MongoDB configuration guide for detailed troubleshooting.

Build docs developers (and LLMs) love