首页 > 解决方案 > 机器人不能在 IRC 上工作,用 Node.js 编写

问题描述

我一直在尝试为 IRC 服务器创建一个机器人。IRC 服务器是:“open.ircnet.net”,频道是“#Mal1t1a”。该机器人的代码是用 Node.js 编写的。我在 youtube 上观看了有关如何编写机器人代码的教程,我观看的视频可以在此链接中找到:https ://www.youtube.com/watch?v=OuYIlea4j7g&t=333s 。制作该视频的人制作了一个用于抽搐的机器人,因此需要一个 oauth。由于我已连接到不需要任何密码登录的 IRC 服务器,因此我注释了这些行,其中包含有关密码的代码。我与我的一个朋友在服务器上连接,在我提到的频道上,我们能够交谈,所以服务器正在工作。在这里,您有机器人的代码:

var irc = require("irc");
var config = {
    channels: ["#Mal1t1a"],
    server: "open.ircnet.net",
    botName: "tren",
    //oauth: "oauth:YOURUNIQUEOAUTHTOKEN",
    autoRejoin: false,
    autoConnect: true,
    floodProtection: true,
    floodProtectionDelay: 3000
};

var bot = new irc.Client(config.server, config.botName, {
    channels: config.channels,
    //password: config.oauth
});

function OnMessage(from, channel, text, message)
{
    console.log(from, channel, text);
    if (text == "!greeting")
    {
        bot.send("PRIVMSG", channel, "Hello " + from + "! You have triggered my command!");
    }
}
bot.addListener("message", OnMessage);
bot.addListener("action", OnMessage);

function OnJoin(channel, nick, msgobj)
{
    if (nick.toLowerCase() == config.botName.toLowerCase())
    {
        console.log("Bot has joined channel", channel);
    }
}
bot.addListener("join", OnJoin);

function OnError(message)
{
    console.log("IRC Error:", message);
}
bot.addListener("error", OnError);

我在聊天中写了无数次“!问候”,但机器人没有回答。它可以做什么?

标签: javascriptnode.js

解决方案


推荐阅读