【问题标题】:Discord.js v13 sending buffer as attachment results in invisible imageDiscord.js v13 将缓冲区作为附件发送会导致图像不可见
【发布时间】:2021-07-15 04:27:32
【问题描述】:

我正在尝试在 Discord.js v13 中将缓冲区作为附件发送。 我正在使用与 v11 相同的代码,但对 v13 进行了必要的语法更改。

指定路径的图像确实存在,如果我将缓冲区作为图像保存到磁盘,它就可以正常工作。
如果我在 Discord 中将其作为附件发送,则会导致如下所示的空附件:

这是我的代码:

const promise = fs.promises.readFile(path.join('./assets/Environment/Base.png')); //this image exists

Promise.resolve(promise).then(function(buffer){
    message.channel.send({content: `${message.author.username}'s base:\n⁣`, attachment: [buffer]}).catch(allerrors)
});

我在几个 Discord 服务器上问过,但没有人能帮助我,所以我想我会在这里问,有谁知道可能导致这种情况的原因吗?
谢谢!

【问题讨论】:

    标签: discord.js buffer


    【解决方案1】:

    根据docs,你应该像这样发送一个缓冲区:

    message.channel.send({content: `${message.author.username}'s base:\n⁣`, files: [
        { attachment: buffer }
    ]}).catch(allerrors);
    

    在 discord.js v13 和 discord.js v12 上都经过测试。

    【讨论】:

    • 这只会导致此错误:TypeError [REQ_RESOURCE_TYPE]: The resource must be a string, Buffer or a valid file stream. 我认为它无法识别该语法
    • 嗯,它对我有用。我认为你的缓冲区有问题。不知何故错误地读取了文件或类似的东西。缓冲区可能是空的、未定义的等。
    • 在发送消息之前尝试记录buffer.length
    • 你说得对,它返回 undefined,谢谢!我看看我能不能找出问题所在^^
    猜你喜欢
    • 2021-10-17
    • 2018-09-20
    • 1970-01-01
    • 1970-01-01
    • 2014-07-13
    • 2018-04-11
    • 2019-05-07
    • 2019-12-22
    • 1970-01-01
    相关资源
    最近更新 更多