Before You Begin
Before you start, you’ll need:- Node.js 18.4.0 or higher installed
- A Discord application created in the Discord Developer Portal
- Your application’s Public Key (found in the “General Information” section)
If you haven’t created a Discord application yet, visit the Discord Developer Portal and click “New Application” to get started.
Build Your First Bot
Create your server
Create a file named
index.js with the following code:- JavaScript
- TypeScript
index.js
Set your environment variables
Create a Replace
.env file in your project directory:.env
your_public_key_here with your application’s public key from the Discord Developer Portal.To use environment variables, install
dotenv and add require('dotenv').config() at the top of your file.Run your server locally
Start your server:Your server should now be running on
http://localhost:3000.Configure Discord
In the Discord Developer Portal:
- Go to your application’s General Information page
- Find the Interactions Endpoint URL field
- Enter your server URL followed by
/interactions(e.g.,https://your-server.com/interactions) - Click Save Changes
If verification fails, check that:
- Your server is running and accessible
- You’re using the correct public key
- Your endpoint URL ends with
/interactions
Create a slash command
To test your bot, you need to create a slash command:
- Go to your application in the Discord Developer Portal
- Navigate to the OAuth2 page
- Under OAuth2 URL Generator, select the
applications.commandsscope - Copy the generated URL and open it in your browser
- Select a server to add your bot to
Understanding the Code
What does verifyKeyMiddleware do?
What does verifyKeyMiddleware do?
The
verifyKeyMiddleware function verifies that incoming requests are actually from Discord by validating the Ed25519 signature in the request headers. This prevents unauthorized users from sending fake requests to your endpoint.It also automatically handles PING interactions that Discord sends to verify your endpoint is working.What are interaction types?
What are interaction types?
Discord sends different types of interactions:
PING(1) - Verification ping from DiscordAPPLICATION_COMMAND(2) - User invoked a slash commandMESSAGE_COMPONENT(3) - User clicked a button or selected from a menuAPPLICATION_COMMAND_AUTOCOMPLETE(4) - User is typing in an autocomplete fieldMODAL_SUBMIT(5) - User submitted a modal form
What are response types?
What are response types?
Your bot can respond to interactions in different ways:
PONG(1) - Acknowledge a PINGCHANNEL_MESSAGE_WITH_SOURCE(4) - Send a new messageDEFERRED_CHANNEL_MESSAGE_WITH_SOURCE(5) - Acknowledge and send a message laterDEFERRED_UPDATE_MESSAGE(6) - Acknowledge a component interactionUPDATE_MESSAGE(7) - Update the message the component was attached toAPPLICATION_COMMAND_AUTOCOMPLETE_RESULT(8) - Return autocomplete choicesMODAL(9) - Show a modal to the user
What about webhook events?
What about webhook events?
Webhook events are different from interactions. They notify your app about server activities like:
- User authorizations
- Entitlement creation
- Lobby messages
- Direct messages during Social SDK sessions
verifyWebhookEventMiddleware to handle these events with the same signature verification.Next Steps
Now that you have a working bot, explore more features:Handling Interactions
Learn about different interaction types and how to handle them
Message Components
Add buttons, select menus, and modals to your messages
Webhook Events
Receive real-time notifications about server activities
Express Integration
Advanced Express patterns and production deployment