SmartIRC4Net不会连接/显示任何活动

时间:2011-08-13 21:40:53

标签: c# irc bots

我正在使用SmartIRC4Net库(http://www.meebey.net/projects/smartirc4net/)在c#中制作机器人。如果您不熟悉该库,请随时告诉我一个替代方案。

我使用它是因为这是我能找到的支持最多的库。我阅读了“测试”示例机器人,并尝试通过删除查询和响应输入将其删除为基础。

我对它进行了编程,试图连接到他们的网络频道,因为没有更好的测试,并且它似乎没有连接。当我调试机器人时,我的客户端上没有显示任何内容(我现在正在他们的频道上)。控制台也不显示任何IRC错误消息或异常,只显示我在结尾处的暂停。 代码:

public static IrcClient irc = new IrcClient();

public static void Main(string[] args)
{

        //Setup
        irc.Encoding = System.Text.Encoding.UTF8;
        irc.SendDelay = 200;
        irc.ActiveChannelSyncing = true;

        //Event Handlers
        irc.OnError += new ErrorEventHandler(irc_OnError);
        irc.OnConnected += new EventHandler(irc_OnConnected);
        irc.OnRawMessage += new IrcEventHandler(irc_OnRawMessage);

        try
        {
            //Connect, log in, join channel
            irc.Connect("irc.freenode.org", 6667);
            irc.Login("HGPBot", "HGP Bot");
            irc.RfcJoin("#smartirc");
        }
        catch (Exception e)
        {
            Console.WriteLine("Could not connect, exception:" + Environment.NewLine
                + e.Message + Environment.NewLine
                + e.ToString());
        }

        //pause
        Console.WriteLine("Press any key to continue");
        Console.ReadKey(true);

        //Disconnect
        irc.Disconnect();

        //Exit
        Environment.Exit(0);
    }

    static void irc_OnRawMessage(object sender, IrcEventArgs e)
    {
        Console.WriteLine("irc_OnRawMessage initiated");
    }

    static void irc_OnConnected(object sender, EventArgs e)
    {
        Console.WriteLine("Connected");
        irc.SendMessage(SendType.Message, "#smartirc", "Connected");
    }

    static void irc_OnError(object sender, ErrorEventArgs e)
    {
        Console.WriteLine("IRC Error: " + e.ErrorMessage);
    }

[更新:添加了@Russ C建议的irc_OnConnected事件。触发事件并在控制台上记录“已连接”。但是,频道上没有任何反应。我将添加一个sendmessage行,看看会发生什么。]

[Update2:添加了SendMessage和OnRawMessage事件。通道上不显示任何输出,并且OnRawMessage事件下的文本不会写入控制台。 (我是否使用OnMessage的正确事件?“OnMessage”事件不存在,测试机器人说OnMessage将“获取所有IRC消息”。)]

1 个答案:

答案 0 :(得分:1)

确定;像所有基于事件的逻辑(在这里读取异步逻辑),您需要订阅一个事件,以便库可以在有事情发生时通知您。 因为您的测试代码没有订阅/附加到SmartIRC库中的任何事件,所以该库只是静止不动。

您正在使用irc.OnError行执行此操作,但您还需要添加这些方法:

irc.OnQueryMessage += new IrcEventHandler(OnQueryMessage);
irc.OnRawMessage += new IrcEventHandler(OnRawMessage);

然后是几种方法:

// this method we will use to analyse queries (also known as private messages)
public static void OnQueryMessage(object sender, IrcEventArgs e)
{
    switch (e.Data.MessageArray[0]) {
        case "hello":
           // this is where you decipher private messages posted to the bot.
           // if someone does "/privmsg HGPBot hello" this will reply "Hello!"
           irc.SendMessage(SendType.Message, "HGPBot, "Hello!");
           break;
        default:
           break;
    }
}

// this method will get all IRC messages
public static void OnRawMessage(object sender, IrcEventArgs e)
{
    System.Console.WriteLine("Received: "+e.Data.RawMessage);
}

如果你在这个System.Console行上设置一个断点,你应该开始看到来自bot的事情。 如果这似乎不起作用,您可以尝试在IRC服务器上创建自己的频道。

此外,请不要忘记:如果您确定您的机器人使用的用户名是唯一且正在运行的用户可以连接到IRC而不在频道中(即您可以自己登录)通过mirc或其他任何方法)只要尝试在程序连接时向您的机器人发送/ privmsg命令。

编辑:另外,我刚注意到你的程序没有循环。 你需要添加irc.Listen();在你的暂停声明之前。这会将irc bot置于监听模式并且是一个阻塞循环,因此在此时退出程序的唯一方法是结束任务,但至少它会告诉你它的工作。

编辑2:让机器人听:

// here we tell the IRC API to go into a receive mode, all events
// will be triggered by _this_ thread (main thread in this case)
// Listen() blocks by default, you can also use ListenOnce() if you
// need that does one IRC operation and then returns, so you need then 
// an own loop 
irc.Listen();
//pause
Console.WriteLine("Press any key to continue");
Console.ReadKey(true);