【问题标题】:SmartIRC4Net won't connect/shows no activitySmartIRC4Net 无法连接/显示无活动
【发布时间】:2011-10-26 13:51:30
【问题描述】:

我正在使用 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 消息”。)]

【问题讨论】:

  • 它没有你要附加到的 onConnected/onMessage 接收事件吗?
  • 我相信它有一个 OnConnected 事件,我会添加它并看看会发生什么,但是你所说的“你应该附加到”是什么意思,以及 OnMessageReceived 事件会做什么?
  • 我会在我离开 iPad 时写一个答案,给我几分钟时间。
  • 这里,我更新了。我会尝试添加一个 sendmessage 行。顺便说一句,感谢您的帮助。
  • 这很可能是因为您缺少 irc.Listen();你的 catch 语句关闭后的命令。

标签: c# irc bots


【解决方案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 行上设置断点,您应该开始看到来自机器人的内容。 如果这似乎不起作用,您可以尝试在 IRC 服务器上创建自己的频道。

另外,不要忘记:如果您确定您的机器人使用的用户名是唯一的并且可以正常工作(即您可以自己登录),那么用户可以在不进入频道的情况下连接到 IRC通过 mirc 或其他方式)只是在程序似乎已连接时尝试向您的机器人发送 /privmsg 命令。

编辑:另外,我刚刚注意到您的程序没有循环。 您需要添加 irc.Listen();在您的暂停声明之前。这将使 irc 机器人进入侦听模式并且是一个阻塞循环,因此此时退出程序的唯一方法是结束任务,但至少它会显示它正在工作。

编辑 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);

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2018-01-24
    • 2013-01-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-03-03
    相关资源
    最近更新 更多