将文件下载到本地计算机,发送到消息不和谐 [英] download file to local computer sent attatched to message discord

查看:71
本文介绍了将文件下载到本地计算机,发送到消息不和谐的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在查看Discord.js文档之后,我可以找到问题的答案,有人知道该怎么做吗?该页面已经存在问题,但没有答案或评论.

after looking on the Discord.js docs i can find an answer for the question, does someone know how to do it? theres already a question the page but has no answers or comments.

想象聊天中的某人发送了一张图片,机器人可以通过这种方式下载图片或获取图片的网址吗?

imagine that someone on the chat sentí an image, is there a way of the bot downloading the image or get the url of the image?

谢谢!

推荐答案

对于初学者...您需要代码才能访问附件.

For starters... You'd need the code to access the attachment.

client.on(`message`,function(msg){
    if(msg.attachments.first()){//checks if an attachment is sent
        if(msg.attachments.first().filename === `png`){//Download only png (customize this)
            download(msg.attachments.first().url);//Function I will show later
        }
    }
});

注意:我仅将附件限制为 png ,因此我们下载了经过验证的图像.否则,我们可能会下载一些错误的脚本,甚至可能下载病毒.下载内容时要小心.

Note: I limited attachments to png only so we download verified images. Otherwise we might download some bad scripts and possibly viruses. Be careful when downloading stuff.

现在,我刚刚给您的代码调用了 download 并传入了 url .
现在,您需要请求模块 AND fs 模块.

Now the code I just gave you calls download and passes in the url.
Now you will need the request module AND the fs module.

为什么?很高兴您询问... request 模块访问该URL并将其从Web提取数据.
fs 模块在本地/外部计算机上创建/读取/写入文件...

Why? Glad you asked... The request module accesses the url and pulls it the data from the web.
The fs module create/reads/writes files on your local/external machine...

使用这两个模块,我们将,然后保存.

Using the two modules, we will pull it and then save it.

现在让我们假设 url 是这个 meme.png (不匹配的png附件)

Now lets assume url is this meme.png (discord png attachment)

let request = require(`request`);
let fs = require(`fs`);
function download(url){
    request.get(url)
        .on('error', console.error)
        .pipe(fs.createWriteStream('meme.png'));
}

Voila!我们现在有一个关于Doritos XD的 meme.png 图像

and Voila! We now have a meme.png image about Doritos XD

这篇关于将文件下载到本地计算机,发送到消息不和谐的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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