Fetch an array of message IDs at once

Is there some sort of way to fetch multiple messages by id in a single request? Kind of like message.channel.messages.fetch([snowflake_one, snowflake_two]) I tried interaction.channel.messages.fetch([1124766378705027212,1124766519826587670]).then(_ => console.log(_.size)) And it reports 50, which is a lot more than two lol I basically create an array full of message ids, and I want to fetch them to get their content, author id, and displayname But just running messages.fetch(id) for each one is very slow This is what I've got right now, which is works, but is undesirable as it's very slow (many times slower requesting 3 specific IDs vs limit of 3)
// Fetch references from pins
let references = await Promise.all(pins.map(p => p.reference && message.channel.messages.fetch(p.reference.messageId)))
// Insert references before the relevant pin (working backwards to prevent indexes moving)
for (let i = pins.length - 1; i >= 0; i--)
if(references[i]) pins.splice(i, 0, references[i])
// Fetch references from pins
let references = await Promise.all(pins.map(p => p.reference && message.channel.messages.fetch(p.reference.messageId)))
// Insert references before the relevant pin (working backwards to prevent indexes moving)
for (let i = pins.length - 1; i >= 0; i--)
if(references[i]) pins.splice(i, 0, references[i])
It just turns an array of pins, into an array of pins and whatever they're replying to I've read MessageManager.fetch(), but I don't really see what I'm looking for Despite it saying that it accepts an array of options, and individual options can be snowflakes
discord.js
discord.js
discord.js is a powerful Node.js module that allows you to interact with the Discord API very easily. It takes a much more object-oriented approach than most other JS Discord libraries, making your bot's code significantly tidier and easier to comprehend.
8 Replies
d.js toolkit
d.js toolkit2y 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.
Shane
ShaneOP2y ago
# npm list; node -v
beta@ /home/shane/Documents/Discord Bot/beta

v20.3.1
# npm list; node -v
beta@ /home/shane/Documents/Discord Bot/beta

v20.3.1
treble/luna
treble/luna2y ago
i dont think .fetch can take an array of id's, hence why it returns 50. Only way to do it without spamming the api would be checking cache or making a backend yourself but i might be wrong
Syjalo
Syjalo2y ago
First of all. You have an old version of the website cached. Shift + F5 or Ctrl + Shift + R
d.js docs
d.js docs2y ago
method MessageManager#fetch() Fetches message(s) from a channel. (more...)
Shane
ShaneOP2y ago
Oh I see That does happen to have identical content to the cached page I've got, though I've been redirected now to the old subdomain Still says that it accepts an array of MessageResolvable For some reason I assumed that cache was always checked first, and then it falls back to making an API request I'll give this one a try
treble/luna
treble/luna2y ago
cache is always checked first, if not a request is made but thats gonna get you reatelimited if none of the messages are cached and you have to fetch a lot
Shane
ShaneOP2y ago
I swapped it out with this
{
// Fetch references from pins
let references = await Promise.all(pins.map(p => p.reference && (
message.channel.messages.cache.get(p.reference.messageId) ||
message.channel.messages.fetch(p.reference.messageId)
)))
// Insert references before the relevant pin (working backwards to prevent indexes moving)
for (let i = pins.length - 1; i >= 0; i--)
if(references[i]) pins.splice(i, 0, references[i])
}
{
// Fetch references from pins
let references = await Promise.all(pins.map(p => p.reference && (
message.channel.messages.cache.get(p.reference.messageId) ||
message.channel.messages.fetch(p.reference.messageId)
)))
// Insert references before the relevant pin (working backwards to prevent indexes moving)
for (let i = pins.length - 1; i >= 0; i--)
if(references[i]) pins.splice(i, 0, references[i])
}
And I seem to get what I'm looking for, so thank you I thought this was redundancy, but it does seem to cut the eval time in half (with only 3 pins in total, one of which doesn't have a reference to begin with) 1.178s → 0.457s
Want results from more Discord servers?
Add your server