Using timestamp to fetch messages history
discord.py has support for querying message history using timestamps (not only message ids) https://discordpy.readthedocs.io/en/latest/api.html#discord.TextChannel.history
I was looking for this feature in discord.js closest thing is https://discord.js.org/#%2Fdocs%2Fdiscord.js%2Fmain%2Fclass%2FMessageManager= but that accepts only snowflake.
channel.messages.fetch({ limit: 10, cache: false, after: '99539446449315840' })
Is there an official way or the hack-ish workaround would be encoding the timestamp into a dummy snowflake and use it to query the history?
i.e. if i want messages since 1658413979113
timestamp i do (1658413979113 - 1420070400000)<<22
?8 Replies
You can do that manually as shown or use the
SnowflakeUtil
exported from djs. (Doesn't seem to be documented anymore?)
Use the generate
method to generate your method https://www.sapphirejs.dev/docs/Documentation/api-utilities/classes/snowflake_src.Snowflake#generate
Where you can pass timestamp
as an option
https://www.sapphirejs.dev/docs/Documentation/api-utilities/interfaces/snowflake_src.SnowflakeGenerateOptions#timestampamazing, thanks a lot!
safe to use? Looks like it's formatted as a number (isnt there the overflow issue?)
its not a number, its a bigint
there is no overflow issue as bigint doesnt have a size limit
ah right so
.toString()
thank you very much!
one more thing
yields a collection of messages. Do i have to paginate myself so i dont store all messages in memory or is there something already implemented?you can only fetch 100 messages at a time
cache: false means it wont be added to the channels message manager, and will only stay in memory at that one
messages
variable
once it is deemed unused by the GC it will be freedokay that makes sense. I will fetch 100 at a time and paginate.
From your experience if i plan to process multiple channels (potentially all in a guild).
Does it make sense to process them in paralell or i would get rate limited even downloading messages from one channel at a time?
the latter
great, thanks a ton!