# Adding a Config File

Now that you have a bot up and running, we can start splitting it into some more useful parts. And the first part of this is separating some of the variables we have defined into a configuration file, config.json. We'll be loading this file on boot.

# Why a config file?

One of the advantages of having a configuration file is that you can safely copy your bot's code into, say, hastebin.com to show people, and your token won't be in there. A second advantage is that you can upload the code to a repository like github and, as long as you ignore the config file, your bot can be shared but remain secure. We'll see that in action in a future walk through.

# Step 1: The config file

The 2 things that we can add to the config file are:

  • The bot's token
  • The prefix

Simply take the following example, and create a new file in the same folder as your bot's file, calling it config.json:

{
  "token": "insert-bot-token-here",
  "prefix": "!"
}

# Step 2: Require the config file

At the top of your bot file, you need to add a line that will load this configuration, and put it in a variable. This is what it looks like:

const { Client,  Intents } = require("discord.js");
const client = new Client({
  intents: [Intents.FLAGS.GUILDS, Intents.FLAGS.GUILD_MESSAGES]
});
const config = require("./config.json");

This means that now, config is your configuration object. config.token is your token, config.prefix is your prefix! Simple enough.

# Step 3: Using config in your code

So let's use what we did earlier, and use the token from the config file, instead of putting it directly in the file. The last line of our bot looks like this:

client.login("SuperSecretBotTokenHere");

And we simply need to change it to this:

client.login(config.token);

The other thing we have, is of course the prefix. Again from before, we have this line in our message handler:

const prefix = "!";
client.on("messageCreate", (message) => {
  if (!message.content.startsWith(prefix) || message.author.bot) return;

  if (message.content.startsWith(`${prefix}ping`)) {
    message.channel.send("pong!");
  } else

  if (message.content.startsWith(`${prefix}foo`)) {
    message.channel.send("bar!");
  }
});

We're using prefix in a few places, so we need to change them all. Here's how it looks like after the changes:

client.on("messageCreate", (message) => {
  if (!message.content.startsWith(config.prefix) || message.author.bot) return;

  if (message.content.startsWith(`${config.prefix}ping`)) {
    message.channel.send("pong!");
  } else

  if (message.content.startsWith(`${config.prefix}foo`)) {
    message.channel.send("bar!");
  }
});

# Changing the config

If you're asking yourself "but how do I change the prefix, now?" fear not, we have some help for you. We suggest you start by reading the rest of this section of the guide ("First Bot") and then hop on to the Per-Server Configuration Guide on the Enmap Documentation!

# Extending the idea

So is there anything else you could put in that config file? Absolutely. One thing I use it for is to store my personal user ID, so that my bot can use it to recognize me and give me exclusive access to some commands.

{
  "token": "insert-bot-token-here",
  "prefix": "!",
  "ownerID": "your-user-ID"
}

Then, in a protected command (eval for example), I could use the following line to prevent access to all the users that think they can use it!:

if (message.author.id !== config.ownerID) return;

# What's next now?

So now that we have a functional bot with a configuration file, let's add more stuff to it! Follow me to the Command with arguments for the next part!