ircDotNet bot fails to get messages from IRC channel, long login/logout

I'm trying to write bot for irc channel, which will read messages from channel, recognize if they are commands to him and do some actions depends on command which was send. I've choose ircDotNet because it was the only library that contains some examples how to use it, but they are actually very outdated, only half of them works. My lack of experience in C# and in programming at all don't allows me to understand stuff without good examples :(

So what my program does now:

  • logs in to server using password
  • joins channel
  • log-outs (very buggy)
  • I cant capture and send any messages from and to a channel and i cant log-out instantly.

    Global classes that used for login and IrcClient class exemplar used everywhere in events

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

    Also all current events:

    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());}
        }
    

    Login button code:

       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");
                }
            }
    

    Logic is: program checks if ircClient connected or not, and do some action. Then after that action appropriate event will raise, enable that button again. But that Quit function works very slow or don't works at all, bot will stay at channel until i don't close my program (maybe i need to dispose ircclient?)

    Connect and disconnect events. In connect event, bot will join channel. Bot appears at channel after ~30 seconds after i press connect button, but connected event raised after 2-3 seconds. And same for disconnect - disconnect event raises quickly, but bot stays on channel for much longer time (about 120 seconds).

      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();
            }
    

    Join channel and message received events. They are never raising, have no idea why.

     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); }
            }
    

    So main question is: how do i catch messages from channel and how do i send message to channel? I would appreciate any examples. You can find all code in one piece here: http://pastebin.com/TBkfL3Vq Thanks


    You try to join channel before adding an event.

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

    My suggestion is try adding event first like this:

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

    There is a bug in the IRC.NET library and twitch.tv is using a non-standard message reply that is tripping up IRC.NET.

    I have created a bug here describing it. But basically twitch sends "Welcome, GLHF!" as the RPL_WELCOME message. The IRC RFC describes the format of the message to be "Welcome to the Internet Relay Network !@".

    IRC.NET parses GLHF out of the welcome message as your nick name, which is used for things like firing the JoinedChannel and MessageRecieved events.

    My solution is to download the source code and to comment out where it sets the nick name when receiving the RPL_WELCOME message. It sets the Nickname correctly from the IrcRegistrationInfo passed into the IrcClient constructor and doesn't need to be parsed from the welcome message from twitch. Not sure if this is the case for other IRC servers.

    The function is called ProcessMessageReplyWelcome in IrcClientMessageProcessing.cs:

        /// <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());
        }
    

    A more involved solution might be to refine the nick name Regex so it does not match on GLHF!, which I think is not a valid nickname.

    IRC.NET uses case sensitive string comparisons for finding users by nickname. So the value you pass into the IrcRegistrationInfo for the nickname must match the casing that twitch uses in messages pertaining to you. Which is all lowercase.

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

    上一篇: python irc bot ping的答案

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