【问题标题】:Only add an image to an embed if there is an attachment?如果有附件,仅将图像添加到嵌入?
【发布时间】:2020-10-03 23:19:42
【问题描述】:

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

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

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

这就是我所拥有的:

url: messagea.attachments.first().url

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

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

如果有附件,我怎样才能使嵌入仅包含图像?

【问题讨论】:

    标签: javascript node.js discord.js


    【解决方案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
    

    【讨论】:

    • 我需要将此添加到 .then() 方法中。如何在 .then() 方法中设置变量?现在我有:.then(messagea => message.channel.send({embed: {,然后是我的嵌入。
    • 同理,你可以照常使用varletconst,即使你在then回调中
    猜你喜欢
    • 2019-06-01
    • 2021-08-13
    • 2022-08-03
    • 2014-08-15
    • 2021-11-20
    • 2012-12-03
    • 2017-11-30
    • 2011-09-07
    • 1970-01-01
    相关资源
    最近更新 更多