无法将消息发送到特定频道

时间:2020-10-28 21:40:22

标签: node.js discord discord.js

我有一个简单的直流机器人,代码像这样。我试图简化它,使其更具可读性

    const client = new Discord.Client();
    ...
    client.on('ready', () => {
    client.channels.cache.get('315445287374028800').send("works here");
});
    setInterval(( () =>{
    try{
        removeHTML(downloadHTML);

    } catch(err){
        console.log(err);
    }
}),30000);
    ...
    ...
    let removeHTML = function(callback){
    client.channels.cache.get('315445287374028800').send("WORKS HERE");
    readHTML();
    ...
}
let readHTML = function(){
    console.log("dolar is read");
    //client.channels.cache.get('315445287374028800').send("DOESNT WORK HERE");

    fs.readFile(dir + '/index.html' , 'utf-8', function(err,html){
        if(err)
            console.log(err);
        else{
            //client.channels.cache.get('315445287374028800').send("DOESNT WORK HERE");
            if(isPeak){
                //client.channels.cache.get('315445287374028800').send("DOESNT WORK HERE");
            }
        }
    });
}

    

似乎send函数在回调函数中不起作用,但是我该如何解决?

错误类型:所有行(“在此处进行操作”)的错误相同:


TypeError: Cannot read property 'send' of undefined
   at readHTML (C:\Users\user\Desktop\Discord Bot\index.js:142:18)
   at Object.<anonymous> (C:\Users\user\Desktop\Discord Bot\index.js:182:1)
   at Module._compile (internal/modules/cjs/loader.js:1015:30)
   at Object.Module._extensions..js (internal/modules/cjs/loader.js:1035:10)
   at Module.load (internal/modules/cjs/loader.js:879:32)
   at Function.Module._load (internal/modules/cjs/loader.js:724:14)
   at Function.executeUserEntryPoint [as runMain] 

1 个答案:

答案 0 :(得分:0)

我已将所有功能改为异步等待,并且有效