Discord.js V13发送消息附件



升级到discord.js v13并使用Array.from(message.attachments.values())代替message.attachments.array()从消息发送附件后,

message.client.channels.cache.get("123456789").send({
files: [Array.from(message.attachments.values())],
content: `test`
});

我从节点模块的控制台得到一个错误:

DesktopBotnode_modulesdiscord.jssrcstructuresMessagePayload.js:223
if (thing.path) {
^
TypeError: Cannot read property 'path' of undefined

出现错误的部分在这里:

const findName = thing => {
if (typeof thing === 'string') {
return Util.basename(thing);
}
if (thing.path) {
return Util.basename(thing.path);
}
return 'file.jpg';
};

我真的很困惑什么是错的或如何解决它,有人帮助吗?

你正在创建一个数组内部的数组,删除额外的方括号。Array.from()返回一个新的Array实例。

message.client.channels.cache.get("channel id").send({
files: Array.from(message.attachments.values()),
content: `test`
});

也可以使用展开操作符将可迭代对象展开到数组中。

message.client.channels.cache.get("channel id").send({
files: [...message.attachments.values()],
content: `test`
});

最新更新