#Fetch an array of message IDs at once

13 messages · Page 1 of 1 (latest)

sleek marten
#

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])

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 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.

unique thornBOT
#

• 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.

sleek marten
#
# npm list; node -v
beta@ /home/shane/Documents/Discord Bot/beta
└── [email protected]

v20.3.1
tidal bone
#

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

ivory patrol
#

First of all. You have an old version of the website cached.
Shift + F5 or Ctrl + Shift + R

ocean gladeBOT
sleek marten
#

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

sleek marten
tidal bone
#

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

sleek marten
#

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])
}

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)