如何编辑嵌入不和谐内的图像?

时间:2020-07-23 08:48:36

标签: canvas discord discord.js

是否可以更改嵌入内容中的图像?我正在尝试重新创建我在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.

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

4 个答案:

答案 0 :(得分:2)

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

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

答案 1 :(得分:1)

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

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

Link to DiscordJS Documentation

答案 2 :(得分:0)

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

通过:.attachFiles(`etch${n}.png`)

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

答案 3 :(得分:0)

是的,这是可能的!

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

  2. 获取附加图片的网址。

  3. 在您要编辑的嵌入中,将图片网址更改为新图片的网址。

this answer下查看Fahadmy similar question

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

相关问题