Welcome to Hack@Brown's Discord Bot starter kit! Not only is building a bot a fun and exciting project, but it also teaches valuable skills in programming and automation. Moreover, as Discord supports numerous media types (audio, video, text, images, etc.), possibilities are basically infinite!
In this kit, we're providing you with some boilerplate code, as well as a little guide to get started on building your very own bot! We provide steps on how to build an ultra cool bot that responds to a simple ping slash command. We've included some other ultra cool commands, but you can use this as a launchpad for the endless possibilities of your very own discord bot!
Table of Contents
- Prerequisites
- Boilerplate Structure
- About Discord and Bots
- Adding the Bot to Your Discord Server
- Building and Running the Starter Code
- Creating & Deploying Commands
- Other Ultra Cool Commands
- Useful Resources
Before getting started, make sure you have the following set up:
- A Discord Account: You’ll need a Discord account to create a server and invite your bot.
- Node.js: Ensure you have Node.js installed on your machine. You can download it from Node.js.
- Basic Knowledge of JavaScript/TypeScript: While we try to make this guide beginner-friendly, a basic understanding of programming will be helpful!
- A Code Editor: It’s recommended to use a code editor like Visual Studio Code to write and manage your code.
This boilerplate uses Node.js and the discord.js module, and is written in TypeScript. You can find the folder structure of our starter kit below:
.
├── src/
│ ├── commands/
│ │ └── ping.ts
│ ├── config/
│ │ └── IntentOptions.ts
│ ├── handlers/
│ │ └── onInteraction.ts
│ ├── interfaces/
│ │ └── ICommand.ts
│ ├── utils/
│ │ └── deployCommands.ts
│ └── index.ts
├── .env
├── .gitignore
├── package-lock.json
├── README.md
└── tsconfig.json
We have included comments in each file we wrote, that explain the purpose of the most fundamental parts of the code. Make sure to read them to better understand how the boilerplate works!
Discord is a free app for voice, video, and text chatting, mostly popular among gaming groups, study circles, and other communities with shared interests. It’s kind of like a mix between a chat room and a video call platform. People gather in “servers” (which are like chat rooms) and can chat in different “channels” within those servers. Servers are a great place to share conversations, media, and even add bots to help automate certain tasks.
A Discord bot is a program that can interact with your server and help you automate things. For example, it can respond to commands, moderate chat, play music, fetch info, and more. Essentially, bots make your server more interactive and fun by adding cool features that Discord doesn’t offer out of the box.
You’ve probably seen bots in action before—like when a bot welcomes new members to a server or gives you the weather when asked. With this starter kit, you’ll be able to build your own bot that can do all sorts of cool stuff!
To test your bot and actually use it, you'll need to create a "profile" for it and invite it to a Discord server. If you haven’t made a server yet, don’t worry! Let’s walk through how to create one.
If you don’t have a Discord server yet, here’s how to set one up:
- Open Discord (either the desktop app or in your browser).
- On the left sidebar, click the + icon at the bottom of your server list.
- Select "Create My Own" and give your server a name.
- Discord might ask you to choose your region, but it usually sets this automatically based on your location.
- Once your server is created, you’ll be the server owner with full control to manage it—this includes inviting your bot!
If you haven’t created a server yet, no worries! Let’s do it now and get everything set up.
Now that your server is ready, let’s invite your bot:
- Head over to the Discord Developer Portal and follow the instructions in the discord.js Guide to create your bot’s profile.
- Generate a bot token, then open the
.env
file in your project and paste it afterBOT_TOKEN=
. - You’ll also need your bot's Client ID (listed as "APPLICATION ID" in the Discord Developer Portal). Paste it into the
CLIENT_ID
field in your.env
file. - Finally, invite the bot to your server by generating an OAuth2 link:
- Navigate to the OAuth2 tab in the Developer Portal.
- Under OAuth2 URL Generator, select the bot scope and choose the permissions your bot needs.
- Copy the generated invite link, open it in your browser, and select your newly created server from the dropdown list to invite the bot.
And that’s it! Your bot should now be part of your server, ready to respond to commands. If you run into any issues, make sure your bot is online and double-check the token and permissions.
- cd into the
discordbot-starter
folder (or open the directory in your terminal) - Run
npm install
to download all required Node.js packages. - Run
npm run build
to build the TypeScript code. This will create abuild
folder, which contains the executable JavaScript code. - Finally, run
npm start
to start your bot up! The program should print "Bot is connected to Discord" if everything went well.
After you make any changes to the code, make sure to run npm run build
and npm start
again! As you should only be writing code in the src
folder, this ensures you build it (i.e., "convert" it to JavaScript) in order to run it.
One of the main ways of interacting with a Discord bot is through using slash commands. These commands are comparable to UNIX commands, and instruct the bot to do something. There are many other ways of interacting with a Discord bot, and you can find a list of events your bot can listen for here.
As commands are a little tedious to setup, we have done so in the boilerplate. Currently, only a single command is functional in the boilerplate (the /ping command). However, to make your bot actually receive commands, you must deploy them. Deploying means telling Discord servers about your commands: their name, and a description. To do so:
- cd into
discordbot-starter
- Run
npm run deploy
The script should say "Successfully reloaded 1 application (/) commands."
Now, head to Discord and open a text channel that the bot has access to. When typing /ping
, a menu should pop up:
Clicking the suggestion, and sending the command should have your bot say "pong!". If it does not work, make sure your bot is online!
- Create a new TypeScript file
<command name>.ts
inside of thesrc/commands
directory. Make sure the name of your file corresponds exactly to the name of the command you want to create. - Create a JavaScript object that implements the
ICommand
interface. The interface specifies that your object must have two fields,data
of typeSlashCommandBuilder
, and an asynchronousexecute
command. Thedata
field holds information about the command (its name, and description), while theexecute
function contains the code that should be ran when a user types a comand. That is where you will write all the code for your command. - Set your
module.exports
equal to the ICommand object you just created. This ensures that it is accessible from other TypeScript files in the project. - Run
npm run deploy
to deploy your new command to Discord's servers, before starting up your bot.
Useful comments can be found in the sample ping.ts
we provided.
This article from the discord.js Guide is also very useful for creating commands.
Here are some commands to showcase the endless possibilities of commands for your discord bot
- /gif: Search for GIFs!
- /shrug: Appends ¯_(ツ)_/¯ to the end of your message
- /tts: Text to speech, reads a message out loud
- /swiftie: Taylor Swift lyrics at random! Who doesn't love that?
- A great tutorial by Naomi Carrigan which helped me write this guide.
- The absolutely amazing discord.js Guide.
- discord.js Official Documentation