首页 > 解决方案 > Discord.JS 机器人很慢,不知道为什么

问题描述

抱歉标题不好,我想不出一个好标题。

我正在运行一个自我机器人(我知道,针对 TOS,但我正在试验),它需要在发送时下载任何新文件(特别是图像 [gifs too] 和视频)。它确实有效,但速度很慢,而且下载的文件是几分钟前的。

这是我当前的代码:

const client = new Discord.Client();
let request = require(`request`);
let fs = require(`fs`);
function download(url, fname){
    request.get(url)
        .on('error', console.error)
        .pipe(fs.createWriteStream("./cmc-memes/"+fname));
}
client.on('ready', () => {
  console.log(`Logged in as ${client.user.tag}!`);
});
// lags idk why
client.on('message', msg => {
    if (msg.channel.id === "296056831514509312") {
        console.log(msg.content)
        if(msg.attachments.first()){//checks if an attachment is sent
            download(msg.attachments.first().url, msg.attachments.first().filename);//Function I will show later
        }
    }
});

我该怎么做才能解决这个问题?

任何帮助表示赞赏,谢谢!

标签: javascriptnode.jsdiscorddiscord.js

解决方案


我不知道 request.get(url) 如何工作的细节(我更喜欢 node-fetch)但是如果它是一个同步调用,它会显着减慢它。否则问题只是您的互联网连接。


推荐阅读