简体   繁体   English

Discord.JS 获取消息中的第一个附件

[英]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.我只想使用我的 Discord.JS bot 来获取消息中的第一个附件( MessageAttatchment对象),简单地说。 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.我知道这是 2 岁,但我想我可能发现了问题。 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 .discord.js 中有一个Attachment类。 So, it will create a conflict between variables definitions.因此,它会在变量定义之间产生冲突。 You should rename it to something else!您应该将其重命名为其他名称!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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