async_ask_junkie
Run the user’s AI team with a query.User input text or prompt
Discord user ID
Session ID (typically channel ID)
List of Image objects for vision capabilities
Discord client instance
AI-generated response text
Example
setup_chat
Setup chat event handlers for the Discord bot.SelfBot instance to configure
Registered Events
This function registers the following Discord event handlers:on_ready
- Initializes database connection
- Connects MCP tools
- Starts message backfill task
- Syncs offline edits/deletes
on_message
- Handles chatbot prefix (
!) messages - Builds context from recent chat history
- Processes images from messages and replies
- Sets execution context for tools
- Generates and sends AI responses
on_message_edit
- Updates message cache when messages are edited
on_message_delete
- Updates message cache when messages are deleted
on_disconnect
- Closes database connections gracefully
Example
main_cli
CLI entrypoint for running Junkie in terminal mode.Behavior
- Creates a team for default CLI user
- Runs interactive CLI if terminal is available
- Gracefully closes MCP tools on exit
- Skips CLI app in non-interactive environments
Example
Message Processing Flow
- Message received → Cache updated
- Prefix detected (
!) → Extract prompt - Mentions resolved → Convert to readable format
- Context built → Include recent messages (default: 100)
- Reply context → Include replied message if present
- Images extracted → From message and reply
- Execution context set → Channel ID and object
- AI team invoked → Generate response
- Mentions restored → Convert back to Discord format
- Response sent → With timing information
Context Building
The chat handler builds context-aware prompts by:- Retrieving recent messages from cache (limit:
TEAM_LEADER_CONTEXT_LIMIT) - Including replied message for conversation threading
- Resolving user mentions to readable names
- Attaching image URLs from messages and replies
- Formatting with timestamps and user information
Error Handling
- Invalid responses → Returns fallback error message
- Team failures → Logs error and sends truncated error to user
- Backfill failures → Logs error but doesn’t block bot startup
- MCP connection failures → Logs warning and continues