“Mods are asleep, post pictures of ducks.”

Running a Discord server can be demanding.The potential consequences of leaving a channel unattended are worrisome (hint: worse than duck spam). In an ideal world, community members would behave themselves and nothing would go wrong while the heroic moderator left to eat dinner or sleep.



That’s where bots come in. Discord allows users to create bots to perform a variety of functions including sending out scheduled messages, issuing warnings, or even playing music for the server.

How do you make a Discord bot? Set up a Discord server and access npm Create a new application in the developer portal Get your secure bot token Copy the bot into your server Make a folder for your bot's code Write your bot's application code Test and run your bot

How to create a Discord bot in 2019

Even without coding experience, creating a Discord bot isn’t as daunting as it sounds. Here’s a step-by-step guide to prove it.

1. Set up your Discord server and access npm

If you don’t have a Discord account and a server already, head to discordapp.com and set yourself up. Once you have an account, click the + button on the left side of your home screen to add a server.

You’ll use JavaScript to build your bot, and you’ll need access to npm, a package manager for JavaScript. While there are many ways to accomplish this, for our purposes we’ll use Node.js. Head to nodejs.org and install it. Lastly, make sure you have a text editor, the canvas for your code.

Create new application in developer portal

Now, just create a bot.

Okay, I’m pulling your leg. But, you will need an application on Discord to serve as the temporary shell of your bot.

Head over to your developer portal. Click New Application, then create a super cool name for your bot, like DuckWatch. Once you settle on a good name (allow up to three hours) click Create.

Pick an icon and description for your bot, then click the Bot button on the left side of the screen. From there, click Add Bot.

Get the bot’s token

With Bot button selected, you should see a clickable link that says “Click to Reveal Token.” Clicking it reveals your bot’s authorization token, allowing you to send code to your bot. You don’t want any old schmuck controlling your bot, so don’t share your bot’s authorization token with anyone. If your bot’s token gets into the wrong hands, make sure to click Regenerate to get a new, secure token.



For now, keep that token on hand.

Put your bot in your Discord server

From the developer portal, click OAuth2 on the left side of the screen. Under the Scopes header, select the box for Bot. Then click Copy just below that to grab the URL.

Paste the URL into a new tab or window to navigate there. From there, select your server and click Authorize.

Your lifeless husk of a bot should now appear as a new user in your server. Hooray!

Make a folder for your bot’s code

You’ll want a folder for the text files that make your bot tick. Create one and give it a simple, memorable name. (Something like “DuckBot,” but more relevant to your bot’s functionality.)

Make your bot’s files with your text editor

“package.json” and “bot.js” are a couple files that allow your Discord bot to operate correctly and provide relevant information about itself.

To create the first file, open your text editor and paste the following:

{

"name": "DuckWatch",

"version": "1.0.0",

"description": "No Ducks Allowed",

"main": "bot.js",

"author": "Your Name",

"dependencies": {}

}

Feel free to replace “DuckWatch,” “No Ducks Allowed,” and “Your Name” with more relevant text. This file is for informational purposes, so you’ll want something that makes sense to you if you need it. Save this file as “package.json” and put it in your bot’s code folder.

Now, initialize the bot’s folder. On Mac, you’ll want to right-click on the folder and select New Terminal at Folder. On Windows, you can hold down the Shift key and right-click the folder, then open a command window. In the Terminal or command window, paste the following:

npm install discord.js

After pasting, hit Enter. This uses npm, to grant access to some necessary libraries your bot will reference in its code. You’ll see some new files pop up in your bot folder when this is complete.

Next, you’ll need the code that tells your bot what to do and when to do it. This step is a great reason to learn to code even if you’re not a developer. In this case you’ll want to learn more about JavaScript to give yourself deeper control over your bot’s behavior, but for now you can paste the following to give your bot some functionality (namely banning users for sending the message “ducks”). Below is some functional code for that purpose:

const Discord = require('discord.js')

const client = new Discord.Client()

client.on('ready', () => {

console.log(`Logged in as ${client.user.tag}!`)

})

client.on('message', message => {

if (message.content === 'ducks') {

const guild = message.guild;

const member = guild.member(message.author);

message.reply('Banned.')

member.ban();

}

})

client.login('token')

Remember the authorization token you grabbed earlier? Replace the word token with your bot’s token, ensuring that the apostrophes remain intact. Remember, whoever has your bot’s authorization token can control your bot. If you think your bot’s token has been compromised make sure to navigate back to your bot’s token in the developer portal and click Regenerate for a new, secure token.

Save this file as “bot.js” in your Discord bot folder.

Run the bot

You’re all set! To run your bot, initialize your bot’s folder again (as described earlier) and type “node bot.js” in the command prompt or Terminal, followed by the Enter key. You should see your bot come online through Discord, at which point you can test it out! Hooray!



This guide has provided a solid foundation for building bots, but Discord bots can do much more than ban users for saying “ducks” if you’re willing to put in the research. They’re useful for sending automated messages at scheduled times, managing channel members, logging and documenting interactions for future review, and more.

More resources on bots

Check out additional resources on how to create bots on various social networks and communities, including: