diff options
| author | Edouard Oger <eoger@anthropic.com> | 2026-11-17 13:13:36 -0800 |
|---|---|---|
| committer | Edouard Oger <eoger@anthropic.com> | 2026-11-17 13:13:36 -0800 |
| commit | 41448d4184f113d70ee758a024b5bab1ef2ab0b5 (patch) | |
| tree | 799ad87d808eb122cc917cd4f6922fb4236ba3c2 | |
| parent | fd30cee3ecd5a8ee772733c5d20c0f2bae2da2f0 (diff) | |
feat(discord): add create_thread tooleoger/discord-create-thread
| -rw-r--r-- | external_plugins/discord/.claude-plugin/plugin.json | 2 | ||||
| -rw-r--r-- | external_plugins/discord/README.md | 2 | ||||
| -rw-r--r-- | external_plugins/discord/server.ts | 52 |
3 files changed, 55 insertions, 1 deletions
diff --git a/external_plugins/discord/.claude-plugin/plugin.json b/external_plugins/discord/.claude-plugin/plugin.json index 6418b1ec..16961db4 100644 --- a/external_plugins/discord/.claude-plugin/plugin.json +++ b/external_plugins/discord/.claude-plugin/plugin.json @@ -1,7 +1,7 @@ { "name": "discord", "description": "Discord channel for Claude Code \u2014 messaging bridge with built-in access control. Manage pairing, allowlists, and policy via /discord:access.", - "version": "0.0.4", + "version": "0.0.5", "keywords": [ "discord", "messaging", diff --git a/external_plugins/discord/README.md b/external_plugins/discord/README.md index ad04f6fa..0e290121 100644 --- a/external_plugins/discord/README.md +++ b/external_plugins/discord/README.md @@ -32,6 +32,7 @@ Navigate to **OAuth2** → **URL Generator**. Select the `bot` scope. Under **Bo - View Channels - Send Messages - Send Messages in Threads +- Create Public Threads - Read Message History - Attach Files - Add Reactions @@ -96,6 +97,7 @@ Quick reference: IDs are Discord **snowflakes** (numeric — enable Developer Mo | `edit_message` | Edit a message the bot previously sent. Useful for "working…" → result progress updates. Only works on the bot's own messages. | | `fetch_messages` | Pull recent history from a channel (oldest-first). Capped at 100 per call. Each line includes the message ID so the model can `reply_to` it; messages with attachments are marked `+Natt`. Discord's search API isn't exposed to bots, so this is the only lookback. | | `download_attachment` | Download all attachments from a specific message by ID to `~/.claude/channels/discord/inbox/`. Returns file paths + metadata. Use when `fetch_messages` shows a message has attachments. | +| `create_thread` | Start a public thread in an opted-in text or announcement channel. Takes `chat_id` + `name`, optionally `message_id` to start from a message. Returns the thread ID, which works as `chat_id` in the other tools. The thread follows its parent channel's access rules. Needs the Create Public Threads permission — if you invited the bot without it, add it to the bot's role. | Inbound messages trigger a typing indicator automatically — Discord shows "botname is typing…" while the assistant works on a response. diff --git a/external_plugins/discord/server.ts b/external_plugins/discord/server.ts index 0595fc7e..841704bf 100644 --- a/external_plugins/discord/server.ts +++ b/external_plugins/discord/server.ts @@ -22,6 +22,7 @@ import { GatewayIntentBits, Partials, ChannelType, + MessageType, ButtonBuilder, ButtonStyle, ActionRowBuilder, @@ -595,6 +596,23 @@ mcp.setRequestHandler(ListToolsRequestSchema, async () => ({ required: ['channel'], }, }, + { + name: 'create_thread', + description: + "Start a public thread in a server text or announcement channel. Use when someone asks for a thread, or to move a long side topic out of the channel. Returns the thread ID — post in it with reply, passing that ID as chat_id. @mention someone in the thread (<@user_id>) to add them to it. The parent channel's access rules, including its mention requirement, apply inside the thread.", + inputSchema: { + type: 'object', + properties: { + chat_id: { type: 'string', description: 'The parent channel.' }, + name: { type: 'string', description: 'Thread title, 1-100 characters.' }, + message_id: { + type: 'string', + description: 'Message in the parent channel to start the thread from, usually the one you are answering. Prefer this; omit it only for a standalone thread.', + }, + }, + required: ['chat_id', 'name'], + }, + }, ], })) @@ -705,6 +723,37 @@ mcp.setRequestHandler(CallToolRequestSchema, async req => { content: [{ type: 'text', text: `downloaded ${lines.length} attachment(s):\n${lines.join('\n')}` }], } } + case 'create_thread': { + const ch = await fetchAllowedChannel(args.chat_id as string) + if (ch.isThread()) { + throw new Error(`chat_id is already a thread — reply in it, or pass its parent channel ${ch.parentId} to start a new one`) + } + if (ch.type !== ChannelType.GuildText && ch.type !== ChannelType.GuildAnnouncement) { + throw new Error('threads need a server text or announcement channel, not a DM or voice chat') + } + const message_id = args.message_id as string | undefined + let text: string + try { + const thread = await ch.threads.create({ name: args.name as string, startMessage: message_id }) + text = `thread created (id: ${thread.id})` + } catch (err) { + const code = (err as { code?: unknown }).code + // Discord API errors: 50001 Missing Access, 50013 Missing Permissions. + if (code === 50001 || code === 50013) { + throw new Error(`${(err as Error).message} — check that the bot has Create Public Threads, View Channel and Read Message History in this channel`) + } + // 10008 Unknown Message. + if (code === 10008) throw new Error(`no message ${message_id} in channel ${ch.id} — it may be deleted or in another channel`) + // 160004: the message already has a thread, which shares the message's ID. + if (code !== 160004) throw err + text = `message already has a thread, which keeps its current name (id: ${message_id})` + } + text += ' — use it as chat_id to reply there.' + if (loadAccess().groups[ch.id]?.requireMention ?? true) { + text += ' As in the parent channel, people must @mention you or reply to one of your messages to reach you there.' + } + return { content: [{ type: 'text', text }] } + } default: return { content: [{ type: 'text', text: `unknown tool: ${req.params.name}` }], @@ -804,6 +853,9 @@ client.on('interactionCreate', async (interaction: Interaction) => { client.on('messageCreate', msg => { if (msg.author.bot) return + // First message of a thread started from a message: a system pointer back + // to that message, with no text of its own. + if (msg.type === MessageType.ThreadStarterMessage) return handleInbound(msg).catch(e => process.stderr.write(`discord: handleInbound failed: ${e}\n`)) }) |
