简体   繁体   中英

Discord.JS Get First Attachment in Message

I just want to use my Discord.JS bot to get the first attachment in a message ( MessageAttatchment object), simply put. I have already tried this, but it just seems to crash immediately.

  var Attachment = (message.attachments)
  if (Attatchment){
    console.log( Attatchment.array()[0] )
    console.log( Attatchment.array()[0].url )
  }

The crash might not be due to this, it could be due to relevant code surrounding the area, but this is not a problem, since if I find a solution, that relevant code will become irrelevant.

I know this is 2 years old, but I think I might of found the problem. You seem to have mistyped the variable attachment, instead of attachment, you types in attatchment.

This is how the code should look like. Although, not sure if there may be more errors, this code snippet just fixes the typos.

var Attachment = (message.attachments)
if (Attachment){
    console.log( Attachment.array()[0] )
    console.log( Attachment.array()[0].url )
}

There is a comment on it that does point it out, but they haven't considered it as an answer.

Tip: Always try to check for typos in your code, that is the most common problem. Use the Find tool in a text editor to help find typos.

There's a Attachment class in discord.js . So, it will create a conflict between variables definitions. You should rename it to something else!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM