Configuration files
💡 This page follows the official discordjs.guide .
Once you add your bot to a server , the next step is to start coding and get it online! Let's start by creating a config file for your client token and a main file for your bot application.
As explained in the "What is a token, anyway?" section, your token is essentially your bot's password, and you should protect it as best as possible. This can be done through a config.js
file or by using environment variables.
Open your application in the Discord Developer Portal and go to the "Bot" page to copy your token
Using config.js
Storing data in a config.js
file is a common way of keeping your sensitive values safe. Create a config.js
file in your project directory and paste in your token. You can access your token inside other files by using require().
- config.js
- Usage
module.exports = {
token: "", // required
lastFmApiKey: "", // optional
nodes: [
{
host: "", // required
port: 9999, // required
password: "", // required
secure: false, // optional
retryAmount: 500, // optional
retryDelay: 300000, // optional
resumeStatus: true, // optional
resumeTimeout: 300, // optional
},
],
};
const { token, lastFmApiKey, nodes } = require('./config.json');
Using dotenv
Another common approach is storing these values in a .env
file. This spares you from always copying your token into the command line. Each line in a .env
file should hold a KEY=value
pair.
You can use the dotenv
package for this. Once installed, require and use the package to load your .env file and attach the variables to process.env:
- npm
- yarn
- pnpm
- bun
npm install dotenv
yarn add dotenv
pnpm add dotenv
# dotenv is not necessary with Bun # Bun reads .env files automatically
⚠️ DOTENV DOES NOT NATIVELY SUPPORT ARRAYS, BUT YOU CAN STORE ARRAYS SUITABLE FOR NODES BY SERIALIZING THEM INTO A STRING (e.g., JSON).
- .env
- Usage
TOKEN=""
LASTFM_API_KEY=""
LAVALINK_NODES=[{"host":"","port":9999,"password":"","secure":false,"retryAmount":500,"retryDelay":300000,"resumeStatus":true,"resumeTimeout":300}]
require("dotenv").config();
const { TOKEN, LASTFM_API_KEY, LAVALINK_NODES } = process.env;
// Parse LAVALINK_NODES JSON string
const nodes = JSON.parse(LAVALINK_NODES || "[]");
Resulting code
If you want to compare your code to the final result after completing all the steps in this guide, you can review it on the GitHub repository here .