Threads
Threads can be thought of as temporary sub-channels inside an existing channel, to help better organize conversation in a busy channel.
Thread related gateway events
TIP
You can use the ThreadChannel#isThread()
open in new window type guard to make sure a channel is a ThreadChannel
open in new window!
Threads introduce a number of new gateway events, which are listed below:
Client#threadCreate
open in new window: Emitted whenever a thread is created or when the client user is added to a thread.Client#threadDelete
open in new window: Emitted whenever a thread is deleted.Client#threadUpdate
open in new window: Emitted whenever a thread is updated (e.g. name change, archive state change, locked state change).Client#threadListSync
open in new window: Emitted whenever the client user gains access to a text or news channel that contains threads.Client#threadMembersUpdate
open in new window: Emitted whenever members are added or removed from a thread. RequiresGuildMembers
privileged intent.Client#threadMemberUpdate
open in new window: Emitted whenever the client user's thread member is updated.
Creating and deleting threads
Threads are created and deleted using the GuildTextThreadManager
open in new window of a text or news channel. To create a thread you call the GuildTextThreadManager#create()
open in new window method:
const thread = await channel.threads.create({
name: 'food-talk',
autoArchiveDuration: ThreadAutoArchiveDuration.OneHour,
reason: 'Needed a separate thread for food',
});
console.log(`Created thread: ${thread.name}`);
2
3
4
5
6
7
To delete a thread, use the ThreadChannel#delete()
open in new window method:
const thread = channel.threads.cache.find(x => x.name === 'food-talk');
await thread.delete();
2
Joining and leaving threads
To join your client to a ThreadChannel, use the ThreadChannel#join()
open in new window method:
const thread = channel.threads.cache.find(x => x.name === 'food-talk');
if (thread.joinable) await thread.join();
2
And to leave one, use ThreadChannel#leave()
open in new window;
const thread = channel.threads.cache.find(x => x.name === 'food-talk');
await thread.leave();
2
Archiving, 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()
open in new window method and pass in a boolean parameter:
const thread = channel.threads.cache.find(x => x.name === 'food-talk');
await thread.setArchived(true); // archived
await thread.setArchived(false); // unarchived
2
3
This same principle applies to locking and unlocking a thread via the ThreadChannel#setLocked()
open in new window method:
const thread = channel.threads.cache.find(x => x.name === 'food-talk');
await thread.setLocked(true); // locked
await thread.setLocked(false); // unlocked
2
3
Public and private threads
Public threads are viewable by everyone who can view the parent channel of the thread. Public threads can be created with the GuildTextThreadManager#create()
open in new window method.
const thread = await channel.threads.create({
name: 'food-talk',
autoArchiveDuration: ThreadAutoArchiveDuration.OneHour,
reason: 'Needed a separate thread for food',
});
console.log(`Created thread: ${thread.name}`);
2
3
4
5
6
7
They can also be created from an existing message with the Message#startThread()
open in new window method, but will be "orphaned" if that message is deleted.
const thread = await message.startThread({
name: 'food-talk',
autoArchiveDuration: ThreadAutoArchiveDuration.OneHour,
reason: 'Needed a separate thread for food',
});
console.log(`Created thread: ${thread.name}`);
2
3
4
5
6
7
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.
Private threads behave similar to Group DMs, but in a Guild. Private threads can only be created on text channels.
To create a private thread, use GuildTextThreadManager#create()
open in new window and pass in ChannelType.PrivateThread
as the type
:
const { ChannelType, ThreadAutoArchiveDuration } = require('discord.js');
const thread = await channel.threads.create({
name: 'mod-talk',
autoArchiveDuration: ThreadAutoArchiveDuration.OneHour,
type: ChannelType.PrivateThread,
reason: 'Needed a separate thread for moderation',
});
console.log(`Created thread: ${thread.name}`);
2
3
4
5
6
7
8
9
10
Adding and removing members
You can add and remove members to and from a thread channel.
To add a member to a thread, use the ThreadMemberManager#add()
open in new window method:
const thread = channel.threads.cache.find(x => x.name === 'food-talk');
await thread.members.add('140214425276776449');
2
And to remove a member from a thread, use ThreadMemberManager#remove()
open in new window:
const thread = channel.threads.cache.find(x => x.name === 'food-talk');
await thread.members.remove('140214425276776449');
2
Sending 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.
const webhooks = await channel.fetchWebhooks();
const webhook = webhooks.first();
await webhook.send({
content: 'Look ma! I\'m in a thread!',
threadId: '123456789012345678',
});
2
3
4
5
6
7
And that's it! Now you know all there is to know on working with threads using discord.js!