Multi-agent Discord bot system with batch and real-time message processing, circuit breaker protection, bot-to-bot communication support, and ACL (Agent Chain Length) limiting to prevent infinite bot conversations.
Startup: Discord Messages → processChannelMessages() → processBatchedMessages() → AI Response → Discord (with ACL)
Real-time: Discord Message → processRealtimeMessage() → AI Response → Discord (with ACL)
Message filtering, error detection, and debug utilities:
isOwnBotMessage(botUserId)- Excludes bot's own messagesisAfterCutoff(cutoffId)- Filters messages after cutoff IDisBotDMsRelevant(botUserId)- Bot-dms channel relevance filterisErrorResponse(response)- Detects Q CLI errorshandleErrorResponse(context, state, maxFailures, logFn)- Circuit breaker managementgetChannelSlowdown(channelId, client)- Channel rate limit detectiondebugBotDMsRouting(messages, botUserId, debugEnabled, logFn)- Debug logging
ACL (Agent Chain Length) system for bot conversation control:
getACL(message)- Extracts ACL from Discord message embed footercreateFooter(acl)- Creates embed footer with ACL metadataaddCourtesyMessage(query)- Adds courtesy message at ACL limitMAX_ACLconstant (4) - Maximum allowed chain length
Long message handling with ACL integration:
splitMessage(content)- Smart message chunking for Discord limitssendLongMessage(message, content)- Send with ACL footer and blocking
System monitoring and resource management:
checkLoadAverage(maxLoadAverage, logFn)- Monitor system load and exit if too high
Environment variable validation:
validEnvironment()- Validates required Discord/webhook configuration
Command-line help and usage documentation:
showHelp()- Display comprehensive usage information
processChannelMessages()- Batch processes missed messages during startupprocessBatchedMessages()- AI processing for message arraysprocessRealtimeMessage()- Single message AI processingcheckBotDMsChannel()- Special handling for bot-to-bot communication
- Guild text channels with ViewChannel permission
- Processes all messages where bot has access
- Direct messages between users and bot
- Cached and environment-configured channels
- Full message processing
- Special channel for bot-to-bot communication
- Filters: mentions, replies to bot, human messages only
- Prevents bot response loops
Prevents infinite bot-to-bot conversation loops by tracking and limiting response chains.
- Human messages: Start with ACL 0 (no ACL footer)
- Bot responses: Get ACL footer with incremented value
- Chain tracking: Each bot response increments the referring message's ACL
- Maximum limit: ACL 4 is the highest allowed (MAX_ACL = 4)
- Blocking: Messages that would create ACL 5+ are blocked from being sent
Bot responses include embed footers with ACL metadata:
acl:2 • Sent by a ZDS AI Agent • zds-agents.com
Human message (ACL 0) → Bot response (ACL 1) → Bot reply (ACL 2) → Bot reply (ACL 3) → Bot reply (ACL 4) → BLOCKED
- Outbound blocking:
sendLongMessage()blocks ACL 5+ responses - Courtesy message: ACL 4 responses include "For your information only" notice
- Inbound filtering: Messages with ACL ≥ MAX_ACL are filtered before agent processing
- Footer parsing:
getACL()extracts ACL from Discord embed footers
- MAX_FAILURES: 5 consecutive Q CLI errors triggers exit
- MAX_LOAD_AVERAGE: 21 system load triggers immediate exit
- Failure Tracking:
consecutiveFailurescounter with context logging - Load Monitoring: System load checked every 30 seconds
- Load last processed message IDs from persistence
- Check DM channels for missed messages
- Check bot-dms channel for relevant messages
- Check guild channels (unless --dms-only)
- Process queued real-time messages
- Enter real-time monitoring mode
- Queue messages during startup
- Apply bot message delay (17s + random 3s)
- Filter by mentions, DMs, or ViewChannel permission
- Process through AI and send responses
DISCORD_TOKEN - Bot authentication token
BOT_USER_ID - Bot's Discord user ID
WEBHOOK_ID - Webhook for responses
WEBHOOK_TOKEN - Webhook authentication
HTTP_PORT - Optional HTTP server port
BOT_MESSAGE_DELAY - Delay for bot message processing (default: 17000ms)
DM_CHANNEL_IDS - Comma-separated known DM channel IDs
--no-monitoring, -1- Process backlog then exit (no real-time monitoring)--no-agent- Skip agent processing (also skips Discord forwarding)--no-discord- Skip Discord forwarding (agent still processes)--scope=TYPE- Process specific channel types (dms|botdms|text|all)--show-backlog- Show backlog messages without processing (exits)--clear-backlog- Clear backlog without processing (exits)--debug- Enable verbose debug output--help, -h- Show help message
- Q CLI error detection and circuit breaker
- Discord delivery failure tracking
- Load average monitoring
- Graceful degradation and exit strategies