Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

596
Views
Discord.js v13 - Extract Image Embed attached in Message

I am writing a function that returns an Image Embed from a Message. I mean, Image from MessageEmbed.

The thing: Discord resolves the Image URL and returns an Image Embed object instead (can be found using message.embeds property).

Here's an example image

And here is the main problem:

When you are trying to access in embeds property from Message Object, will get an empty Array like this: []. But accessing again will get the expected result. [ [MessageEmbed] ] An ImageEmbed.

The problem with this is Discord takes some time to resolve the URL and displays into Message as Attachment, and dont want to use the setTimeout function to wait it. Because if the message contains multiple image URLs this will take more time. Well... trying to access in embeds property after that operation will not work properly because returns again... an empty array.

So looking the messageUpdate event i found that emits the same Message with the Image URL resolved in embeds property. So i make this code:

async function getImageEmbeds(message) {
   // Access into Client to listen the messageUpdate event.
   const { client } = message;
   const collectedImages = new Array();

   // Waits the event emittion
   await new Promise((resolve) => {
      function fnListener(oldmsg, newmsg) {
         if (message.id !== newmsg.id) return;
         const imageEmbeds = newmsg.embeds.filter(embed => embed.thumbnail !== null).map(embed => embed.thumbnail);
         collectedImages.push(...imageEmbeds);
       
         client.removeListener("messageUpdate", fnListener);
         // Ends this promise.
         resolve(true);
      }
      client.on("messageUpdate", fnListener);
   })

   return collectedImages;
}

This code works as expected (Waits the Image URL resolving that will be emitted in messageUpdate event and returns an Array with the Image Embed resolved). But there's a new problem, The function will wait indefinitely if the message doesn't have an Image URL.

  • How i can tell to the function if the message has an unresolved Image URL?

Any suggestions that can solve this problem?

about 4 years ago · Juan Pablo Isaza
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!