Discord.js V13 发送消息附件。

3
升级到 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`
});

我从node模块的控制台中得到一个错误:

Desktop\Bot\node_modules\discord.js\src\structures\MessagePayload.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';
    };

我真的很困惑,不知道出了什么问题或者如何解决,有人能帮帮我吗?

1个回答

1
您正在数组内部创建另一个数组,需要删除多余的方括号。 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`
});

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接