使用 discord.js 从 link 下载图像,然后删除文件
download image from link with discord.js then delete the file
我需要从 imgur 下载图像然后发送,然后使用 fs 或您可能推荐的任何其他模块删除文件。感谢您的帮助!
您可以尝试使用模块 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');
});
});
请注意,复制和粘贴该代码可能不起作用。您应该查看模块的文档并尝试理解您在做什么。
如果您只想发送图像,则不需要保存然后删除文件:您只需将 URL 放在消息的文件选项中,因为它接受 BufferResolvable
(所以 URLs)也是。
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 下载图像然后发送,然后使用 fs 或您可能推荐的任何其他模块删除文件。感谢您的帮助!
您可以尝试使用模块 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');
});
});
请注意,复制和粘贴该代码可能不起作用。您应该查看模块的文档并尝试理解您在做什么。
如果您只想发送图像,则不需要保存然后删除文件:您只需将 URL 放在消息的文件选项中,因为它接受 BufferResolvable
(所以 URLs)也是。
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