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);