Discord Bots — Developing and Hosting in Node.js

Miguel Peter
Nov 1 · 6 min read

For those who aren’t in the loop, Discord is a group chat app in a similar vein to GroupMe and especially Slack, which shares a very similar style and almost identical use case. However, Discord is much more about community and having fun among people who share similar hobbies and goals, the biggest attractor being gaming.

In Discord, one of the elements of having a fun or responsive channel is the presence of Discord bots. Discord bots are programmable interactive machines that can perform actions within a server, such as sending messages or posting images and video links. Want a bot that will help simplify moderating your channel? Circle and Pancake are good for that. Want a fun text-based chat game to play with friends or alone? Mythical and Pokecord are very popular and entertaining choices.

Rythm Logo

One popular bot is the Rythm bot, which allows users to play music in voice channels to add atmosphere or to just listen to music friends. Or alone, that works too. Rythm gets its music from SoundCloud and YouTube depending on what a user searches for as well as the link given to the bot and it adds a lot of fun and utility for your Discord server’s users to take advantage of.

Part of the fun of Discord bots, in my opinion, is making it and being able to customize what its capabilities are, and its a lot simpler than many developers think. For this, we’re going to walk through the process of creating, managing, and hosting a Discord bot. The prerequisites for accomplishing this are that you will want a code editor (such as NotePad++ or Visual Studio Code), and you will want to install Node.js onto the machine that you will be coding in and, by extension, have a comfortable knowledge of JavaScript, as this is the language that most if not all of this project will be written in.

First, we’ll need a Discord bot security key to allow the bot to connect to Discord when we want to test or host it. To acquire this, we will need to log into Discord and navigate to Discord’s developer portal. Once there we will need to create an application that will serve as the hub for our bot. After creating and naming our application, we create a bot and give it a name, and if you want, a profile image to be identified by.

This is our bot. Isn’t it cute?

It is probably for the best that until your bot is ready to be used by others, the Public Bot should be disabled to prevent outsiders from performing unsolicited “tests” during development.

You will notice a “TOKEN” option. Copy it down to your clipboard, as we will need it for our next steps. We finally get into some code! Open up your favorite text editor and make a new folder with three new files inside. This folder will contain all of your code for your bot. The three files we’ll call auth.json, bot.js, and package.json (though you can omit package.json through the console command npm init on the bot file).

In auth.json make a JSON object that has a key of “token” with the value of a string containing the “TOKEN” we copied earlier. Should look something like this:

{"token": "AUTH_TOKEN"}

Next in the package.json file, make another JSON object that has these properties:

{  "name": "bot_name",  "version": "1.0.0",  "description": "Bat Bot for Discord",  "main": "bot.js",  "scripts": {  "test": "test"  },  "author": "your_name",  "license": "ISC",}

The name, description, and author will all be of your choosing, but the rest should be the same in this file. Finally, the bot.js file is what is responsible for connecting your code to your Discord bot and giving the bot it’s options and capabilities.

In order to accomplish this, install Discord.io and Winston from npm. Discord.io’s package has access to a large set of methods that allow the bot to connect and interact with Discord servers and users, and Winston is a package that allows for the logging of errors and messages on your server or anywhere you want to send them. Once they are installed into your node_modules folder, go into your bot.js file and require both Winston and Discord.io into the file as well as your auth.json file.

I’ve skipped ahead a few steps but this is the necessary code to get the bot up and running:

var Discord = require('discord.io');var logger = require('winston');var auth = require('./auth.json');// Configure logger settingslogger.remove(logger.transports.Console);logger.add(new logger.transports.Console, {colorize: true});logger.level = 'debug';
// Initialize Discord Botvar bot = new Discord.Client({token: auth.token,autorun: true});
bot.on('ready', function (evt) {logger.info('Connected');logger.info('Logged in as: ');logger.info(bot.username + ' - (' + bot.id + ')');});

Below the “Initialize Discord Bot” comment, we make a new instance of the Discord client and pass the “TOKEN” from the auth.json file and set it to autorun so that when we start our bot, it should connect to any servers that it is a part of automatically. The on-ready function just below that logs when our server connects, which will appear when we start our server.

So now that we’ve created our bot, we need to invite it to a Discord guild in order to test it. Either create your own or invite it to an already-existing room (if you do not own the room, its always best to ask first). This must be done by going back to the developer portal and creating going to the OAuth2 tab of your application, then selecting bot from the list of OAuth scopes.

Once you select bot, copy the URL below that it gives you below the checklist and paste it into your search bar. From there, you’ll be prompted to select a server you wish to invite the bot to. Once that’s done, your bot with the assigned name should appear in the list of server members, but as offline. To start up your bot, just navigate your command line to your bot file and type node bot.js . You should see in your console the logger outputs we set in our on-ready function and the grey status turn green indicating the bot is online! Hooray!

So we’ve created a bot now, but have not created a way for it to interact with the server, so let’s make a simple test to make sure it can communicate properly on command. To do this, all that is needed is a listener for messages, which is provided by the on method we used for our on-ready function. Here is how we call it:

Here the bot reads all messages and looks for any that begin with “!”. Should one be sent by a user, the bot will check if the message after the “!” was “ping.” If so, the bot sends a message to the server via the bot.sendMessage function, in this case saying “Hello” for all of the server members to see! To test it, while your bot is online, send “!ping” in your test chat channel. The bot should reply very shortly after!

It’s alive! It’s ALIVE!

These are the simple tools needed to get a bot started. You can listen for other messages or commands and make the bot react differently between each message or send back images, videos, even music. I will be going over a few of these tools in the near future, but feel free to read the documentation on it on Discord’s developer portal and let your creativity run wild!

Miguel Peter

Written by

A current student at Operation Spark, I post explanations of the coding terminology and concepts I learn there.

Welcome to a place where words matter. On Medium, smart voices and original ideas take center stage - with no ads in sight. Watch
Follow all the topics you care about, and we’ll deliver the best stories for you to your homepage and inbox. Explore
Get unlimited access to the best stories on Medium — and support writers while you’re at it. Just $5/month. Upgrade