ircDotNet bot无法从IRC频道获取消息,长时间登录/注销

我试图为irc通道编写bot,它将从通道读取消息,识别它们是否是对他的命令,并根据发送的命令执行一些操作。 我选择了ircDotNet,因为它是唯一包含一些示例如何使用它的库,但它们实际上已经过时,只有一半可以使用。 我在C#和编程方面缺乏经验并不能让我理解没有好例子的东西:(

所以我的程序现在做了什么:

  • 使用密码登录到服务器
  • 加入频道
  • 注销(非常错误)
  • 我无法捕获并发送任何来自某个频道的消息,而且我无法立即注销。

    用于登录和IrcClient类示例的全局类在事件中随处可见

     public IrcRegistrationInfo  irc_iri 
            {
                get
                {
                    return new IrcUserRegistrationInfo()
                    {
                        NickName = "jsBot",
                        UserName = "jsBot",
                        RealName = "jsBot",
                        Password = "oauth:p4$$w0rdH3Re48324729214812489"
                    };
                }
            }
       public IrcClient gIrcClient = new IrcClient();
    

    当前所有事件:

    private void Form1_Load(object sender, EventArgs e)
        {
            try
            {
                gIrcClient.Connected += ircClient_Connected;
                gIrcClient.Disconnected += gIrcClient_Disconnected;
                gIrcClient.FloodPreventer = new IrcStandardFloodPreventer(1, 10000);
            }
            catch (Exception ex) { MessageBox.Show(ex.ToString());}
        }
    

    登录按钮代码:

       private void button1_Click(object sender, EventArgs e)
            {
                button1.Enabled = false;
    
                if (!gIrcClient.IsConnected)
                {
                    button1.Text = "Connecting...";
                    gIrcClient.Connect("irc.twitch.tv", 6667, false, irc_iri);
                }
                else
                {
                    button1.Text = "Disconnecting...";
                    gIrcClient.Quit(5000, "bye");
                }
            }
    

    逻辑是:程序检查ircClient是否连接,并执行一些操作。 然后在那个动作之后适当的事件将会升起,再次启用该按钮。 但是,退出功能工作非常缓慢或根本不工作,机器人将停留在通道,直到我不关闭我的程序(也许我需要处理ircclient?)

    连接和断开事件。 在连接事件中,机器人将加入频道。 在按下连接按钮约30秒后,Bot出现在通道中,但连接的事件在2-3秒后上升。 断开连接也是一样 - 断开连接事件迅速增加,但机器人在通道上停留的时间更长(大约120秒)。

      void ircClient_Connected(object sender, EventArgs e)
            {
                try
                {
                    if (button1.InvokeRequired)
                    {
                        MethodInvoker del = delegate { 
                            button1.Text = "Disconnect"; 
                            button1.Enabled = true; };
                        button1.Invoke(del);
                    }
                    else
                    {
                        button1.Text = "Disconnect"; 
                        button1.Enabled = true;
                    }
                    gIrcClient.Channels.Join("#my_channel");   
                    gIrcClient.LocalUser.JoinedChannel += LocalUser_JoinedChannel;             
                }
                catch (Exception ex) { MessageBox.Show(ex.Message); }
            }
    
            void gIrcClient_Disconnected(object sender, EventArgs e)
            {
                if (!gIrcClient.IsConnected)
                {
                    try
                    {
                        if (button1.InvokeRequired)
                        {
                            MethodInvoker del = delegate
                            {
                                button1.Text = "Connect";
                                button1.Enabled = true;
                            };
                            button1.Invoke(del);
                        }
                        else
                        {
                            button1.Text = "Connect";
                            button1.Enabled = true;
                        }
                    }
                    catch (Exception ex) { MessageBox.Show(ex.Message); }
                }
                else gIrcClient.Disconnect();
            }
    

    加入频道和消息收到的事件。 他们从不举起,不知道为什么。

     void LocalUser_JoinedChannel(object sender, IrcChannelEventArgs e)
            {
                try
                {                
                    gIrcClient.Channels[0].MessageReceived += Form1_MessageReceived;
                    gIrcClient.LocalUser.SendMessage(e.Channel, "test");
                    MessageBox.Show(gIrcClient.Channels[0].Users[0].User.NickName);
                    MessageBox.Show("bot_join_channel_event_raised");
                }
                catch (Exception ex) { MessageBox.Show(ex.Message); }
            }
    
            void Form1_MessageReceived(object sender, IrcMessageEventArgs e)
            {
                try
                {
                    if (e.Text.Equals("asd"))
                        gIrcClient.LocalUser.SendMessage(e.Targets, "received");
                }
                catch (Exception ex) { MessageBox.Show(ex.Message); }
            }
    

    所以主要的问题是:我如何捕捉来自频道的消息,以及如何向频道发送消息? 我会很感激任何例子。 你可以在这里找到所有的代码:http://pastebin.com/TBkfL3Vq谢谢


    您尝试在添加活动之前加入频道。

      gIrcClient.Channels.Join("#my_channel");   
      gIrcClient.LocalUser.JoinedChannel += LocalUser_JoinedChannel;
    

    我的建议是尝试像这样首先添加事件:

      gIrcClient.LocalUser.JoinedChannel += LocalUser_JoinedChannel;
      gIrcClient.Channels.Join("#my_channel");   
    

    IRC.NET库中存在一个错误,twitch.tv使用非标准消息回复,导致IRC.NET跳闸。

    我在这里创建了一个错误来描述它。 但基本抽动发送“欢迎,GLHF!” 作为RPL_WELCOME消息。 IRC RFC将消息的格式描述为“欢迎使用Internet Relay Network!@”。

    IRC.NET将GLHF从欢迎消息中解析出来,作为你的昵称,这个名字用于发射JoinedChannel和MessageRecieved事件。

    我的解决方案是下载源代码并在收到RPL_WELCOME消息时注释它设置昵称的位置。 它将传递到IrcClient构造函数的IrcRegistrationInfo中的Nickname设置为正确,并且不需要从来自twitch的欢迎消息中解析。 不确定这是否适用于其他IRC服务器。

    该函数在IrcClientMessageProcessing.cs中称为ProcessMessageReplyWelcome:

        /// <summary>
        /// Process RPL_WELCOME responses from the server.
        /// </summary>
        /// <param name="message">The message received from the server.</param>
        [MessageProcessor("001")]
        protected void ProcessMessageReplyWelcome(IrcMessage message)
        {
            Debug.Assert(message.Parameters[0] != null);
    
            Debug.Assert(message.Parameters[1] != null);
            this.WelcomeMessage = message.Parameters[1];
    
            // Extract nick name, user name, and host name from welcome message. Use fallback info if not present.
            var nickNameIdMatch = Regex.Match(this.WelcomeMessage.Split(' ').Last(), regexNickNameId);
            //this.localUser.NickName = nickNameIdMatch.Groups["nick"].GetValue() ?? this.localUser.NickName;
            this.localUser.UserName = nickNameIdMatch.Groups["user"].GetValue() ?? this.localUser.UserName;
            this.localUser.HostName = nickNameIdMatch.Groups["host"].GetValue() ?? this.localUser.HostName;
    
            this.isRegistered = true;
            OnRegistered(new EventArgs());
        }
    

    更复杂的解决方案可能是改进昵称Regex,使其与GLHF!不匹配,我认为这不是一个有效的昵称。

    IRC.NET使用区分大小写的字符串比较来按昵称查找用户。 所以你传递给IrcRegistrationInfo昵称的值必须与Twitch在与你有关的消息中使用的外壳匹配。 这是全部小写。

    链接地址: http://www.djcxy.com/p/53467.html

    上一篇: ircDotNet bot fails to get messages from IRC channel, long login/logout

    下一篇: IRC Client C# PING request