Listing entries in a ForumChannel?

I am wanting to list entries in a ForumChannel, but I am not sure how to go about it? I have a topic with two forum threads (if that is the right terminology?), but the following code returns 0:
async function listForumThreads (channelId: string, since?: Date) {
const client = await getClient();
const channel = client.channels.cache.get(channelId) as Channel;

if (channel?.type === ChannelType.GuildForum) {
console.log('Channel:', channel.name);
console.log('Topic:', channel.topic);
const channelForum = channel as ForumChannel;
const fetchedThreads = await channelForum.threads.fetch(undefined, { cache: false });

let idx = 0;
console.log('XX', fetchedThreads.threads.entries.length);
fetchedThreads.threads.forEach(message => {;
console.log(idx++, message.toJSON());
});
}

process.exit(1);
}
async function listForumThreads (channelId: string, since?: Date) {
const client = await getClient();
const channel = client.channels.cache.get(channelId) as Channel;

if (channel?.type === ChannelType.GuildForum) {
console.log('Channel:', channel.name);
console.log('Topic:', channel.topic);
const channelForum = channel as ForumChannel;
const fetchedThreads = await channelForum.threads.fetch(undefined, { cache: false });

let idx = 0;
console.log('XX', fetchedThreads.threads.entries.length);
fetchedThreads.threads.forEach(message => {;
console.log(idx++, message.toJSON());
});
}

process.exit(1);
}
I am trying to list the threads in a ForumChannel, their details and messages, but I am not sure how to go about his? - Discord.js 14.17.3 - Node v22.13.0
5 Replies
d.js toolkit
d.js toolkit23h ago
- What's your exact discord.js npm list discord.js and node node -v version? - Not a discord.js issue? Check out #other-js-ts. - Consider reading #how-to-get-help to improve your question! - Explain what exactly your issue is. - Post the full error stack trace, not just the top part! - Show your code! - Issue solved? Press the button! - Marked as resolved by OP
d.js docs
d.js docs20h ago
:method: GuildForumThreadManager#fetch() [email protected] Obtains a thread from Discord, or the channel cache if it's already available.
// Fetch a thread by its id
channel.threads.fetch('831955138126104859')
.then(channel => console.log(channel.name))
.catch(console.error);
// Fetch a thread by its id
channel.threads.fetch('831955138126104859')
.then(channel => console.log(channel.name))
.catch(console.error);
tamarok
tamarokOP8h ago
I had looked at this, but it only returns one thread. Not the list of threads in the channel.
d.js docs
d.js docs8h ago
:method: GuildForumThreadManager#fetchActive() [email protected] Obtains all active threads in the channel.
tamarok
tamarokOP8h ago
Turns out the issue was archived threads. This works for me, as a test bed:
async function displayThreads (threads: AnyThreadChannel[]) {
for (let i = 0; i < threads.length; i++) {
const messages = await threads[i].messages.fetch();
console.log('Name:', threads[i].name, `(msg count: ${messages.size})`);
messages.forEach(message => {
console.log('-', message.content);
});
}
}

async function listForumThreads (channelId: string, since?: Date) {
const client = await getClient();
const channel = client.channels.cache.get(channelId) as Channel;

if (channel?.type === ChannelType.GuildForum) {
console.log('Channel:', channel.name);
console.log('Topic:', channel.topic);

const forumChannel = channel as ForumChannel;

const threadCacheA = await forumChannel.threads.fetchActive(false);
const theadForumChannelsA = Array.from(threadCacheA.threads.entries());
console.log('XX', theadForumChannelsA.length);
displayThreads(Array.from(threadCacheA.threads.entries()).map(entry => entry[1]));

const threadCacheB = await forumChannel.threads.fetchActive(true);
const theadForumChannelsB = Array.from(threadCacheB.threads.entries());
console.log('YY', theadForumChannelsB.length);
displayThreads(Array.from(threadCacheB.threads.entries()).map(entry => entry[1]));

const threadCacheC = await forumChannel.threads.fetchArchived({ fetchAll: true });
const theadForumChannelsC = Array.from(threadCacheC.threads.entries());
console.log('ZZ', theadForumChannelsC.length);
displayThreads(Array.from(threadCacheC.threads.entries()).map(entry => entry[1]));
}

process.exit(1);
}
async function displayThreads (threads: AnyThreadChannel[]) {
for (let i = 0; i < threads.length; i++) {
const messages = await threads[i].messages.fetch();
console.log('Name:', threads[i].name, `(msg count: ${messages.size})`);
messages.forEach(message => {
console.log('-', message.content);
});
}
}

async function listForumThreads (channelId: string, since?: Date) {
const client = await getClient();
const channel = client.channels.cache.get(channelId) as Channel;

if (channel?.type === ChannelType.GuildForum) {
console.log('Channel:', channel.name);
console.log('Topic:', channel.topic);

const forumChannel = channel as ForumChannel;

const threadCacheA = await forumChannel.threads.fetchActive(false);
const theadForumChannelsA = Array.from(threadCacheA.threads.entries());
console.log('XX', theadForumChannelsA.length);
displayThreads(Array.from(threadCacheA.threads.entries()).map(entry => entry[1]));

const threadCacheB = await forumChannel.threads.fetchActive(true);
const theadForumChannelsB = Array.from(threadCacheB.threads.entries());
console.log('YY', theadForumChannelsB.length);
displayThreads(Array.from(threadCacheB.threads.entries()).map(entry => entry[1]));

const threadCacheC = await forumChannel.threads.fetchArchived({ fetchAll: true });
const theadForumChannelsC = Array.from(threadCacheC.threads.entries());
console.log('ZZ', theadForumChannelsC.length);
displayThreads(Array.from(threadCacheC.threads.entries()).map(entry => entry[1]));
}

process.exit(1);
}

Did you find this page helpful?