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 using require().
- config.js
- Usage
module.exports = {
token: "[TOKEN]", // required
prefix: "!", // optional
nodes: [
{
host: "[IP_ADDRESS]", // required
port: 9999, // optional but recommended! Defaults to 443
password: "[PASSWORD]", // optional but recommended!
useSSL: false, // optional
maxRetryAttempts: 500, // optional
retryDelayMs: 300000, // optional
enableSessionResumeOption: true, // optional
sessionTimeoutSeconds: 300, // optional
},
],
};
const { token, prefix, nodes } = require('./config.js');
Using dotenvโ
Another common approach is storing these values in a .env file. Each line should hold a KEY=value pair.
Install the dotenv package :
- npm
- pnpm
- yarn
- bun
npm install dotenv
pnpm add dotenv
yarn add dotenv
# dotenv is not needed with Bun โ .env files are read automatically
dotenv does not natively support arrays. Store your node config as a JSON string and parse it at runtime, as shown below.
- .env
- Usage
TOKEN=""
PREFIX=""
LAVALINK_NODES=[{"host":"","port":9999,"password":"","useSSL":false,"maxRetryAttempts":500,"retryDelayMs":300000,"enableSessionResumeOption":true,"sessionTimeoutSeconds":300}]
require("dotenv").config();
const { TOKEN, PREFIX, LAVALINK_NODES } = process.env;
const nodes = JSON.parse(LAVALINK_NODES || "[]");
Resulting codeโ
You can review the complete example on the GitHub repository here .