|
| 1 | +--- |
| 2 | +name: add-discord |
| 3 | +description: Add Discord bot channel integration to NanoClaw. |
| 4 | +--- |
| 5 | + |
| 6 | +# Add Discord Channel |
| 7 | + |
| 8 | +This skill adds Discord support to NanoClaw, then walks through interactive setup. |
| 9 | + |
| 10 | +## Phase 1: Pre-flight |
| 11 | + |
| 12 | +### Check if already applied |
| 13 | + |
| 14 | +Check if `src/channels/discord.ts` exists. If it does, skip to Phase 3 (Setup). The code changes are already in place. |
| 15 | + |
| 16 | +### Ask the user |
| 17 | + |
| 18 | +Use `AskUserQuestion` to collect configuration: |
| 19 | + |
| 20 | +AskUserQuestion: Do you have a Discord bot token, or do you need to create one? |
| 21 | + |
| 22 | +If they have one, collect it now. If not, we'll create one in Phase 3. |
| 23 | + |
| 24 | +## Phase 2: Apply Code Changes |
| 25 | + |
| 26 | +### Ensure channel remote |
| 27 | + |
| 28 | +```bash |
| 29 | +git remote -v |
| 30 | +``` |
| 31 | + |
| 32 | +If `discord` is missing, add it: |
| 33 | + |
| 34 | +```bash |
| 35 | +git remote add discord https://github.com/qwibitai/nanoclaw-discord.git |
| 36 | +``` |
| 37 | + |
| 38 | +### Merge the skill branch |
| 39 | + |
| 40 | +```bash |
| 41 | +git fetch discord main |
| 42 | +git merge discord/main |
| 43 | +``` |
| 44 | + |
| 45 | +This merges in: |
| 46 | +- `src/channels/discord.ts` (DiscordChannel class with self-registration via `registerChannel`) |
| 47 | +- `src/channels/discord.test.ts` (unit tests with discord.js mock) |
| 48 | +- `import './discord.js'` appended to the channel barrel file `src/channels/index.ts` |
| 49 | +- `discord.js` npm dependency in `package.json` |
| 50 | +- `DISCORD_BOT_TOKEN` in `.env.example` |
| 51 | + |
| 52 | +If the merge reports conflicts, resolve them by reading the conflicted files and understanding the intent of both sides. |
| 53 | + |
| 54 | +### Validate code changes |
| 55 | + |
| 56 | +```bash |
| 57 | +npm install |
| 58 | +npm run build |
| 59 | +npx vitest run src/channels/discord.test.ts |
| 60 | +``` |
| 61 | + |
| 62 | +All tests must pass (including the new Discord tests) and build must be clean before proceeding. |
| 63 | + |
| 64 | +## Phase 3: Setup |
| 65 | + |
| 66 | +### Create Discord Bot (if needed) |
| 67 | + |
| 68 | +If the user doesn't have a bot token, tell them: |
| 69 | + |
| 70 | +> I need you to create a Discord bot: |
| 71 | +> |
| 72 | +> 1. Go to the [Discord Developer Portal](https://discord.com/developers/applications) |
| 73 | +> 2. Click **New Application** and give it a name (e.g., "Andy Assistant") |
| 74 | +> 3. Go to the **Bot** tab on the left sidebar |
| 75 | +> 4. Click **Reset Token** to generate a new bot token — copy it immediately (you can only see it once) |
| 76 | +> 5. Under **Privileged Gateway Intents**, enable: |
| 77 | +> - **Message Content Intent** (required to read message text) |
| 78 | +> - **Server Members Intent** (optional, for member display names) |
| 79 | +> 6. Go to **OAuth2** > **URL Generator**: |
| 80 | +> - Scopes: select `bot` |
| 81 | +> - Bot Permissions: select `Send Messages`, `Read Message History`, `View Channels` |
| 82 | +> - Copy the generated URL and open it in your browser to invite the bot to your server |
| 83 | +
|
| 84 | +Wait for the user to provide the token. |
| 85 | + |
| 86 | +### Configure environment |
| 87 | + |
| 88 | +Add to `.env`: |
| 89 | + |
| 90 | +```bash |
| 91 | +DISCORD_BOT_TOKEN=<their-token> |
| 92 | +``` |
| 93 | + |
| 94 | +Channels auto-enable when their credentials are present — no extra configuration needed. |
| 95 | + |
| 96 | +Sync to container environment: |
| 97 | + |
| 98 | +```bash |
| 99 | +mkdir -p data/env && cp .env data/env/env |
| 100 | +``` |
| 101 | + |
| 102 | +The container reads environment from `data/env/env`, not `.env` directly. |
| 103 | + |
| 104 | +### Build and restart |
| 105 | + |
| 106 | +```bash |
| 107 | +npm run build |
| 108 | +launchctl kickstart -k gui/$(id -u)/com.nanoclaw |
| 109 | +``` |
| 110 | + |
| 111 | +## Phase 4: Registration |
| 112 | + |
| 113 | +### Get Channel ID |
| 114 | + |
| 115 | +Tell the user: |
| 116 | + |
| 117 | +> To get the channel ID for registration: |
| 118 | +> |
| 119 | +> 1. In Discord, go to **User Settings** > **Advanced** > Enable **Developer Mode** |
| 120 | +> 2. Right-click the text channel you want the bot to respond in |
| 121 | +> 3. Click **Copy Channel ID** |
| 122 | +> |
| 123 | +> The channel ID will be a long number like `1234567890123456`. |
| 124 | +
|
| 125 | +Wait for the user to provide the channel ID (format: `dc:1234567890123456`). |
| 126 | + |
| 127 | +### Register the channel |
| 128 | + |
| 129 | +Use the IPC register flow or register directly. The channel ID, name, and folder name are needed. |
| 130 | + |
| 131 | +For a main channel (responds to all messages): |
| 132 | + |
| 133 | +```typescript |
| 134 | +registerGroup("dc:<channel-id>", { |
| 135 | + name: "<server-name> #<channel-name>", |
| 136 | + folder: "discord_main", |
| 137 | + trigger: `@${ASSISTANT_NAME}`, |
| 138 | + added_at: new Date().toISOString(), |
| 139 | + requiresTrigger: false, |
| 140 | + isMain: true, |
| 141 | +}); |
| 142 | +``` |
| 143 | + |
| 144 | +For additional channels (trigger-only): |
| 145 | + |
| 146 | +```typescript |
| 147 | +registerGroup("dc:<channel-id>", { |
| 148 | + name: "<server-name> #<channel-name>", |
| 149 | + folder: "discord_<channel-name>", |
| 150 | + trigger: `@${ASSISTANT_NAME}`, |
| 151 | + added_at: new Date().toISOString(), |
| 152 | + requiresTrigger: true, |
| 153 | +}); |
| 154 | +``` |
| 155 | + |
| 156 | +## Phase 5: Verify |
| 157 | + |
| 158 | +### Test the connection |
| 159 | + |
| 160 | +Tell the user: |
| 161 | + |
| 162 | +> Send a message in your registered Discord channel: |
| 163 | +> - For main channel: Any message works |
| 164 | +> - For non-main: @mention the bot in Discord |
| 165 | +> |
| 166 | +> The bot should respond within a few seconds. |
| 167 | +
|
| 168 | +### Check logs if needed |
| 169 | + |
| 170 | +```bash |
| 171 | +tail -f logs/nanoclaw.log |
| 172 | +``` |
| 173 | + |
| 174 | +## Troubleshooting |
| 175 | + |
| 176 | +### Bot not responding |
| 177 | + |
| 178 | +1. Check `DISCORD_BOT_TOKEN` is set in `.env` AND synced to `data/env/env` |
| 179 | +2. Check channel is registered: `sqlite3 store/messages.db "SELECT * FROM registered_groups WHERE jid LIKE 'dc:%'"` |
| 180 | +3. For non-main channels: message must include trigger pattern (@mention the bot) |
| 181 | +4. Service is running: `launchctl list | grep nanoclaw` |
| 182 | +5. Verify the bot has been invited to the server (check OAuth2 URL was used) |
| 183 | + |
| 184 | +### Bot only responds to @mentions |
| 185 | + |
| 186 | +This is the default behavior for non-main channels (`requiresTrigger: true`). To change: |
| 187 | +- Update the registered group's `requiresTrigger` to `false` |
| 188 | +- Or register the channel as the main channel |
| 189 | + |
| 190 | +### Message Content Intent not enabled |
| 191 | + |
| 192 | +If the bot connects but can't read messages, ensure: |
| 193 | +1. Go to [Discord Developer Portal](https://discord.com/developers/applications) |
| 194 | +2. Select your application > **Bot** tab |
| 195 | +3. Under **Privileged Gateway Intents**, enable **Message Content Intent** |
| 196 | +4. Restart NanoClaw |
| 197 | + |
| 198 | +### Getting Channel ID |
| 199 | + |
| 200 | +If you can't copy the channel ID: |
| 201 | +- Ensure **Developer Mode** is enabled: User Settings > Advanced > Developer Mode |
| 202 | +- Right-click the channel name in the server sidebar > Copy Channel ID |
| 203 | + |
| 204 | +## After Setup |
| 205 | + |
| 206 | +The Discord bot supports: |
| 207 | +- Text messages in registered channels |
| 208 | +- Attachment descriptions (images, videos, files shown as placeholders) |
| 209 | +- Reply context (shows who the user is replying to) |
| 210 | +- @mention translation (Discord `<@botId>` → NanoClaw trigger format) |
| 211 | +- Message splitting for responses over 2000 characters |
| 212 | +- Typing indicators while the agent processes |
0 commit comments