discord.js 正在保存附件 "undefined"?
discord.js saving an attachment "undefined"?
我最近遇到了一个问题,用户在我看到它们是什么之前拖拽然后删除图像。所以我正在创建一个日志以将所有内容下载到日志中。 (是的,我已经实例化了 fs.js)。但出于某种原因,在写入文件时...文件只有 9 个字节大(内容只有 "undefined")。请帮忙。
var attachment = (message.attachments).array();
attachment.forEach(function(attachment) {
console.log(attachment.url);
tempName = attachment.url.split("/");
attachName = tempName[tempName.length-1]
console.log(attachName);
fs.writeFileSync(dir + "/" + attachName, attachment.file, (err) => {
// throws an error, you could also catch it here
if (err) throw err;
// success case, the file was saved
console.log('attachment saved!');
});
theLog += '<img src="'+ "attachments/" + message.channel.name + "/" + attachName + '"> \n';
//theLog += '<img src="'+ attachment.url + '"> \n';
})
让我们从回答为什么将其保存为 undefined 开始。
如果您检查 MessageAttachment 的文档,message.attachments.first().file
是未定义的。有 fileName
和 fileSize
但没有 file
要保存文件,您可以做两件事...
正在保存 URL。
您可以将 url 保存在 JSON 文件的数组中,如下所示:
JSON 文件
{
"images":[]
}
JS 文件
let imgs = require(JSON_FILE)
imgs.images.push(attachment.url);
fs.writeFile(JSON_FILE,JSON.stringify(imgs,null,4));
- 保存图像本身
您可以使用 request 模块从 url
中提取图像
JS 文件
//Start of code
let request = require(`request`);
let fs = require(`fs`);
//Later
request.get(attachment.url)
.on('error', console.error)
.pipe(fs.createWriteStream(`Img-${Date.now()}`));//The "Img-${Date.now}" Guarantees Unique file names.
编辑: request
已弃用。它已被 fetch
取代 我无法确认此代码是否适用于 fetch
但下划线原则是相同的。
我最终用一个小函数解决了它。谢谢大家(尤其是那个问变量是什么的人……非常有帮助)
function downloadAttachment(url, dest, hash){
console.log('initiating download of '+ url +'...');
request(url).pipe(fs.createWriteStream(dest));
}
"hash" 变量现在没有使用。我饿了,想吃咸牛肉哈希...
我最近遇到了一个问题,用户在我看到它们是什么之前拖拽然后删除图像。所以我正在创建一个日志以将所有内容下载到日志中。 (是的,我已经实例化了 fs.js)。但出于某种原因,在写入文件时...文件只有 9 个字节大(内容只有 "undefined")。请帮忙。
var attachment = (message.attachments).array();
attachment.forEach(function(attachment) {
console.log(attachment.url);
tempName = attachment.url.split("/");
attachName = tempName[tempName.length-1]
console.log(attachName);
fs.writeFileSync(dir + "/" + attachName, attachment.file, (err) => {
// throws an error, you could also catch it here
if (err) throw err;
// success case, the file was saved
console.log('attachment saved!');
});
theLog += '<img src="'+ "attachments/" + message.channel.name + "/" + attachName + '"> \n';
//theLog += '<img src="'+ attachment.url + '"> \n';
})
让我们从回答为什么将其保存为 undefined 开始。
如果您检查 MessageAttachment 的文档,message.attachments.first().file
是未定义的。有 fileName
和 fileSize
但没有 file
要保存文件,您可以做两件事...
正在保存 URL。
您可以将 url 保存在 JSON 文件的数组中,如下所示:
JSON 文件
{
"images":[]
}
JS 文件
let imgs = require(JSON_FILE)
imgs.images.push(attachment.url);
fs.writeFile(JSON_FILE,JSON.stringify(imgs,null,4));
- 保存图像本身
您可以使用 request 模块从 url
中提取图像JS 文件
//Start of code
let request = require(`request`);
let fs = require(`fs`);
//Later
request.get(attachment.url)
.on('error', console.error)
.pipe(fs.createWriteStream(`Img-${Date.now()}`));//The "Img-${Date.now}" Guarantees Unique file names.
编辑: request
已弃用。它已被 fetch
取代 我无法确认此代码是否适用于 fetch
但下划线原则是相同的。
我最终用一个小函数解决了它。谢谢大家(尤其是那个问变量是什么的人……非常有帮助)
function downloadAttachment(url, dest, hash){
console.log('initiating download of '+ url +'...');
request(url).pipe(fs.createWriteStream(dest));
}
"hash" 变量现在没有使用。我饿了,想吃咸牛肉哈希...