Note
π I have a blog post about this project on freeCodeCamp. Check it out here.
Note
This project demonstrates how graph-based workflows work in practice, using LangGraph to create intelligent, context-aware bot interactions with dynamic message routing and tool integration. Take this as an idea to how you can use LangGraph in unusual ways ;)
An AI-powered Discord bot that leverages LangGraph for sophisticated workflow management and Composio for seamless tool integration. The bot intelligently routes messages through a graph-based system, providing context-aware responses and automated support ticket management.
- Demonstrate Graph-Based AI Workflows: Show how LangGraph enables complex, intelligent decision-making
- Smart Message Processing: Automatically categorize and route Discord messages
- Tool Integration: Seamlessly connect with external services (Google Sheets, APIs, etc.)
- Support Automation: Intelligent support ticket classification and handling
- Contextual Responses: Maintain conversation context across different scenarios
- LangGraph - Graph-based workflow orchestration for AI applications
- Composio - Universal tool integration platform for AI agents
- Discord.js - Powerful library for interacting with Discord API
- OpenAI - Language model for intelligent message processing
- Bun - Fast JavaScript runtime and package manager
The bot uses a state graph with multiple nodes that process messages intelligently:
START β process-message β [routing decisions] β END
process-message
: Initial message categorization and routingprocess-support
: Support request classification and handlingprocess-tool-call
: External tool execution and integrationprocess-other
: General conversation and miscellaneous queriesprocess-support-question
: Specific support question handlingprocess-support-help
: Support assistance workflow
No hard reasons. Just to show you all how you can use it at places you don't think you can. This is a fun little project to visualize LangGraph.
Composio provides:
- Universal Tool Integration: Connect to 100+ tools and services
- Standardized Interface: Consistent API across different tools
- Authentication Management: Handle OAuth and API keys securely
- Action Execution: Reliable tool calling with proper error handling
- Extensibility: Easy to add custom tools and integrations
- Bun (latest version)
- Discord Bot Token
- OpenAI API Key
- Composio API Key (for tool integrations)
-
Clone the repository
git clone <repository-url> cd discord-bot-langgraph
-
Install dependencies
bun install
-
Environment Setup Create a
.env
file with:OPENAI_API_KEY=<YOUR_OPENAI_API_KEY> COMPOSIO_API_KEY=<YOUR_COMPOSIO_API_KEY> DISCORD_BOT_TOKEN=<YOUR_DISCORD_BOT_TOKEN> DISCORD_BOT_GUILD_ID=<YOUR_DISCORD_BOT_GUILD_ID> DISCORD_BOT_CHANNEL_ID=<YOUR_DISCORD_BOT_CHANNEL_ID>
-
Deploy Slash Commands
bun run commands:deploy
-
Start the Bot
# Development mode (with hot reload) bun run dev # Production mode bun start
discord-bot-langgraph/
βββ src/
β βββ index.ts # Bot entry point and Discord client setup
β βββ graph.ts # LangGraph workflow definition
β βββ nodes.ts # Individual workflow nodes
β βββ edges.ts # Conditional edge logic
βββ types/
β βββ types.ts # TypeScript type definitions
βββ utils/
β βββ env-validator.ts # Environment variable validation
β βββ helpers.ts # Utility functions
β βββ logger.ts # Logging configuration
β βββ slash-deploy.ts # Slash command deployment
βββ graph/
β βββ graph.png # Visual representation of the workflow
βββ package.json # Dependencies and scripts
- Message Received: Discord message triggers the workflow
- Initial Processing:
process-message
node categorizes the message - Routing Decision: Based on AI analysis, message is routed to:
- Support Path: For help requests, bugs, or questions
- Tool Path: For actions requiring external tool integration
- General Path: For casual conversation or other queries
- Response Generation: Appropriate response is generated and sent
- Context Preservation: Message history is maintained for follow-ups
- Automatic Classification: AI determines if message is a support request
- Category Assignment: Support requests are further categorized (question, help, bug)
- Thread Creation: Complex issues automatically create Discord threads
- Context Tracking: Maintains conversation history across thread interactions
- Dynamic Tool Calling: AI decides when external tools are needed
- Iteration Support: Up to 5 iterations for complex tool sequences
- Error Handling: Graceful handling of tool failures with user feedback
- Response Formatting: Tool outputs are formatted for Discord presentation
# Development
bun run dev # Start with hot reload
bun run start # Production start
bun run build # Build for production
# Utilities
bun run commands:deploy # Deploy slash commands
- Fork the repository
- Create a feature branch (
git checkout -b feature/amazing-feature
) - Commit changes (
git commit -m 'Add amazing feature'
) - Push to branch (
git push origin feature/amazing-feature
) - Open a Pull Request
- Intelligent Message Routing - AI-powered message categorization
- Automated Support Tickets - Context-aware support request handling
- External Tool Integration - Seamless connection to external services
- Context Preservation - Maintains conversation history and context
- Dynamic Thread Creation - Automatic thread management for complex discussions
- Structured Responses - Consistent, well-formatted bot responses
- Iterative Processing - Multi-step tool execution capabilities
- Error Handling - Robust error handling and user feedback
This project is mainly build as a practical example of how graph-based workflows can create more intelligent and flexible AI applications. Unlike traditional linear bot logic, the graph approach allows for:
- Dynamic Path Selection: The bot chooses different paths based on message analysis
- State Management: Information flows between nodes maintaining context
- Conditional Logic: Complex decision trees that adapt to user input
- Extensible Design: New capabilities can be added as additional nodes
- Visual Representation: The workflow can be visualized and understood easily