Skip to content

How to find url of attachment of a message collected by the await messages function in discord.js? #5601

Closed Locked Answered by kyranet
Rijak2005 asked this question in Q&A
Discussion options

You must be logged in to vote

That's because Collection#toJSON() calls toJSON() to all of its values, which may not have the same information as not everything can be serialized, that includes MessageAttachment#toJSON().

You should use message.attachments.first()?.url.

Or alternatively, in older syntax (if you don't use Node.js v14):

const attachment = message.attachments.first();
const url = attachment ? attachment.url : null;

There's also the possibility to do:

const [attachments] = message.attachments.values();
const url = attachment ? attachment.url : null;

Replies: 1 comment

Comment options

You must be logged in to vote
0 replies
Answer selected by Rijak2005
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
2 participants