How to let the bot read the chat history?

I have come across something like "channel.messages.fetch", but I'm not sure if this is still a working way to make the bot read the chat history, because I could not find any official document related to that 😢 Does anyone has any experience on this function?
5 Replies
d.js toolkit
d.js toolkit•12mo ago
- What's your exact discord.js npm list discord.js and node node -v version? - Post the full error stack trace, not just the top part! - Show your code! - Explain what exactly your issue is. - Not a discord.js issue? Check out #useful-servers. - Issue solved? Press the button!
duck
duck•12mo ago
that is the method for fetching existing, previously sent messages currently the docs are experiencing issues, so I'll mention a few details: - it's asynchronous and therefore returns a promise - it can fetch up to 100 messages at a time, with the (mutually exclusive) options before, after, and around accepting snowflakes to control which 100 messages are fetched - when fetching multiple messages, it will resolve in a Collection<string, Message> keyed by message id - it can also be used to fetch a single Message by id in which case it will just resolve in the Message object
Kangaroo
Kangaroo•12mo ago
Thanks @duck! I'm wondering when the last update of this function was. If I saw some related resources but it's from 2021, are they still applicable now? I'm also thinking to fetch the history using timestamp (e.g. within 5mins), should I take a look into the snowflakes thing? I'm very new to the discord api and JS stuffs🙈
duck
duck•12mo ago
If I saw some related resources but it's from 2021, are they still applicable now?
potentially not it's probable that you'd be looking at the v13 usage, and I don't remember off the top of my head whether it was a single object param back then
I'm also thinking to fetch the history using timestamp (e.g. within 5mins), should I take a look into the snowflakes thing?
because you can only fetch up to 100 messages at a time, it's entirely possible that you won't be able to fetch all messages within a given time frame without making copious amounts of requests, which would be considered api spam probably just best to work with a smaller amount of the most recent messages and simply <Collection>.filter() to ensure that you're only working with ones within the given time frame
Kangaroo
Kangaroo•12mo ago
Oh good to know! Thanks for answering my questions! I'll try it!