0

I'm working on a Discord bot that can fetch archived threads from various channels on my Discord server. Right now I have the code working where it will fetch the data from the current channel that the message is sent in, but I'm looking for a way to fetch thread data from all channels on the server (or at least allow me to specify which channel to print that data from so I can run all of my commands in my bot channel).

I'm on Discord.js version 14.1.2.

This is what I have so far:

client.on('messageCreate',  async function (message) {
    if (message.content === "!test"){
    const guild = client.guilds.cache.get("GUILD_ID_HERE"); // This has been removed here for privacy

    let archivedThreads = await message.channel.threads.fetchArchived()
    console.log(archivedThreads)
    }
}

I believe the reason it's only printing the current channel is because of the message. part of the code, but I haven't figured out a way to access archived threads without using that code.

Any ideas on how I can fetch thread data from all of the channels on the server?

Eevee
  • 23
  • 1
  • 6

1 Answers1

1

The fetchArchived() function is available for any channel, so you can just loop through all the guild's channels and fetch the threads for each of them:

var textChannels = message.guild.channels.cache.filter(c => c.type === ChannelType.GuildText);
textChannels.forEach(async (channel) => {
    let archivedThreads = await channel.threads.fetchArchived();
    console.log(archivedThreads);
});

Note: remember to import the ChannelType object from discord.js

isaac.g
  • 681
  • 1
  • 4
  • 12