create_thread
Create a new thread in a Discord server channel to organize discussions or focus on specific topics.
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 Discord guild and channel, validates the channel type, maps auto-archive durations, and creates either a public/private thread or starts one from a message using Discord.js APIs. Returns JSON response or error.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:63-70 (schema)Zod schema defining the input parameters for the create_thread tool, including guild ID, channel ID, thread name, optional message ID, auto-archive duration, 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:61-121 (registration)Registration of the create_thread tool via server.tool() within the registerThreadTools function, specifying name, description, input schema, and handler.'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) }] }; } );