简体   繁体   English

我在启动Node.js IRC机器人时遇到问题

[英]I'm having trouble getting started my node.js IRC bot

So, as a project I am adapting this bot for my IRC channel: https://gist.github.com/996827 因此,作为一个项目,我正在为我的IRC频道改编该机器人: https//gist.github.com/996827

What I am trying to do is test out it's ability to send out messages. 我正在尝试做的是测试它发送消息的能力。 The message function seems to work fine, but I'm having a bit of a hard time trying to make it "listen" for messages from the channel. 消息功能似乎工作正常,但是我很难让它“侦听”来自频道的消息。

//handles incoming messages
irc.handle = function(data)
{
  var i, info;
  for (i = 0; i < irc.listeners.length; i++)
  {
    info = irc.listeners[i][0].exec(data);
    if (info)
    {
      irc.listeners[i][1](info, data);
      if (irc.listeners[i][2])
      {
        irc.listeners.splice(i, 1);
      }
    }
    if (irc.listeners[i] == "string that is being listened for")
    {
      irc.msg("#solidoodle", "Test,test,test"); 
    }
  }
}

I've been trying to match this to some of the talk I've been watching it receive in the console. 我一直在尝试将其与控制台中收到的一些谈话相匹配。 Am I doing anything obviously wrong? 我在做任何明显错误的事情吗? I know I'll need some Regexs to make it work well. 我知道我需要一些正则表达式才能使其正常运行。

irc.listeners is plainly an array whose elements are arrays (though it looks like making it an array of objects would be a better design, since you're using "magic numbers" to index the sub-elements), but in the line if (irc.listeners[i] == "string that is being listened for") you're treating it as if it were an array of strings. irc.listeners显然是一个数组,其元素是数组(尽管看起来像将它做成对象数组会是一个更好的设计,因为您使用的是“幻数”对子元素进行索引),但是if (irc.listeners[i] == "string that is being listened for")您将其视为字符串数组。 I'm guessing you meant if (irc.listeners[i][someOtherMagicNumber] == ... 我猜你的意思if (irc.listeners[i][someOtherMagicNumber] == ...

Once again, use objects, not sub-arrays, to represent each listener, and give their elements meaningful keys. 再一次,使用对象而不是子数组来表示每个侦听器,并为其元素提供有意义的键。 Right now we're left guessing at what the first, second, and third elements of a listener are supposed to represent, and so will you if you need to revisit your code several months from now. 现在,我们只需要猜测侦听器的第一个,第二个和第三个元素应该代表什么,如果您需要在几个月后重新访问代码,那么您也可以猜测。

Also, under some conditions you're deleting an element from irc.listeners but then falling through into further tests of what will now be the next element. 同样,在某些情况下,您将从irc.listeners删除一个元素,但随后会对下一个元素进行进一步测试。 However, that next element will never be subject to the earlier tests, since the next iteration of the loop will skip past it. 但是,由于循环的下一次迭代将跳过该下一个元素,因此该下一个元素将永远不会经受较早的测试。 Removing or inserting elements from an array you're iterating over is quite tricky and easy to get wrong. 从要迭代的数组中删除或插入元素非常棘手,而且很容易出错。

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

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