Discord has an excellent API for writing custom bots, and a very active bot community. Today we’ll take a look at how to get started making your own.

You will need a bit of programming knowledge to code a bot, so it isn’t for everyone, but luckily there are some modules for popular languages that make it very easy to do. We’ll be using the most popular one, discord.js.

RELATED: How to Create, Set Up, and Manage Your Discord Server

Getting Started

Head over to Discord’s bot portal, and create a new application.

You’ll want to make a note of the Client ID and secret (which you should keep a secret, of course). However, this isn’t the bot, just the “Application.” You’ll have to add the bot under the “Bot” tab.

Make a note of this token as well, and keep it a secret. Do not, under any circumstances, commit this key to Github. Your bot will be hacked almost immediately.

Install Node.js and Get Coding

To run Javascript code outside of a webpage, you need Node. Download it, install it, and make sure it works in a terminal (or Command Prompt, as all of this should work on Windows systems). The default command is “node.”

We also recommend installing the nodemon tool. It’s a command line app that monitors your bot’s code and restarts automatically on changes. You can install it by running the following command:

npm i -g nodemon

You’ll need a text editor. You could just use notepad, but we recommend either Atom or VSC.

Here’s our “Hello World”:

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

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

client.on('message', msg => {
 if (msg.content === 'ping') {
 msg.reply('pong');
 }
 });

client.login('token');

This code is taken from the discord.js example. Let’s break it down.

  • The first two lines are to configure the client. Line one imports the module into an object called “Discord,” and line two initializes the client object.
  • The client.on('ready') block will fire when the bot starts up. Here, it’s just configured to log its name to the terminal.
  • The client.on('message') block will fire everytime a new message is posted to any channel. Of course, you’ll need to check the message content, and that’s what the if block does. If the message just says “ping,” then it will reply with “Pong!”
  • The last line logs in with the token from the bot portal. Obviously, the token in the screenshot here is fake. Don’t ever post your token on the internet.

Copy this code, paste in your token at the bottom, and save it as index.js in a dedicated folder.

How to Run the Bot

Head over to your terminal, and run the following command:

nodemon --inspect index.js

This starts up the script, and also fires up the Chrome debugger, which you can access by typing chrome://inspect/  into Chrome’s Omnibar and then opening “dedicated devtools for Node.”

Now, it should just say “Logged in as <bot-name>,” but here I’ve added a line that will log all message objects received to the console:

So what makes up this message object? A lot of stuff, actually:

Most notably, you have the author info and the channel info, which you can access with msg.author and msg.channel. I recommend this method of logging objects to the Chrome Node devtools, and just looking around to see what makes it work. You may find something interesting. Here, for example, the bot logs its replies to the console, so the bot’s replies trigger client.on('message'). So, I made a spambot:

Note: Be careful with this, as you don’t really want to deal with recursion.

How to Add the Bot to Your Server

This part is harder than it should be. You have to take this URL:

https://discordapp.com/oauth2/authorize?client_id=CLIENTID&scope=bot

And replace CLIENTID with your bot’s client ID, found on the general information tab of the application page. Once this is done though, you can give the link to your friends to have them add the bot to their servers as well.

Alright, So What Else Can I Do?

Beyond basic setup, anything else is entirely up to you. But, this wouldn’t be much of a tutorial if we stopped at hello world, so let’s go over some of the documentation, so you have a better idea of what’s possible. I suggest you read through as much as you can, as it’s very well documented.

I would recommend adding console.log(client) to the start of your code, and taking a look at the client object in the console:

From here, you can learn a lot. Since you can add a bot to multiple servers at once, servers are part of the Guilds map object. In that object are the individual Guilds (which is the API’s name for “server”) and those guild objects have channel lists that contain all the info and lists of messages. The API is very deep, and may take a while to learn, but at least it’s easy to set up and get started learning.

Anthony Heddings Anthony Heddings
Anthony Heddings is a tech writer, programmer, and amateur YouTuber. He joined the team in 2015 and focuses on covering Mac content, explaining technology, and sharing anything that makes his workflow a little easier.
Read Full Bio »