0

I've been working on a Discord bot command to fetch all of the archived threads on a server. I'm currently running into the 100 limit and am looking for a way to get around that limit.

Here's a post I made previously and was able to get up to 100 threads fetched from that.

I found this post where they made a system to fetch more than 100 messages, but I haven't been able to successfully convert that into code to fetch more than 100 threads. Unfortunately, dates and orders have been a bit inconsistent when printing threads, so getting that data has been a challenge.

Here's the code that I have so far that's only fetching 100 threads:

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

    var textChannels = message.guild.channels.cache.filter(c => c.type === ChannelType.GuildText);

    textChannels.forEach(async (channel, limit = 500) => {
                let archivedThreads = await channel.threads.fetchArchived({limit: 100});
                console.log(archivedThreads);
            });
    }
}

In my final code, I also am printing this to a text file rather than the console, but I've left that additional code out here to keep this code more simplified for debugging.

Any ideas on how I can iterate through and print multiple sets of 100 threads at a time to bypass the limitations?

Eevee
  • 23
  • 1
  • 6
  • It's a bit hard to fully grasp the issue, but if a core issue that things are arriving out of order it's because you used `forEach` with async/await. Convert it to a regular loop and if possible stop using `forEach` altogether. – Evert Aug 15 '22 at 22:26
  • Thanks for the reply! So right now the biggest issue is that I'm unable to print more than 100 threads from a channel. I haven't found a way to successfully print the thread data out using a regular loop instead of ```forEach```, so that's why I've been using it that way. The data being out of order isn't as big of an issue, it just makes it hard for me to use the ```before:``` command that they recommended in the linked post for messages; and sometimes the dates print out strangely (i.e. Invalid Date or year 2494) so I haven't been able to successfully parse through them that way either. – Eevee Aug 15 '22 at 22:41

1 Answers1

0

The function passed to Array.forEach does not awaits, you may use for of operator or classic for loop inside an async function to await on threads for synchronous execution.

var textChannels = message.guild.channels.cache.filter(c => c.type === ChannelType.GuildText);

async function fetchArchives(){
   for (const channel of textChannels) {
      let archivedThreads = await channel.threads.fetchArchived({limit: 100});
      console.log(archivedThreads);
   }
}
Aquib Vadsaria
  • 350
  • 4
  • 9
  • Thank you for that clarification on that! When I tried out this code though I get this error: ```TypeError: Cannot read properties of undefined (reading 'fetchArchived')``` – Eevee Aug 15 '22 at 22:56
  • Based on your error, looks like `channel.threads` seems to be undefined – Aquib Vadsaria Aug 16 '22 at 21:53
  • Hmm yeah. For some reason, it doesn't have this error when it's referenced in the ```forEach``` loop, but happens when it's converted to a ```for``` loop, which is where I've been kind of stuck with this. Do you have any ideas on why it seems to run without this error in the ```forEach``` code in my post, but has the error with this code? – Eevee Aug 16 '22 at 23:23
  • Is this error for all threads or just for a particular thread? It would be safer to add a check before accessing threads since it could be undefined. if(channel.threads){ let archivedThreads = await channel.threads.fetchArchived({limit: 100}); console.log(archivedThreads); } – Aquib Vadsaria Aug 17 '22 at 10:50
  • So I think the issue is that in the ```forEach``` code, I'm defining ```channel``` in the ```forEach``` function (similar to what we're doing in the ```for``` loop here). But for some reason, it's not undefined in the context of the ```forEach``` when it's used, but it is when it's used in the ```for``` loop. Sorry, I'm still new to JavaScript/Discord API so I'm not entirely sure why there's a difference between those. When I use the if statement you provided in the ```for``` loop from your code, it's undefined, but when I use it in the ```forEach``` loop from the original, it shows threads. – Eevee Aug 17 '22 at 14:55