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:
_3node_modules_3.env_3config.json
_3node_modules_3.env_3config.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!
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_14const { Client, GatewayIntentBits } = require('discord.js');_14const { token } = require('./config.json');_14_14// Create a new client instance_14const client = new Client({ intents: [GatewayIntentBits.Guilds] });_14_14// When the client is ready, run this code (only once)_14client.once('ready', () => {_14console.log('Ready!');_14});_14_14// Login to Discord with your client's token_14client.login(token);
_14// Require the necessary discord.js classes_14const { Client, GatewayIntentBits } = require('discord.js');_14const { token } = require('./config.json');_14_14// Create a new client instance_14const client = new Client({ intents: [GatewayIntentBits.Guilds] });_14_14// When the client is ready, run this code (only once)_14client.once('ready', () => {_14console.log('Ready!');_14});_14_14// Login to Discord with your client's token_14client.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.