What You’ll Learn
- Creating modal responses to slash commands
- Building modal forms with text inputs
- Handling different input styles (short and paragraph)
- Working with required and optional fields
- Understanding modal component structure
Prerequisites
Before you begin, make sure you have:
- An Express server set up (see Express App example)
- A Discord application with a slash command configured
- Basic understanding of Discord interaction types
Complete Example
Understanding Modal Components
Modal Response Structure
When responding to an interaction with a modal:Component Types
Modals use numeric component types:| Type | Component | Description |
|---|---|---|
1 | Action Row | Container for input components |
4 | Text Input | Input field for text |
Action rows (type 1) are required containers for text inputs. Each text input must be wrapped in its own action row.
Text Input Structure
Input Styles
Short Input (style: 1)
Best for single-line text like usernames, titles, or short answers:Paragraph Input (style: 2)
Best for longer text like descriptions, feedback, or multi-line responses:Complete Modal Example
Here’s a more comprehensive example with modal submission handling:Handling Modal Submissions
When a user submits a modal, Discord sends aMODAL_SUBMIT interaction:
Extracting Values
Use Cases
User Registration
User Registration
Bug Report
Bug Report
Content Submission
Content Submission
Important Limitations
Validation
Implement validation in your submission handler:Best Practices
User Experience
User Experience
- Use clear, concise labels that explain what input is expected
- Provide helpful placeholder text as examples
- Mark truly optional fields with
required: false - Use appropriate min/max lengths to guide users
- Consider the user’s context when choosing short vs paragraph style
Data Handling
Data Handling
- Always validate user input before processing
- Sanitize input to prevent injection attacks
- Store modal submissions securely
- Consider rate limiting to prevent spam
- Provide clear feedback on successful submission
Error Handling
Error Handling
- Handle cases where users cancel the modal
- Validate required fields are present
- Provide helpful error messages
- Use ephemeral messages for error feedback
- Log errors for debugging
Next Steps
- Learn about message components for buttons and select menus
- Explore Express.js setup for the complete server
- Check out deferred responses for long-running operations
- Read about interaction types to understand the full interaction lifecycle