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.embedsproperty).
And here is the main problem:
When you are trying to access in
embedsproperty 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.
Any suggestions that can solve this problem?