javascript - 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);
它将成功发送图像。
所以我想知道为什么它不会附加到嵌入,但它可以发送到频道。
解决方案
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 开发者文档。
推荐阅读
- html - 如何使用 django 从 HTML 表中获取列值
- python - Python 错误:“DataFrame”对象没有属性“DataReader”
- flutter - 如何使用 Flutter 使文本居中并防止文本溢出
- python - 使用乘法条件更改列中的值
- flutter - Flutter 将字符串变量转换为整数
- database - EntityFramework / 从数据库中以最低价格获取两个对象
- javascript - node.js 函数调用顺序,Spotify Web API
- javascript - 如何在数组中的图像之间添加过渡
- arrays - 在 C 中扫描和打印字符串的问题
- node.js - 关于涉及外部 API 的个人项目的建议