【问题标题】:download image from link with discord.js then delete the file从带有 discord.js 的链接下载图像,然后删除该文件
【发布时间】:2019-03-31 22:56:17
【问题描述】:

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

【问题讨论】:

    标签: fs discord.js


    【解决方案1】:

    如果您只想发送不需要保存然后删除文件的图像:您可以将 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
    

    【讨论】:

    • Imgur 似乎不再支持这一点,因为图像只是显示为嵌入标题
    • 奇怪...您使用的是哪个链接?直接链接?
    【解决方案2】:

    您可以使用模块 nightmare 和 nightmare-screenshot-selector 进行尝试。 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');
        });
       });
    

    请注意,复制和粘贴该代码可能不起作用,您应该查看模块的文档并尝试了解您在做什么。

    【讨论】:

      猜你喜欢
      • 2011-06-26
      • 2018-06-18
      • 2012-07-01
      • 2013-06-16
      • 2016-01-09
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多