Building a Discord Bot in Node, TypeScript, and Eris
Here's a simple and straightforward guide to creating your first Discord bot with Eris and TypeScript. This guide introduces you to the essentials of writing a modular Discord bot that supports slash commands.
Prerequisites
You'll need these few tools and skills in order to complete this project.
- Node.js along with NPM
- A text editor or IDE, I personally use VSCode although you can use what you prefer
- A basic understanding of TypeScript/JavaScript concepts
Do you like what you're reading from the CoderOasis Technology Blog? We recommend reading our Implementing RSA in Python from Scratch series next.
The CoderOasis Community
Did you know we have a Community Forums and Discord Server? which we invite everyone to join us? Want to discuss this article with other members of our community? Want to join a laid back place to chill and discuss topics like programming, cybersecurity, web development, and Linux? Consider joining us today!
Creating the Project
First you'll want to setup your project directory:
discord-bot/
|__ bot.ts
|__ config.ts
|__ commands/
Once you have your project files created you'll need to install the required dependencies:
Once you've got your dependencies installed you'll want to run tsc --init
this will create a tsconfig.json
file, the only setting you'll want to change is the outDir
property to ourDir: "./out"
. Your tsconfig.json
file will be full of information about the other settings, although those aren't important right now.
Once you've added the out directory property it should look similar to this:
{
"compilerOptions": {
"target": "es2016" /* Set the JavaScript language version for emitted JavaScript and include compatible library declarations. */,
"module": "commonjs" /* Specify what module code is generated. */,
"outDir": "./out" /* Specify an output folder for all emitted files. */,
"esModuleInterop": true,
"skipLibCheck": true
}
}
Setting up the Main File
In the bot.ts
file, we'll be setting up the basics. We'll start by importing all the required modules from Eris, Node, and our configuration file that will be created later.
Next we'll get started with the structure of the bot by creating a Bot
class to store our commands. There's also a Command
type that is being created, which outlines the basic structure of a slash command. A setup like this will make it easy to manage and add commands within the bot:
Within the Bot
class, we'll need the loadCommands
function so that we can dynamically load commands from the commands
directory. We save the command name and the entire command object in the commands
property on the Bot
class, loading the commands dynamically makes scaling and command management much easier in the future.
Now we'll initialize the Bot
instance using a token provided in config.ts
and loading the commands. Once the bot is ready we'll log a message in the terminal and register the commands. We're mapping the commands without the run
function so that it is compatible with Discord's API and then registering them on Discord:
Finally we're going to setup an event listener to handle incoming interactions from Discord. When a user invokes a slash command we acknowledge it and destructure the name
property from the interaction, we then use the name to get the command object from client.commands
. We wrap the command.run
in a try...catch
statement so that we can catch any errors, where we alert the user of an error and log it to the terminal.
After setting up the event listener we connect to Discord to start listening for interactions.
Creating a Configuration File
In the config.ts
file created previously, we'll want to define a TOKEN
export for the bots token. You'll get this token from the Discord Developer page, in the bot
section of your application.
Creating Your Commands
You're going to want to make some command files, let's start with ping.ts
, you'll need to place this in your commands
directory for it to be loaded properly. Once you've created the file your structure should look like this:
discord-bot/
|__ bot.ts
|__ config.ts
|__ commands/
|__ ping.ts
In the ping.ts
file we'll create a simple command that responds with "Pong!" when it is ran.
Adding a Help Command
Assuming you want other users to see the commands your bot has, you'll want a help command. It'll list all the commands available and a description of each underneath it. You'll want to repeat the steps in the ping.ts
command
Your directory should now look like this:
discord-bot/
|__ bot.ts
|__ config.ts
|__ commands/
|__ ping.ts
|__ help.ts
The help
command should be dynamically loaded along with the ping
command whenever you start the bot.
Running the Bot
Now that you've got all the files written you're going to want to test the bot, to get started run the following:
tsc
node ./out/bot.js
You should see the following output:
BOT_NAME#0000 is now online!
Once you see that you're ready to add the bot to your server! Although be sure to use the applications.commands
and the bot
scope to ensure that you add it correctly. You can use this site to easily get an invite URL for your bot.
Here's a quick preview of what your bots interactions should look like:
The Conclusion
Now that you've got the basics down of creating a modular Discord bot with TypeScript and Eris the possibilities are endless. With a modular command structure you're able to easily extend the functionality of your bot.
If you'd like to run your Discord bot 24/7 then you can use a hosting provider like DigitalOcean, Oracle Cloud, or CoderOasis, along with pm2
. You can set it up by running the following commands in your terminal:
npm i -g pm2
pm2 start ./out/bot.js --name "Discord Bot"
To see the bot's logs you can use the logs
sub command, with the name of the process, in this case Discord Bot
:
pm2 logs "Discord Bot"
This should show you the last 15 lines of logging for the bot, you can use the --lines
option to show more.
Do you like what you're reading from the CoderOasis Technology Blog? We recommend reading our Implementing RSA in Python from Scratch series next.
The CoderOasis Community
Did you know we have a Community Forums and Discord Server? which we invite everyone to join us? Want to discuss this article with other members of our community? Want to join a laid back place to chill and discuss topics like programming, cybersecurity, web development, and Linux? Consider joining us today!
Do you like what you're reading from the CoderOasis Technology Blog? We recommend reading our Hacktivism: Social Justice by Data Leaks and Defacements as your next choice.
The CoderOasis Community
Did you know we have a Community Forums and Discord Server? which we invite everyone to join us? Want to discuss this article with other members of our community? Want to join a laid back place to chill and discuss topics like programming, cybersecurity, web development, and Linux? Consider joining us today!