問題描述
查看 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.
想象有人在聊天中發(fā)送了一張圖片,機器人有沒有辦法下載圖片或獲取圖片的網(wǎng)址?
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
,以便我們下載經(jīng)過驗證的圖像.否則我們可能會下載一些不良腳本和病毒.下載東西時要小心.
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.
現(xiàn)在我剛剛給你的代碼調用 download
并傳入 url
.
現(xiàn)在你需要 request 模塊 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 并從網(wǎng)絡中提取數(shù)據(jù).fs
模塊在本地/外部機器上創(chuàng)建/讀取/寫入文件...
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.
現(xiàn)在讓我們假設 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'));
}
和瞧!我們現(xiàn)在有一個關于Doritos XD的meme.png
圖片
and Voila! We now have a meme.png
image about Doritos XD
這篇關于下載文件到本地計算機發(fā)送附加到消息不和諧的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網(wǎng)!