Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/467.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
JavaScript Discord.js,无法从函数内部发送消息_Javascript_Discord.js - Fatal编程技术网

JavaScript Discord.js,无法从函数内部发送消息

JavaScript Discord.js,无法从函数内部发送消息,javascript,discord.js,Javascript,Discord.js,我正试图让discord机器人在if中向我发送消息,但我无法让它工作。 我知道if触发器,因为我从console.log('inside if');)获得了激活;。我知道这一点 函数外部的directMessage可以工作,因为我可以让bot发送“Hello from main”。 还尝试将directMessage中的代码添加到if中,但没有成功。我一直在寻找答案,但没有找到解决办法 function symPriceChecker(){ sellStock.forEach(entry

我正试图让discord机器人在if中向我发送消息,但我无法让它工作。 我知道if触发器,因为我从console.log('inside if');)获得了激活;。我知道这一点 函数外部的directMessage可以工作,因为我可以让bot发送“Hello from main”。 还尝试将directMessage中的代码添加到if中,但没有成功。我一直在寻找答案,但没有找到解决办法

function symPriceChecker(){
    sellStock.forEach(entry => {
      if (parseInt(entry.Price) < symPrice){            
            directMessage(client, 'Hello from if',ID); // But here it does not work
            console.log('inside if');
            sellStock.pop(entry);
            
        }
    });
    repeater2 = setTimeout(symPriceChecker, 5000);   
};

directMessage(client, 'Hello from main',ID); //This one works

// how directMessage looks
module.exports=(client, message, dicsordUser)=>
{
    client.on('ready',()=>{
        client.users.fetch(dicsordUser).then((user) =>{ 
        user.send(message)})})
};

函数symPriceChecker(){ sellStock.forEach(条目=>{ if(parseInt(entry.Price) { client.on('ready',()=>{ client.users.fetch(dicsordUser).then((user)=>{ user.send(message)}) }; 试试这个

const directMessage = async(client, message, dicsordUser) => {
const user = await client.users.fetch(discordUser).catch(() => {});
if (!user) return console.log('no user found');
user.send(message);
}

function symPriceChecker(){
    sellStock.forEach(entry => {
      if (parseInt(entry.Price) < symPrice){            
            directMessage(client, 'Hello from if',ID); // But here it does not work
            console.log('inside if');
            sellStock.pop(entry);
            
        }
    });
    repeater2 = setTimeout(symPriceChecker, 5000);   
};

const directMessage=async(客户端、消息、dicsordUser)=>{
const user=wait client.users.fetch(discordUser.catch)(()=>{});
如果(!user)返回console.log('no user found');
用户发送(消息);
}
函数symPriceChecker(){
sellStock.forEach(条目=>{
if(parseInt(entry.Price)
谢谢,它成功了,还需要在第一个之后添加async=also@Andreas是的,我忘了异步