Build A Bot (DiscordJS) — Better Logging And A Persistent Bot Config

Better logging

To start today’s session, we will implement a prettier solution for our console logs using Winston for the logging and chalk for the pretty colours.

npm i -S winston chalk

Read & Write Configs

Some of the tools we’re going to use for our config are prebaked in Node but in addition to those, we will need a way to work with json files, a way to create directories and to open files.

npm i -S jsonfile mkdirp opn

1) Setting the config directory

We will need a way to keep track of config file paths to a certain directory. We simply store those in our bot object.

2) Run it once initially

Here we are using the sanitise function we defined earlier to take the bot name and use it to create a directory for each bot. If you run the script on your own PC during test and development, the config files will be written to your home/user directory instead of the server’s respective directory. Simply check for files starting with .discord- followed by your bot's name.

3) Open generated config files for proofreading

Furthermore, I want to be able to open the files our script has created on the first run so that the user can check if his values have been merged correctly.

4) Check the configSchema

We also need a function to validate the user-supplied config and merge it with our schema to generate the new bot config. We’ll go through our schema step by step, compare the existence and type of the respective attribute in the bot config and either delete or overwrite it depending on our checks. For objects, it will call itself recursively layer by layer.

5) The big one, loadConfig

This is the place where it all comes together. I broke it down into 5 subsections that we will go through piece by piece.


If no old config is found, we simply create a new config.json in our chosen location using mkdirp, a small package resembling the desktop command mkdir -p, and prepare it with the most basic and important fields from what we are passing in on project start; discordToken, Prefix and


Next step, we load the config file, no matter if it’s an old one or we just created it.


Now call our configIterator with the config we read from the disk and compare it to our schema. As previously written, this makes sure that no old or mismatched values remain in the config once we decide to change the schema in the future.


Write the checked and clean config back to the server.


Last but not least, reload the config from the directory and check one last time. If everything is fine, execute the callback to continue and otherwise abort with an error.

Wrapping up

Using nodeJS for the first time to access and work with files can be a daunting task so depending on where you are/were with your experience, I hope I was able to keep it nice and basic and understandable.

Some words about me:

If you want to see more of my work and progress, feel free to follow me and check out my other articles. If you clap feverishly for the articles you like most, it will be easier for me to decide which directions to pursue in following articles so use your ability to cast a vote for future content.



Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Konrad Abe

Konrad Abe


I’m a Web / App Developer & father 👨‍👩‍👧 doing freelance and part-time agency work since 2003, 💻 building stuff on the side 🕹 and attending conferences 🎟