Build and host your first Discord bot using Node.js and AWS

Discord is one of the most popular chat applications in the world; with over 140 million active users, it’s likely that you and everyone you know already use it! Making your own Discord bot can be a fun way to add custom features to your Discord server and a good way to learn a bit about using the Discord API. In this guide we’ll use Discord.js, a Node.js library that allows for easy intraction with the Discord API, to make a Magic 8-Ball bot. We’ll also host the bot in the cloud using AWS. Will we succeed? 🎱 Signs point to yes. 🎱

Note

This guide assumes you have some experience with the following:

  • Node.js & npm
  • AWS and the AWS CLI (if you want to host your bot)
  • Magic 8-Balls

What is a Discord bot and how to they work?

At a high level, Discord bots connect to Discord servers in a manner very similar to how a user does. The bot recieves events from the server just like the regular Discord client and can be programmed to take actions when specific events occur. You may be surprised to discover that a Discord bot recieves every message and every event in every channel that it has access to. Privacy in the digital age? 🎱 Don’t count on it. 🎱

source: https://giphy.com/gifs/dudeyork-dude-york-xUA7b3UudQ619Y9AIM

Getting Started

To start building your first Discord bot you’ll need a few things:

Register the Bot

The first thing we’ll need to do is create a bot using the Discord developer portal. Click New Application and enter a name for the bot. On this page we can also add an icon and description.

Note

It’s always best practice to give your bot least privilege, meaning only the permissions it will need to perform its task.

Generating an OAuth link for the bot
Inviting the bot

Let’s get coding

In this step we’ll set up a simple Node.js project and bring the bot online. First, create a new project directory and cd into it:
mkdir magic-8-ball && cd magic-8-ball

DISCORD_BOT_TOKEN=your_token_here

Let the divinations begin

Now that the bot is alive, we can add the code needed to give this digital Magic 8-Ball the incredible powers of prescience! Add this code to the top of index.js where the other constants are defined.

Hosting your bot on AWS

You could stop at this point, but your bot will only work while you have the code running on your machine. To really complete this project we need to give the bot a home in the place where all bots wish to live: In the Cloud!
We’ll be running the bot on an AWS EC2 instance that we launch using the AWS CLI. If you don’t already have the AWS CLI configured you can read about how to do that here.

Note

This part of the guide assumes you have some experience with AWS EC2. If you’re new to AWS EC2, this resource is a great place to start.

Resources

Magic 8-Ball answers
Discord Developer Documentation
How To Build a Discord Bot with Node.js
discord.js
dotenv
setting up Node.js on AWS EC2
query for the latest Amazon Linux AMI ID

Software Developer, AWS certified. I love sci-fi🚀📚, Soylent🥛😋, and dark mode🌙🧑‍💻.

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