【问题标题】:How do I edit an image inside an embed discord?如何在嵌入不和谐中编辑图像?
【发布时间】:2020-11-12 22:49:06
【问题描述】:

是否可以更改嵌入中的图像?我正在尝试重新创建我在 reddit 上看到的“etch-a-sketch”机器人,并且想知道它是如何完成的。这是我到目前为止所尝试的: 这是在制作图像的函数内部:

//code that draws the etch-a-sketch
const etchembed = new Discord.MessageEmbed()
    .setAuthor(`${message.author.username}`, `${message.author.displayAvatarURL()}`)
    .setTitle('???? Etch-A-Sketch ????')
    .setColor("#f66868")
    .setFooter(`${client.user.username}`, `${client.user.displayAvatarURL()}`)
    //n is a variable that increases by 1 every time the function is run
    .attachFiles([new Discord.MessageAttachment(canvas.toBuffer(), `etch${n}.png`)])
    .setImage(`attachment://etch${n}.png`)
    .setTimestamp();
return etchembed

在主命令文件中,我在等待函数返回后执行此操作:

message.edit(newetchembed)

所有这些只是将图像移到嵌入之外。我做错了吗?

编辑 1:

我尝试将 message.edit(...) 更改为 message.channel.send(...) 并发送一个带有正确图像的新嵌入。当我尝试使用 message.edit 时,它只是出于某种原因将图像移到嵌入之外。

编辑 2:

我做了更多测试,我开始认为这只是 discord 或 discord.js 出了点问题。这是因为当我记录文件附件和图像时,一切正常:

embed 1: [
  MessageAttachment {
    attachment: < Buffer 89 50 4e 47 0 d 0 a 1 a 0 a 00 00 00 0 d 49 48 44 52 00 00 01 94 00 00 01 2 c 08 06 00 00 00 e4 5 c 45 b8 00 00 00 06 62 4 b 47 44 00 ff 00 ff 00 ff a0 bd a7...1167 more bytes > ,
    name: 'etch_1595840597644.png'
  }
] {
  url: 'attachment://etch_1595840597644.png'
}
embed 2: [
  MessageAttachment {
    attachment: < Buffer 89 50 4e 47 0 d 0 a 1 a 0 a 00 00 00 0 d 49 48 44 52 00 00 01 94 00 00 01 2 c 08 06 00 00 00 e4 5 c 45 b8 00 00 00 06 62 4 b 47 44 00 ff 00 ff 00 ff a0 bd a7...1167 more bytes > ,
    name: 'etch_1595840607390.png'
  }
] {
  url: 'attachment://etch_1595840607390.png'
}

如您所见,消息嵌入具有不同的图像附件,因此我不确定为什么它只是将原始图像移到嵌入之外而不是附加新图像。 This is what it looks like.

另一件事是,当我发送新消息而不是编辑时,它会发送正确的图像。

【问题讨论】:

    标签: canvas discord discord.js


    【解决方案1】:

    无法编辑消息的附件 - 这是 Discord 的限制。

    您必须删除原始消息并发送带有新附件的新消息才能实现此目的。

    【讨论】:

      【解决方案2】:

      无法更改附件。 但是对于图像或缩略图是可能的。只需使用函数message.edit()

      请记住,您必须重新创建传递给 .edit 函数的整个嵌入!

      Link to DiscordJS Documentation

      【讨论】:

        【解决方案3】:

        是的,有可能!

        1. 发送包含新图像的新消息。 (您可以将其发送到专门用于此的特定频道/服务器)。

        2. 获取附加图片的 URL。

        3. 在您要编辑的Embed中,将图片url更改为新图片的url。

        请参阅Fahad 下的this answer my similar question

        除了第 1 步之外,您还可以创建一个网络服务器或找到一个 API,它允许您自己托管新图像。然后您还可以编辑嵌入的图像 URL 以匹配它。

        【讨论】:

          【解决方案4】:

          尝试更改:.attachFiles([new Discord.MessageAttachment(canvas.toBuffer(), `etch${n}.png`)

          作者:.attachFiles(`etch${n}.png`)

          它可以解决您的问题。由于您不想附加新消息,而只是将图像附加到嵌入中。

          【讨论】:

          • 问题是,它实际上是第一次工作(图像出现在嵌入内部并命名为“etch0.png”)。当我尝试将嵌入的图像更改为新图像时,它只是将同一图像移出框外,而不是放入新图像。
          猜你喜欢
          • 2021-01-25
          • 2021-05-14
          • 2021-05-08
          • 2020-11-18
          • 2022-10-23
          • 2020-08-26
          • 2019-02-12
          • 2011-10-15
          • 2021-02-01
          相关资源
          最近更新 更多