Section titled ThreadsThreads

Threads can be thought of as temporary sub-channels inside an existing channel to help better organize conversations in a busy channel.

You can use the BaseChannel#isThread() type guard to make sure a channel is a ThreadChannel!

Tip

Threads introduce a number of new gateway events, which are listed below:

Section titled Creating and deleting threadsCreating and deleting threads

Threads are created and deleted using the GuildTextThreadManager of a text or announcement channel. To create a thread, you call the GuildTextThreadManager#create() method:


_10
import { ThreadAutoArchiveDuration } from 'discord.js';
_10
_10
const thread = await channel.threads.create({
_10
name: 'food-talk',
_10
autoArchiveDuration: ThreadAutoArchiveDuration.OneHour,
_10
reason: 'Needed a separate thread for food',
_10
});
_10
_10
console.log(`Created thread: ${thread.name}`);

They can also be created from an existing message with the Message#startThread() method, but will be "orphaned" if that message is deleted.


_10
import { ThreadAutoArchiveDuration } from 'discord.js';
_10
_10
const thread = await message.startThread({
_10
name: 'food-talk',
_10
autoArchiveDuration: ThreadAutoArchiveDuration.OneHour,
_10
reason: 'Needed a separate thread for food',
_10
});
_10
_10
console.log(`Created thread: ${thread.name}`);

The created thread and the message it originated from will share the same id. The type of thread created matches the parent channel's type.

To delete a thread, use the ThreadChannel#delete() method:


_10
const thread = channel.threads.cache.find((x) => x.name === 'food-talk');
_10
if (thread.manageable) await thread.delete();

Section titled Joining and leaving threadsJoining and leaving threads

To subscribe your client to a thread, use the ThreadChannel#join() method:


_10
const thread = channel.threads.cache.find((x) => x.name === 'food-talk');
_10
if (thread.joinable) await thread.join();

And to leave one, use the ThreadChannel#leave() method:


_10
const thread = channel.threads.cache.find((x) => x.name === 'food-talk');
_10
await thread.leave();

Section titled Archiving, unarchiving, and locking threadsArchiving, unarchiving, and locking threads

A thread can be either active or archived. Changing a thread from archived to active is referred to as unarchiving the thread. Threads that have locked set to true can only be unarchived by a member with the ManageThreads permission.

Threads are automatically archived after inactivity. "Activity" is defined as sending a message, unarchiving a thread, or changing the auto-archive time.

To archive or unarchive a thread, use the ThreadChannel#setArchived() method and pass in a boolean parameter:


_10
const thread = channel.threads.cache.find((x) => x.name === 'food-talk');
_10
await thread.setArchived(true); // Archived.
_10
await thread.setArchived(false); // Unarchived.

This same principle applies to locking and unlocking a thread via the ThreadChannel#setLocked() method:


_10
const thread = channel.threads.cache.find((x) => x.name === 'food-talk');
_10
await thread.setLocked(true); // Locked.
_10
await thread.setLocked(false); // Unlocked.

Section titled Private threadsPrivate threads

Public threads are viewable by everyone who can view the parent channel of the thread. Private threads, however, are only viewable to those who are invited or have the ManageThreads permission. Private threads can only be created on text channels.

To create a private thread, use the GuildTextThreadManager#create() method and pass in ChannelType.PrivateThread as the type:


_10
import { ChannelType, ThreadAutoArchiveDuration } from 'discord.js';
_10
_10
const thread = await channel.threads.create({
_10
name: 'mod-talk',
_10
autoArchiveDuration: ThreadAutoArchiveDuration.OneHour,
_10
type: ChannelType.PrivateThread,
_10
reason: 'Needed a separate thread for moderation',
_10
});
_10
_10
console.log(`Created thread: ${thread.name}`);

Section titled Adding and removing membersAdding and removing members

You can add members to a thread with the ThreadMemberManager#add() method. The thread must be unarchived and you must be able to send messages in it.


_10
const thread = channel.threads.cache.find((x) => x.name === 'food-talk');
_10
await thread.members.add('12345678901234567');

You can remove members from a thread with the ThreadMemberManager#remove() method. The thread must be unarchived and you must have the ManageThreads permission unless the thread is private and you are the owner of it.


_10
const thread = channel.threads.cache.find((x) => x.name === 'food-talk');
_10
await thread.members.remove('12345678901234567');

Section titled Sending messages to threads with webhooksSending messages to threads with webhooks

It is possible for a webhook built on the parent channel to send messages to the channel's threads. For the purpose of this example, it is assumed a single webhook already exists for that channel. If you wish to learn more about webhooks, see our webhook guide.


_10
const webhooks = await channel.fetchWebhooks();
_10
const webhook = webhooks.first();
_10
_10
await webhook.send({
_10
content: "Look ma! I'm in a thread!",
_10
threadId: '123456789012345678',
_10
});

And that's it! Now you know all there is to know on working with threads using discord.js!