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
space
space3y ago
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#timestamp
pocin
pocinOP3y ago
amazing, thanks a lot!
$node
> const djs = require('discord.js')
> djs.SnowflakeUtil.generate({timestamp: 1658413979113})
999685427247976448n
$node
> const djs = require('discord.js')
> djs.SnowflakeUtil.generate({timestamp: 1658413979113})
999685427247976448n
safe to use? Looks like it's formatted as a number (isnt there the overflow issue?)
Almeida
Almeida3y ago
its not a number, its a bigint there is no overflow issue as bigint doesnt have a size limit
pocin
pocinOP3y ago
ah right so .toString() thank you very much! one more thing
const messages = await channel.messages.fetch({
cache: false, // do not cache
after: dummyId
})
const messages = await channel.messages.fetch({
cache: false, // do not cache
after: dummyId
})
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?
Almeida
Almeida3y ago
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 freed
pocin
pocinOP3y ago
okay 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?
Almeida
Almeida3y ago
the latter
pocin
pocinOP3y ago
great, thanks a ton!
Want results from more Discord servers?
Add your server