In this article, we’re going to tweak our environment to make development a little easier and setup our project for hosting later on.

Setting up Nodemon

To prevent us from having to restart our bot for every little change we make, we’re going to install Nodemon and configure our project to use it. Nodemon is a great tool for local development. It monitors your files & folders for changes, and will automatically restart your project if it detects any changes. This can save quite a bit of time by automating that process.

To install Nodemon, open your terminal and run the command npm install -g nodemon and wait for it to finish installing. Once done, open up your package.json file and modify (or add) the start script like below;

"scripts": {
  "start": "nodemon index.js",
  "test": "echo \"Error: no test specified\" && exit 1"
},

Now going forward, we can issue the command npm start and Nodemon will start monitoring our project for changes.

Setting up Environment Configs

The next thing we’re going to do is setup dotenv which is a package that allows you to parse the .env (Environment) configuration files. This file will contain sensitive information that we don’t necessarily want to commit to our repository. And since the project will already expect these secure values to be part of the environment config, whatever service you decide to host your bot on will support these values.

To install the package, open your terminal and enter npm install dotenv. This will install the package into your repository. Next create a file in the root of the project and name it .env. This is the default file that the package will import when it starts.

Now we need to make some changes to index.js. The first thing we need to do is require & configure the package we just installed. To do this, add this line to the first line in index.js;

require(‘dotenv’).config()

And finally, we want to move our Bot’s token into the .env file and replace it with process.env.DISCORD_BOT_TOKEN like so.

DISCORD_BOT_TOKEN = "your_token_here"

Here is the full index.js up til this point;

require('dotenv').config();
const Discord = require('discord.js')
const client = new Discord.Client();

client.on('ready', () => {
  console.log(`Logged in as ${client.user.tag}!`);
})

client.on('message', async msg => {
  if(msg.author.bot) {
    return
  }

  if(msg.content.startsWith("!hello")) {
    msg.reply("world!")
  }

  if(msg.content.startsWith("!dm")) {
    let messageContent = msg.content.replace("!dm", "")
    msg.member.send(messageContent)
  }

  if(msg.content.startsWith("!args")) {
    const args = msg.content.split(' ')
    let messageContent = ""
    if(args.includes("foo")) {
      messageContent += "bar "
    }
    if(args.includes("bar")) {
      messageContent += "baz "
    }
    if(args.includes("baz")) {
      messageContent += "foo "
    }
    msg.reply(messageContent)
  }

})

client.login(process.env.DISCORD_BOT_TOKEN);

Now to ensure our token never makes it into our repository, add .env to your .gitignore file. This will prevent the config from ever getting committed and keep your commits secure.