how do you fetch all messages created after a specific date?

self explanatory
33 Replies
d.js toolkit
d.js toolkit5mo ago
- What's your exact discord.js npm list discord.js and node node -v version? - Not a discord.js issue? Check out #other-js-ts. - Consider reading #how-to-get-help to improve your question! - Explain what exactly your issue is. - Post the full error stack trace, not just the top part! - Show your code! - Issue solved? Press the button!
d.js docs
d.js docs5mo ago
:method: GuildMessageManager#fetch @14.15.3 Fetches message(s) from a channel. The returned Collection does not contain reaction users of the messages if they were not cached. Those need to be fetched separately in such a case.
// Fetch a message
channel.messages.fetch('99539446449315840')
.then(message => console.log(message.content))
.catch(console.error);
// Fetch a message
channel.messages.fetch('99539446449315840')
.then(message => console.log(message.content))
.catch(console.error);
IcyPickle
IcyPickleOP5mo ago
is there a function for fetching all messages in a guild at once or do i have to manually make a loop for it
chewie
chewie5mo ago
loop
d.js docs
d.js docs5mo ago
:propertysignature: FetchMessagesOptions#after @14.15.3 Consider only messages after this id
IcyPickle
IcyPickleOP5mo ago
🙏
NyR
NyR5mo ago
The id here for after is just a snowflake, you can construct one for a specific Date/time by using SnowflakeUtil and pass that
IcyPickle
IcyPickleOP5mo ago
i've hit a wall, it's saying fetch() is undefined for some reason, and chnl is a channel object
const streak = await (async () => {
const timestamp = SnowflakeUtil.generate(Date.now() - (1000 * 60 * 60 * 24));
let msgCountInGuild = 0;
for (const [id, chnl] of await message.guild.channels.fetch()) msgCountInGuild += ((await chnl.messages.fetch({after: timestamp})).filter((id, msg) => msg.author.id === user.id).size)
return Math.floor(msgCountInGuild / 50);
})()
const streak = await (async () => {
const timestamp = SnowflakeUtil.generate(Date.now() - (1000 * 60 * 60 * 24));
let msgCountInGuild = 0;
for (const [id, chnl] of await message.guild.channels.fetch()) msgCountInGuild += ((await chnl.messages.fetch({after: timestamp})).filter((id, msg) => msg.author.id === user.id).size)
return Math.floor(msgCountInGuild / 50);
})()
errors out at the second fetch btw
NyR
NyR5mo ago
Just so you know, snowflake should be a string there in after so do timestamp.toString() also, please show your error
IcyPickle
IcyPickleOP5mo ago
oh okay one min
NyR
NyR5mo ago
Also no need to fetch channels, it's all cached with Guilds intent
IcyPickle
IcyPickleOP5mo ago
okay ty
NyR
NyR5mo ago
Ok i see, you need to convert Collection to iterables
d.js docs
d.js docs5mo ago
Converting a Collection to an array You only need to convert it to an array if you need the index of an entry: - Iteration (looping) is possible with for...of over Collection#values or forEach - You can transform a Collection to an array with Collection#map - If you need indices, you can get the array using [...collection.values()]
IcyPickle
IcyPickleOP5mo ago
i did convert them into iterables const [id,chnl] of collection right?
NyR
NyR5mo ago
Read the first point You can do something like
for (const channel of message.guild.channels.cache.values()) {
...
}
for (const channel of message.guild.channels.cache.values()) {
...
}
IcyPickle
IcyPickleOP5mo ago
oh wow didn't know that okay lemme see if it works mhm fetch is still undefined
IcyPickle
IcyPickleOP5mo ago
No description
NyR
NyR5mo ago
On a closer look, what you are trying to do seems to be API spammy Why do you need to fetch messages from each channel?
IcyPickle
IcyPickleOP5mo ago
^
IcyPickle
IcyPickleOP5mo ago
No description
NyR
NyR5mo ago
I'm asking why? Just pointing it won't help, just because you want to do something doesn't mean you should
IcyPickle
IcyPickleOP5mo ago
intellisense isn't even working here
No description
IcyPickle
IcyPickleOP5mo ago
no i'm getting the count of all messages in the last 24 hours, dividing it by 50 then creating a msg streak stat
NyR
NyR5mo ago
Show your full code, not all channel types have messages property (like Category channels), you'll have to filter those out
IcyPickle
IcyPickleOP5mo ago
it's a seperate function it doesn't have any linkage to the rest of the code
NyR
NyR5mo ago
You don't have to show everything, just the part/fuction related to the error
IcyPickle
IcyPickleOP5mo ago
const timestamp = SnowflakeUtil.generate(Date.now() - (1000 * 60 * 60 * 24));
let msgCountInGuild = 0;
for (const chnl of await message.guild.channels.cache.values()) msgCountInGuild += ((await chnl.messages.fetch({after: timestamp.toString()}))
.filter((msg) =>
msg.author.id === user.id).size
^^
const timestamp = SnowflakeUtil.generate(Date.now() - (1000 * 60 * 60 * 24));
let msgCountInGuild = 0;
for (const chnl of await message.guild.channels.cache.values()) msgCountInGuild += ((await chnl.messages.fetch({after: timestamp.toString()}))
.filter((msg) =>
msg.author.id === user.id).size
^^
just this nothing from outside is mentioned except user.id which is
IcyPickle
IcyPickleOP5mo ago
No description
NyR
NyR5mo ago
^ 2nd part of this message still applies
IcyPickle
IcyPickleOP5mo ago
oh worked and didn't work no errors but msgCountInGuild is always 0
const streak = await (async () => {
const timestamp = SnowflakeUtil.generate(Date.now() - (1000 * 60 * 60 * 24));
let msgCountInGuild = 0;
for (const chnl of message.guild.channels.cache.filter((chnl) => {chnl.type === ChannelType.GuildText}).values()) msgCountInGuild += ((await chnl.messages.fetch({after: timestamp.toString()}))
.filter((msg) =>
msg.author.id === user.id).size
)
console.log(msgCountInGuild);
return Math.floor(msgCountInGuild / 50);
})()
const streak = await (async () => {
const timestamp = SnowflakeUtil.generate(Date.now() - (1000 * 60 * 60 * 24));
let msgCountInGuild = 0;
for (const chnl of message.guild.channels.cache.filter((chnl) => {chnl.type === ChannelType.GuildText}).values()) msgCountInGuild += ((await chnl.messages.fetch({after: timestamp.toString()}))
.filter((msg) =>
msg.author.id === user.id).size
)
console.log(msgCountInGuild);
return Math.floor(msgCountInGuild / 50);
})()
nvm got it working it just takes a long time to finish is there any better way to do this?
chewie
chewie5mo ago
no
IcyPickle
IcyPickleOP5mo ago
😢
Want results from more Discord servers?
Add your server