从带有discord.js的链接下载图像,然后删除文件

时间:2018-10-27 11:08:40

标签: fs discord.js

我需要从imgur下载图像,然后发送图像,然后使用fs或您可能建议的任何其他模块删除文件。感谢您的帮助!

2 个答案:

答案 0 :(得分:0)

您可以使用噩梦和噩梦截图选择器模块来尝试。 Nightmare是用于抓取网络的模块,而Nightmare-screenshot-selector使其可以更轻松地截取不同内容。

您应该执行的步骤:

nightmare
  .goto(url) //imgur.com/xyz
  .wait(#pic) // wait for the picture to load
  .screenshotSelector(#image) //screenshot the image
  .then(function(data) {
    fs.writeFileSync(path, data);  //save the file
    message.channel.send("", {    //send the picture in the channel
      file: path
    });
    fs.unlink(path, (err) => {  //delete the file
     if (err) throw err;
      console.log(path + ' was deleted');
    });
   });

请注意,复制和粘贴该代码可能无法正常工作,您应该查看模块的文档并尝试了解自己的工作。

答案 1 :(得分:0)

如果您只想发送图像,则无需保存然后删除文件:您可以将URL放在消息的文件选项中,因为它接受BufferResolvable(因此URL )。

let image = "https://discordapp.com/assets/c4bae281354a2b4e2db85415955e0994.svg"; // this is the URL for your image
let channel = message.channel; // this is your channel (I assumed this is in a command)

channel.send("", { file: image }); // you can send the image like this
channel.send("Description message", { file: image }); // you can also set a description