简体   繁体   中英

Discord.js: Fetch archived threads from specific/all channels

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?

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM