Search...
K

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.json file or by using environment variables.

Open your application in the Discord Developer Portal and go to the “Bot” page to copy your token.

Storing data in a config.json file is a common way of keeping your sensitive values safe. Create a config.json file in your project directory and paste in your token. You can access your token inside other files by using require().

config.json

_3
{
_3
"token": "your-token-goes-here"
_3
}

Usage

_3
const { token } = require('./config.json');
_3
_3
console.log(token);

If you’re using Git, you should not commit this file and should ignore it via .gitignore.

Caution

Environment variables are special values for your environment (e.g., terminal session, Docker container, or environment variable file). You can pass these values into your code’s scope so that you can use them.

One way to pass in environment variables is via the command line interface. When starting your app, instead of node index.js, use TOKEN=your-token-goes-here node index.js. You can repeat this pattern to expose other values as well.

You can access the set values in your code via the process.env global variable, accessible in any file. Note that values passed this way will always be strings and that you might need to parse them to a number, if using them to do calculations.

Command

_1
A=123 B=456 DISCORD_TOKEN=your-token-goes-here node index.js

Usage

_3
console.log(process.env.A);
_3
console.log(process.env.B);
_3
console.log(process.env.DISCORD_TOKEN);

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

_1
npm install dotenv

.env

_3
A=123
_3
B=456
_3
DISCORD_TOKEN=your-token-goes-here

If you’re using Git, you should not commit this file and should ignore it via .gitignore.

Caution

Git is a fantastic tool to keep track of your code changes and allows you to upload progress to services like GitHub, GitLab, or Bitbucket. While this is super useful to share code with other developers, it also bears the risk of uploading your configuration files with sensitive values!

You can specify files that Git should ignore in its versioning systems with a .gitignore file. Create a .gitignore file in your project directory and add the names of the files and folders you want to ignore:


_3
node_modules
_3
.env
_3
config.json

Aside from keeping credentials safe, node_modules should be included here. Since this directory can be restored based on the entries in your package.json and package-lock.json files by running npm install, it does not need to be included in Git. You can specify quite intricate patterns in .gitignore files, check out the Git documentation on .gitignore for more information!

Tip

Open your code editor and create a new file. We suggest that you save the file as index.js, but you may name it whatever you wish.

Here’s the base code to get you started:


_14
// Require the necessary discord.js classes
_14
const { Client, GatewayIntentBits } = require('discord.js');
_14
const { token } = require('./config.json');
_14
_14
// Create a new client instance
_14
const client = new Client({ intents: [GatewayIntentBits.Guilds] });
_14
_14
// When the client is ready, run this code (only once)
_14
client.once('ready', () => {
_14
console.log('Ready!');
_14
});
_14
_14
// Login to Discord with your client's token
_14
client.login(token);

This is how you create a client instance for your Discord bot and login to Discord. The GatewayIntentBits.Guilds intents option is necessary for your client to work properly, as it ensures that the caches for guilds, channels and roles are populated and available for internal use.

Intents also define which events Discord should send to your bot, and you may wish to enable more than just the minimum. You can read more about the other intents on the Intents topic.

Open your terminal and run node index.js to start the process. If you see “Ready!” after a few seconds, you’re good to go!

You can open your package.json file and edit the "main": "index.js" field to point to your main file. You can then run node . in your terminal to start the process! After closing the process with Ctrl + C, you can press the up arrow on your keyboard to bring up the latest commands you’ve run. Pressing up and then enter after closing the process is a quick way to start it up again.

Tip
null
Test