Skip to content

feat(discord): multi-bot support with per-bot trigger injection#88

Open
stevengonsalvez wants to merge 1 commit into
nanocoai:mainfrom
stevengonsalvez:feat/discord-multi-bot
Open

feat(discord): multi-bot support with per-bot trigger injection#88
stevengonsalvez wants to merge 1 commit into
nanocoai:mainfrom
stevengonsalvez:feat/discord-multi-bot

Conversation

@stevengonsalvez
Copy link
Copy Markdown

Summary

  • Adds DISCORD_BOTS env var for running multiple Discord bots in a single NanoClaw instance, each with its own identity, JID prefix, and trigger name
  • Fixes trigger injection so each bot injects its own trigger on @mention and reply-to-bot, instead of the global ASSISTANT_NAME — without this fix, non-primary groups never match their trigger pattern in multi-bot setups
  • Fully backward-compatible: single DISCORD_BOT_TOKEN setup works identically to before

Motivation

When running multiple Discord bots (e.g., one per NanoClaw group with distinct personas), the original code injected the global ASSISTANT_NAME on @mention and reply-to-bot for all bots. This meant only the primary group's trigger matched — secondary groups never activated.

Config format

# Multi-bot (semicolon-separated entries: name:token:triggerName)
DISCORD_BOTS=engineer:xMTbot1token...:Engineer;ops:xMTbot2token...:Ops

Each entry produces:

  • Registry name: discord-{name} (e.g. discord-engineer)
  • JID prefix: dc-{name}: (e.g. dc-engineer:12345)
  • Trigger: @{triggerName} injected on @mention and reply-to-bot

Bot names are validated against /^[a-z0-9-]+$/i to prevent regex injection via JID prefix interpolation.

Changes

File Change
src/channels/discord.ts Parameterized constructor via DiscordBotOptions, per-bot triggerPattern, DISCORD_BOTS parser with name validation, multi-bot registration with single-bot fallback
src/channels/discord.test.ts Add buildTriggerPattern to config mock, 9 new tests for parser + options
src/db.ts Widen migration LIKE pattern to cover dc-%: multi-bot prefixes
.env.example Document DISCORD_BOTS format

Test plan

  • All 43 tests pass (34 original + 9 new)
  • Single-bot backward compat: DISCORD_BOT_TOKEN still works with dc: prefix
  • Multi-bot: two bots connect, each with distinct JID prefix and trigger name
  • @mention on bot A injects bot A's trigger (not global ASSISTANT_NAME)
  • Reply-to-bot-A injects bot A's trigger
  • Invalid bot names (dots, spaces, special chars) are rejected with warning
  • Malformed DISCORD_BOTS entries are skipped with warning
  • DISCORD_BOTS takes precedence over DISCORD_BOT_TOKEN with logged info message

Add DISCORD_BOTS env var for running multiple Discord bots in a single
NanoClaw instance. Each bot gets its own identity, JID prefix, and
trigger name for correct routing in multi-group setups.

Format: DISCORD_BOTS=name:token:triggerName;name:token:triggerName

Key changes:
- Parameterize DiscordChannel with jidPrefix, label, triggerName via
  options object (backward-compatible — single-token setup unchanged)
- Each bot injects its own trigger on @mention and reply-to-bot instead
  of the global ASSISTANT_NAME, fixing trigger routing for non-primary
  groups
- Validate bot names against /^[a-z0-9-]+$/i to prevent regex injection
  via JID prefix interpolation
- Strict 3-part colon parsing (name:token:triggerName) with warnings
  for malformed entries
- Export parseDiscordBots for testability with 9 new test cases covering
  valid entries, malformed input, name validation, and edge cases
- DB migration LIKE pattern covers both dc: and dc-name: prefixes
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant