从提交的图像中获取 URL

Getting the URL from a submitted image

代码 node.js 和 discord.js:

client.on('message', function(message){
  //  if (message.author.client) return;
    var Attachment = (message.attachments).array();
    console.log(Attachment); //outputs array
    console.log(Attachment.url); //undefined
    console.log(Attachment.MessageAttachment); //undefined
    console.log(Attachment.MessageAttachment['url']); //error
});

output of "console.log(Attachment);"

如何从

获取字符串
[MessageAttachment
  {...,
   url: '..png',
   ...}
]

从你的控制台截图来看,Attachment 是一个数组,而不是一个对象。因此,您需要访问该数组中的第一个元素,然后是 url 属性。像这样:

Attachment[0].url

此外,如果可能有多个附件,您可以使用 forforEach 循环遍历它们。像这样:

Attachment.forEach(function(attachment) {
  console.log(attachment.url);
})