channel fetch

When u fetch messages from text channel does it also fetch messages in threads?
6 Replies
d.js toolkit
d.js toolkit5mo 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
Хепка
Хепка5mo ago
No, messages in thread fetching by ThreadManager, not TextChannel
Creeper
CreeperOP5mo ago
So if I wanted to fetch last 100 messages in a channel including threads how would I check if thread messages contribute to the recent 100
Хепка
Хепка5mo ago
const channelId = 'YOUR_CHANNEL_ID'; // Replace with your channel ID
const channel = await client.channels.fetch(channelId);

// Fetch the last 100 messages from the main channel
const mainChannelMessages = await channel.messages.fetch({ limit: 100 });

// Fetch active threads in the channel
const activeThreads = await channel.threads.fetchActive();
const archivedThreads = await channel.threads.fetchArchived();

let allMessages = [...mainChannelMessages.values()];

// Function to fetch messages from threads
const fetchThreadMessages = async (threadsCollection) => {
for (const thread of threadsCollection.threads.values()) {
const threadMessages = await thread.messages.fetch({ limit: 100 });
allMessages = allMessages.concat([...threadMessages.values()]);
}
};

// Fetch messages from all active threads
await fetchThreadMessages(activeThreads);

// Fetch messages from all archived threads
await fetchThreadMessages(archivedThreads);

// Sort messages by createdTimestamp
allMessages.sort((a, b) => b.createdTimestamp - a.createdTimestamp);

// Trim the array to the latest 100 messages
const latest100Messages = allMessages.slice(0, 100);
const channelId = 'YOUR_CHANNEL_ID'; // Replace with your channel ID
const channel = await client.channels.fetch(channelId);

// Fetch the last 100 messages from the main channel
const mainChannelMessages = await channel.messages.fetch({ limit: 100 });

// Fetch active threads in the channel
const activeThreads = await channel.threads.fetchActive();
const archivedThreads = await channel.threads.fetchArchived();

let allMessages = [...mainChannelMessages.values()];

// Function to fetch messages from threads
const fetchThreadMessages = async (threadsCollection) => {
for (const thread of threadsCollection.threads.values()) {
const threadMessages = await thread.messages.fetch({ limit: 100 });
allMessages = allMessages.concat([...threadMessages.values()]);
}
};

// Fetch messages from all active threads
await fetchThreadMessages(activeThreads);

// Fetch messages from all archived threads
await fetchThreadMessages(archivedThreads);

// Sort messages by createdTimestamp
allMessages.sort((a, b) => b.createdTimestamp - a.createdTimestamp);

// Trim the array to the latest 100 messages
const latest100Messages = allMessages.slice(0, 100);
Creeper
CreeperOP5mo ago
Wow thanks
Хепка
Хепка5mo ago
Mark as solved
Want results from more Discord servers?
Add your server