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.
Section titled Thread related gateway eventsThread related gateway events
You can use the BaseChannel#isThread() type guard to make sure a channel is a ThreadChannel!
Threads introduce a number of new gateway events, which are listed below:
- Client#threadCreate: Emitted whenever a thread is created or when the client user is added to a thread.
- Client#threadDelete: Emitted whenever a thread is deleted.
- Client#threadUpdate: Emitted whenever a thread is updated (e.g. name change, archive state change, locked state change).
- Client#threadListSync: Emitted whenever the client user gains access to a text or announcement channel that contains threads.
- Client#threadMembersUpdate: Emitted whenever members are added or
removed from a thread. Requires
GuildMembers
privileged intent. - Client#threadMemberUpdate: Emitted whenever the client user's thread member is updated.
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:
_10import { ThreadAutoArchiveDuration } from 'discord.js';_10_10const thread = await channel.threads.create({_10 name: 'food-talk',_10 autoArchiveDuration: ThreadAutoArchiveDuration.OneHour,_10 reason: 'Needed a separate thread for food',_10});_10_10console.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.
_10import { ThreadAutoArchiveDuration } from 'discord.js';_10_10const thread = await message.startThread({_10 name: 'food-talk',_10 autoArchiveDuration: ThreadAutoArchiveDuration.OneHour,_10 reason: 'Needed a separate thread for food',_10});_10_10console.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:
_10const thread = channel.threads.cache.find((x) => x.name === 'food-talk');_10if (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:
_10const thread = channel.threads.cache.find((x) => x.name === 'food-talk');_10if (thread.joinable) await thread.join();
And to leave one, use the ThreadChannel#leave() method:
_10const thread = channel.threads.cache.find((x) => x.name === 'food-talk');_10await 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:
_10const thread = channel.threads.cache.find((x) => x.name === 'food-talk');_10await thread.setArchived(true); // Archived._10await thread.setArchived(false); // Unarchived.
This same principle applies to locking and unlocking a thread via the ThreadChannel#setLocked() method:
_10const thread = channel.threads.cache.find((x) => x.name === 'food-talk');_10await thread.setLocked(true); // Locked._10await 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
:
_10import { ChannelType, ThreadAutoArchiveDuration } from 'discord.js';_10_10const 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_10console.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.
_10const thread = channel.threads.cache.find((x) => x.name === 'food-talk');_10await 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.
_10const thread = channel.threads.cache.find((x) => x.name === 'food-talk');_10await 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.
_10const webhooks = await channel.fetchWebhooks();_10const webhook = webhooks.first();_10_10await 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!