首页 > 解决方案 > Discord.MessageAttachment() 未附加到嵌入

问题描述

所以我试图将图像附加到不和谐的嵌入中(最好是缩略图)。图像本地存储在我的硬盘上。我目前正在这样做:

attachment = await new discord.MessageAttachment('serverFavicon.png', 'favicon.png');
embed.setThumbnail(attachment);

这将返回此错误并且不发送嵌入:

(node:60598) UnhandledPromiseRejectionWarning: DiscordAPIError: Invalid Form Body
embed.thumbnail.url: Could not interpret "{'attachment': 'serverFavicon.png', 'name': 'favicon.png'}" as string.
    at RequestHandler.execute (/Users/manders/Desktop/Bots/Minecraft Server Discord Bot/node_modules/discord.js/src/rest/RequestHandler.js:154:13)
    at processTicksAndRejections (internal/process/task_queues.js:93:5)
    at async RequestHandler.push (/Users/manders/Desktop/Bots/Minecraft Server Discord Bot/node_modules/discord.js/src/rest/RequestHandler.js:39:14)
(Use `node --trace-warnings ...` to show where the warning was created)
(node:60598) UnhandledPromiseRejectionWarning: Unhandled promise rejection. This error originated either by throwing inside of an async function without a catch block, or by rejecting a promise which was not handled with .catch(). To terminate the node process on unhandled promise rejection, use the CLI flag `--unhandled-rejections=strict` (see https://nodejs.org/api/cli.html#cli_unhandled_rejections_mode). (rejection id: 1)
(node:60598) [DEP0018] DeprecationWarning: Unhandled promise rejections are deprecated. In the future, promise rejections that are not handled will terminate the Node.js process with a non-zero exit code.

奇怪的是,如果我这样做

message.channel.send('Image', attachment);

它将成功发送图像。

所以我想知道为什么它不会附加到嵌入,但它可以发送到频道。

标签: javascriptdiscorddiscord.js

解决方案


setThumbnail只接受一个字符串(图片的 URL),而不是一个MessageAttachment.

在您的示例中,您可以简单地使用图像的 URL:

embed.setThumbnail('serverFavicon.png');

如果要重命名图像,可以MessageAttachment在发送消息时附加并attachment://image-name.png用于缩略图 URL:

// You also don't need to use await here; constructors can't be async
const attachment = new discord.MessageAttachment('serverFavicon.png', 'favicon.png');
embed.setThumbnail('attachment://favicon.png');

// ...
// Discord.js v12:
message.channel.send({embed, files: [attachment]});
// Discord.js v13:
message.channel.send({embeds: [embed], files: [attachment]});

有关在嵌入中使用附件的更多信息,请参阅Discord 开发者文档


推荐阅读