create_thread
Create a new thread in a Discord server channel to organize discussions around specific topics or messages.
Instructions
Create a new thread
Input Schema
TableJSON Schema
| Name | Required | Description | Default |
|---|---|---|---|
| guildId | Yes | The ID of the server (guild) | |
| channelId | Yes | The ID of the parent channel | |
| name | Yes | Name of the thread | |
| messageId | No | Message ID to start thread from | |
| autoArchiveDuration | No | Auto archive after minutes | |
| type | No | Thread type (default public) | |
| reason | No | Reason for creating |
Implementation Reference
- src/tools/thread-tools.ts:71-120 (handler)The handler function that implements the create_thread tool logic. It fetches the guild and channel, validates the channel type, maps autoArchiveDuration, and creates a thread either from a message or a new public/private thread using Discord.js API.async ({ guildId, channelId, name, messageId, autoArchiveDuration, type = 'public', reason }) => { const result = await withErrorHandling(async () => { const client = await getDiscordClient(); const guild = await client.guilds.fetch(guildId); const channel = await guild.channels.fetch(channelId); if (!channel || channel.type !== ChannelType.GuildText) { throw new Error('Channel does not support creating threads'); } const textChannel = channel as TextChannel; const archiveDurationMap: Record<string, ThreadAutoArchiveDuration> = { '60': ThreadAutoArchiveDuration.OneHour, '1440': ThreadAutoArchiveDuration.OneDay, '4320': ThreadAutoArchiveDuration.ThreeDays, '10080': ThreadAutoArchiveDuration.OneWeek, }; let thread; if (messageId) { const message = await textChannel.messages.fetch(messageId); thread = await message.startThread({ name, autoArchiveDuration: autoArchiveDuration ? archiveDurationMap[autoArchiveDuration] : undefined, reason, }); } else { thread = await textChannel.threads.create({ name, autoArchiveDuration: autoArchiveDuration ? archiveDurationMap[autoArchiveDuration] : undefined, type: type === 'private' ? ChannelType.PrivateThread : ChannelType.PublicThread, reason, }); } return { id: thread.id, name: thread.name, type: ChannelType[thread.type], parentId: thread.parentId, message: 'Thread created successfully', }; }); if (!result.success) { return { content: [{ type: 'text', text: result.error }], isError: true }; } return { content: [{ type: 'text', text: JSON.stringify(result.data, null, 2) }] }; }
- src/tools/thread-tools.ts:62-70 (schema)Zod schema defining the input parameters for the create_thread tool, including guildId, channelId, name, and optional fields like messageId, autoArchiveDuration, type, and reason.{ guildId: z.string().describe('The ID of the server (guild)'), channelId: z.string().describe('The ID of the parent channel'), name: z.string().describe('Name of the thread'), messageId: z.string().optional().describe('Message ID to start thread from'), autoArchiveDuration: z.enum(['60', '1440', '4320', '10080']).optional().describe('Auto archive after minutes'), type: z.enum(['public', 'private']).optional().describe('Thread type (default public)'), reason: z.string().optional().describe('Reason for creating'), },
- src/tools/thread-tools.ts:59-121 (registration)The server.tool() call that registers the create_thread tool, providing name, description, input schema, and handler function within the registerThreadTools function.server.tool( 'create_thread', 'Create a new thread', { guildId: z.string().describe('The ID of the server (guild)'), channelId: z.string().describe('The ID of the parent channel'), name: z.string().describe('Name of the thread'), messageId: z.string().optional().describe('Message ID to start thread from'), autoArchiveDuration: z.enum(['60', '1440', '4320', '10080']).optional().describe('Auto archive after minutes'), type: z.enum(['public', 'private']).optional().describe('Thread type (default public)'), reason: z.string().optional().describe('Reason for creating'), }, async ({ guildId, channelId, name, messageId, autoArchiveDuration, type = 'public', reason }) => { const result = await withErrorHandling(async () => { const client = await getDiscordClient(); const guild = await client.guilds.fetch(guildId); const channel = await guild.channels.fetch(channelId); if (!channel || channel.type !== ChannelType.GuildText) { throw new Error('Channel does not support creating threads'); } const textChannel = channel as TextChannel; const archiveDurationMap: Record<string, ThreadAutoArchiveDuration> = { '60': ThreadAutoArchiveDuration.OneHour, '1440': ThreadAutoArchiveDuration.OneDay, '4320': ThreadAutoArchiveDuration.ThreeDays, '10080': ThreadAutoArchiveDuration.OneWeek, }; let thread; if (messageId) { const message = await textChannel.messages.fetch(messageId); thread = await message.startThread({ name, autoArchiveDuration: autoArchiveDuration ? archiveDurationMap[autoArchiveDuration] : undefined, reason, }); } else { thread = await textChannel.threads.create({ name, autoArchiveDuration: autoArchiveDuration ? archiveDurationMap[autoArchiveDuration] : undefined, type: type === 'private' ? ChannelType.PrivateThread : ChannelType.PublicThread, reason, }); } return { id: thread.id, name: thread.name, type: ChannelType[thread.type], parentId: thread.parentId, message: 'Thread created successfully', }; }); if (!result.success) { return { content: [{ type: 'text', text: result.error }], isError: true }; } return { content: [{ type: 'text', text: JSON.stringify(result.data, null, 2) }] }; } );
- src/index.ts:63-63 (registration)Call to registerThreadTools(server) which includes the registration of create_thread among other thread tools.registerThreadTools(server);