Fetch all of the Discord threads on a server (Discord.js)

Viewed 196

I'm starting out in Discord.js and trying to make a bot that prints all of the thread data from the server to the console. For all threads on the server, I basically want it to print just the name of the thread, the member who created the thread, and the timestamp it was made.

Previously I was working on code for one that prints thread entries from the audit log, but because that data deletes after 45 days, I'm looking to make a more efficient strategy to print all threads that have ever been made since the beginning of the server (or at least this year).

I found this post on fetching all channel ids for the server, and that code works for me, but when I try to convert that code to find data on threads, I'm struggling to figure out how to do that.

Does anyone have any suggestions on how I could approach this?

EDIT 4:

The code is now working to print some thread data, but I can't print more than 100 entries. I found this post that might help with pulling that data, but I need to convert it to threads rather than messages to use that.

With my code to print using cached data, it only prints 5-10, but if I pull from the audit log, I'm able to print up to 100. I still don't think that's the method I want though because it will delete after 45 days and I'd like to at least pull all data one time, then from there I can use this command to just pull less data after the initial pull if I do it more frequently.

Here's my current code:

const { Client, GatewayIntentBits } = require('discord.js'); 
const client = new Client({ 
    intents: [
    GatewayIntentBits.Guilds,
    GatewayIntentBits.GuildMessages, 
    GatewayIntentBits.MessageContent
] 
});

client.on('ready', () =>{
    console.log(`${client.user.tag}` + ' is online!');
  })

    // should be more specific with message, (so it can be like "!audit")
    client.on('messageCreate',  async function (message) {
        const fetchedLogs = await message.guild.fetchAuditLogs({
            limit: 100,
            type: 110, // THREAD_CREATE
            action_type: 110 // THREAD_CREATE
        });

        //const fetchedChannels = await message.guild.channels.fetch();
    
        // Checks which command was done; message.content gets the message that the user sent to trigger this:
        if(message.content === "!test"){
            // get the guild
            const guild = client.guilds.cache.get("694998889358557297");
            // filter all the channels
            const threads = guild.channels.cache.sort((a, b) => a.createdAt - b.createdAt).filter(x => x.isThread());
    
              // Title of the Thread
              const threadTitle = threads.map(info => `${info.name}`);
    
              // ID of the Thread Creator
              const threadUser = threads.map(info => `${info.ownerId}`);
    
              // Date the Thread was Created
              const threadDate = threads.map(info => `${info.createdAt}`);
    
              // ALL INFO, if you want an array just remove the ".join()"
              const threadInfo = threads.map(info => `Name: ${info.name}\nCreator: ${info.ownerId}\nCreated at: ${info.createdAt}\n`).join("");
              console.log(threadTitle);
              //console.log(threadUser);
              //console.log(threadDate);
              //console.log(threadInfo);
          } 
          else {
                console.log ("target doesn't exist");
          }
    });
2 Answers

Threads are just channels that are parented to another channel, so you can fetch them all the same way as normal channels and then filter the result to only include threads.

const threads = channels.cache.filter(x => x.isThread());

This will result in the threads variable being an array of threads for the guild.

You can just filter through all the guild's channels and find the ones that are a thread.

// get the guild
const guild = client.guilds.cache.get("guild ID");
// filter all the channels
const threads = guild.channels.cache.filter(x => x.isThread());

Then, you can map the information that you need:

// if you want an array just remove the ".join()"
const threadInfo = threads.map(info => `Name: ${info.name}\nCreator: ${info.ownerId}\nCreated at: ${info.createdAt}\n`).join("");
constole.log(threadInfo);
Related