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

Time:08-06

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 1:

Here's where my code is at currently:

function getChannelIDs()
    {
      let channels = client.channels.cache.array();
      const threads = channels.cache.filter(channel => channel.isThread());
      console.log(threads);
    }

EDIT 2:

I've tried adding the code suggestions. Here's my entire bot code in case that's helpful:

const { AuditLogEvent } = require('discord.js');
const client = new Discord.Client();

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


// Suggestion 1
 /*client.on('message',  async message => {
  if (message.content == "!test") {
    const guild = message.guild;
    const channels = guild.channels.cache.filter(x => x.isThread());
      console.log(channels);
  }
 });*/


 // Suggestion 2
 client.on('message',  async message => {
  if (message.content == "!test") {
    // get the guild, in the actual code I've replaced "GUILD_ID" with my actual server, but not posting online here for privacy
    const guild = client.guilds.cache.get("GUILD_ID");
    // filter all the channels
    const threads = guild.channels.cache.filter(x => x.isThread());
    // 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(threadInfo);
  }
 });

  client.config = require("./config.json");
  client.login(client.config.token);

In both of the above suggestions, I now receive the TypeError: x.isThread is not a function console error.

EDIT 3:

I updated to Discord.js v14 and updated my code to this:

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!');
  })
  
   client.on('messageCreate',  async message => {
    if (message.content == "!threads") {
      // get the guild, in the actual code I've replaced "GUILD_ID" with my actual server, but not posting online here for privacy
      const guild = client.guilds.cache.get("GUILD_ID");
      // filter all the channels
      const threads = guild.channels.cache.filter(x => x.isThread());
      // 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(threadInfo);
    }
   });
  
    client.config = require("./config.json");
    client.login(client.config.token);

CodePudding user response:

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.

CodePudding user response:

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