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

self explanatory
33 Replies
d.js toolkit
d.js toolkit3d 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 docs3d 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
IcyPickle3d 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 🌈
chewie 🌈3d ago
loop
d.js docs
d.js docs3d ago
:propertysignature: FetchMessagesOptions#after @14.15.3 Consider only messages after this id
IcyPickle
IcyPickle3d ago
🙏
NyR
NyR3d 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
IcyPickle3d 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
NyR3d ago
Just so you know, snowflake should be a string there in after so do timestamp.toString() also, please show your error
IcyPickle
IcyPickle3d ago
oh okay one min
NyR
NyR3d ago
Also no need to fetch channels, it's all cached with Guilds intent
IcyPickle
IcyPickle3d ago
okay ty
NyR
NyR3d ago
Ok i see, you need to convert Collection to iterables
d.js docs
d.js docs3d 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
IcyPickle3d ago
i did convert them into iterables const [id,chnl] of collection right?
NyR
NyR3d 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
IcyPickle3d ago
oh wow didn't know that okay lemme see if it works mhm fetch is still undefined
IcyPickle
IcyPickle3d ago
No description
NyR
NyR3d 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
IcyPickle3d ago
^
IcyPickle
IcyPickle3d ago
No description
NyR
NyR3d ago
I'm asking why? Just pointing it won't help, just because you want to do something doesn't mean you should
IcyPickle
IcyPickle3d ago
intellisense isn't even working here
No description
IcyPickle
IcyPickle3d 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
NyR3d ago
Show your full code, not all channel types have messages property (like Category channels), you'll have to filter those out
IcyPickle
IcyPickle3d ago
it's a seperate function it doesn't have any linkage to the rest of the code
NyR
NyR3d ago
You don't have to show everything, just the part/fuction related to the error
IcyPickle
IcyPickle3d 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
IcyPickle3d ago
No description
NyR
NyR3d ago
^ 2nd part of this message still applies
IcyPickle
IcyPickle3d 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 🌈
chewie 🌈3d ago
no
IcyPickle
IcyPickle3d ago
😢