An AI-powered Discord bot that answers questions about channel message history using Claude.
- Question Answering: Ask the bot questions about past discussions in channels
- Intelligent Tool Use: Uses pydanticai agents that intelligently fetch relevant message history
- Natural Interaction: Simply mention the bot with your question
- Smart Chunking: Handles long responses by splitting at natural boundaries
- Error Handling: User-friendly error messages with optional debug logging
- Discord.py: Discord API integration and event handling
- PydanticAI: AI agent framework with tool calling capabilities
- Anthropic Claude: LLM for understanding questions and generating responses
- Pydantic Settings: Type-safe configuration management
-
Discord Bot Token
- Go to Discord Developer Portal
- Create a new application
- Go to Bot section and create a bot
- Enable "Message Content Intent" under Privileged Gateway Intents
- Copy the token
-
Invite Bot to Server
- In Developer Portal, go to OAuth2 > URL Generator
- Select scopes:
bot - Select permissions:
Read Messages,Send Messages,Read Message History - Use generated URL to invite bot to your server
-
Anthropic API Key
- Sign up at Anthropic Console
- Generate an API key
-
Clone and enter directory
cd discord-bot -
Install Python dependencies
poetry install
-
Configure environment
cp .env.example .env # Edit .env and add your tokensRequired variables:
DISCORD_TOKEN: Your Discord bot tokenANTHROPIC_API_KEY: Your Anthropic API key
Optional variables:
DEBUG_CHANNEL_ID: Discord channel ID for error loggingDEFAULT_MESSAGE_LIMIT: Max messages to fetch (default: 100)DEFAULT_TIME_WINDOW_HOURS: Default hours to look back (default: 24)MAX_RESPONSE_LENGTH: Max message length before chunking (default: 2000)
HAL-9000 supports observability through Langfuse for tracking AI agent performance, tool usage, and costs.
Setup:
- Create a Langfuse Cloud account at https://cloud.langfuse.com
- Get your public and secret keys from the dashboard
- Add to your
.envfile:LANGFUSE_PUBLIC_KEY=pk-lf-your_public_key_here LANGFUSE_SECRET_KEY=sk-lf-your_secret_key_here LANGFUSE_HOST=https://us.cloud.langfuse.com
What's Traced:
- All agent runs (inputs, outputs, timing, token usage)
- Tool calls (get_channel_id, fetch_messages) with parameters
- Errors and exceptions
If Langfuse credentials are not configured, the bot runs normally without observability.
poetry run python bot.pyMention the bot in any channel with a question:
@BotName what did we discuss about the deployment?
@BotName summarize today's conversation
@BotName who mentioned the bug fix?
The bot will:
- Show a typing indicator
- Use AI to determine what message history to fetch
- Analyze the messages
- Respond with an answer
# Run all tests
poetry run pytest
# Run with coverage
poetry run pytest --cov=. --cov-report=html
# Run specific test file
poetry run pytest tests/test_agent.py -vdiscord-bot/
├── bot.py # Discord client and message handling
├── agent.py # PydanticAI agent configuration
├── tools.py # Tool definitions for fetching messages
├── config.py # Settings with pydantic-settings
├── utils.py # Helper functions
├── tests/ # Test suite
│ ├── test_bot.py
│ ├── test_agent.py
│ ├── test_tools.py
│ ├── test_config.py
│ └── test_utils.py
└── docs/
└── plans/ # Implementation plans
To add new capabilities:
- Define tool function in
tools.py - Register tool with agent in
agent.pyusing@productivity_agent.tool - Update system prompt if needed
- Add tests in
tests/test_tools.py
Example:
# In tools.py
async def search_messages_tool(query: str, client: discord.Client):
# Implementation
pass
# In agent.py (within create_productivity_agent)
@agent.tool
async def search_messages(ctx: AgentContext, deps: AgentDependencies, query: str):
return await search_messages_tool(query, deps.discord_client)- Verify "Message Content Intent" is enabled in Discord Developer Portal
- Check bot has permissions in the channel
- Verify bot is actually online (check on_ready message in console)
- Check API keys are correct in
.env - Verify you have API credits (Anthropic)
- Check console for detailed error messages
- Run
poetry installto ensure all dependencies are installed - Verify you're using correct Python version (3.13+)
MIT