提问者:小点点

只有在有附件的情况下才将图像添加到嵌入式中?


我正在创建一个引用命令。 该命令接收消息ID,并发送包含消息作者,内容,时间等的嵌入式。有一件事它不包括消息中的任何附件。

我在嵌入中添加了一个“image:”字段,如果引用的消息有附件,这个字段就可以正常工作。 如果消息没有任何附件,则嵌入器不发送。

我相信这是因为它正在搜索一个附件以图像的形式添加到嵌入式中,但是它找不到,所以它出错了。

以下是我所得到的:

url: messagea.attachments.first().url

下面是我尝试过的一个解决方案(这也有完全相同的问题):

url: messagea.attachments === null ? null : messagea.attachments.first().url

如果有附件,我怎样才能使嵌入只包括一个图像?


共1个答案

匿名用户

确定附件是否为图像的一个技巧是使用MessageAttachment.Height属性,因为只有当附件为图像或视频时,该属性才存在。

let attachment = message.attachments.first() // Get the first attachment

// If the attachment exists and has an height...
if (attachment && typeof attachment.height == 'number') {
  let { url } = attachment // ...get the URL
  // You can now send your embed
} // else: do nothing