简体   繁体   English

Discord.js“通道”未定义错误消息

[英]Discord.js “channel” not defined error message

I recently became interested in making a discord bot using Javascript and node.js.我最近对使用 Javascript 和 node.js 制作 discord 机器人产生了兴趣。 I am trying to make a command that spins a virtual "lottery", and sends a message telling the user whether they have received anything.我正在尝试制作一个旋转虚拟“彩票”的命令,并发送一条消息告诉用户他们是否收到任何东西。 (Here is my code:) (这是我的代码:)

` `

function lotteryCommand(arguments, receivedMessage){


/*note: receivedMessage is defined as the command (for my bot, it's e!spin) 
  and arguments is the part following the command (for this particular bit of code, 
  it's merits, so the user sends "e!spin merits")*/

    if (arguments == "merits"){
        setTimeout(randomlottery1, 1000);
    }
    else{receivedMessage.channel.send("Message here")}
    }

here is the other function.这是另一个 function。 This is where it stops working这是它停止工作的地方

  function randomlottery1(arguments, receivedMessage){
    let respond;
    let responses = [
        //some random phrases here
    ]
    respond = responses[Math.floor(Math.random() * responses.length)]
    receivedMessage.channel.send(respond)
}

For some reason I cannot understand, it does not recognize the channel in receivedMessage.channel.send the second time, in the randomlottery1 function, but it recognized the command earlier in the code.由于某种原因,我无法理解,它第二次在randomlottery1 function 中无法识别receivedMessage.channel.send消息.channel.send 中的channel ,但它识别了代码中前面的命令。 Is there anything I am doing wrong here?我在这里做错了什么吗? Thanks.谢谢。

I think your problem is you are not passing in any parameters to your function, you can easily fix it by doing this:我认为您的问题是您没有将任何参数传递给您的 function,您可以通过以下操作轻松修复它:

function lotteryCommand(arguments, receivedMessage){


/*note: receivedMessage is defined as the command (for my bot, it's e!spin) 
  and arguments is the part following the command (for this particular bit of code, 
  it's merits, so the user sends "e!spin merits")*/

    if (arguments == "merits"){
            // dont forget to pass in the required parameters while executing your function (parameter1, parameter2)
            setTimeout(randomlottery1(arguments, receivedMessage), 1000);
    }
    else{receivedMessage.channel.send("Message here")}
    }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM