discord.js是否保存附件“未定义"? [英] discord.js saving an attachment "undefined"?

查看:29
本文介绍了discord.js是否保存附件“未定义"?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我最近遇到了一个问题,用户拖曳然后删除图像,然后才能看到它们是什么.因此,我正在创建一个日志,以将所有内容下载到日志中.(是的,我已经实例化了fs.js).但是由于某种原因,在写文件时...文件只有9个字节大(内容只是未定义").请帮忙.

I've had a problem recently with users trolling and then deleting images before I can see what they are. So I'm creating a log to download everything into a log. (yes I've instantiated fs.js already). For some reason though, when writing the file... the file is only 9 bytes big (and the content is just "undefined"). Please help.

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';
})

推荐答案

我最终用一个很小的函数解决了它.谢谢大家(尤其是那个问变量是什么的人……那很有帮助)

I ended up solving it with a tiny function. Thanks everyone (especially the guy asking what a variable was... that was super helpful)

function downloadAttachment(url, dest, hash){
  console.log('initiating download of '+ url +'...');

  request(url).pipe(fs.createWriteStream(dest));

}

"hash"变量目前未使用.我饿了,渴望咸牛肉哈希...

the "hash" variable is not used right now. I was hungry and craving corned beef hash...

这篇关于discord.js是否保存附件“未定义"?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆