我做了一个简单的irc bot如何在加入频道后向频道发送消息?

时间:2013-08-25 03:51:50

标签: c#

这是代码:

private void connectToIrc()
        {
            //Connect to irc server and get input and output text streams from TcpClient.
            nick = textBox1.Text;
            owner = textBox2.Text;
            server = textBox3.Text;
            port = Convert.ToInt32(textBox4.Text);
            chan = "#" + textBox5.Text;
            sock.Connect(server, port);//server, port);

            richTextBox1.Invoke((MethodInvoker)delegate
            {
                ColorText.AppendText(richTextBox1, "Server: ", Color.Green);
                ColorText.AppendText(richTextBox1, server+"          ", Color.Red);
                ColorText.AppendText(richTextBox1, "Port: ", Color.Green);
                ColorText.AppendText(richTextBox1, port.ToString() + Environment.NewLine + Environment.NewLine, Color.Red);
            });
            if (!sock.Connected)
            {
                Console.WriteLine("Failed to connect!");
                return;
            }
            input = new System.IO.StreamReader(sock.GetStream());
            output = new System.IO.StreamWriter(sock.GetStream());

             //Starting USER and NICK login commands 
         output.Write(
            "USER " + nick + " 0 * :" + owner + "\r\n" +
            "NICK " + nick + "\r\n"
         );
         output.Flush();

         //Process each line received from irc server
         //buf = input.ReadLine();
         while ((buf = input.ReadLine()) != null)
         {
             buf = buf + Environment.NewLine;
             //Display received irc message
             //Console.WriteLine(buf);
             richTextBox1.Invoke((MethodInvoker)delegate
             {
                 ColorText.AppendText(richTextBox1, buf,Color.Black);
             });
             if (buf.StartsWith("ERROR")) break;

             //Send pong reply to any ping messages
             if (buf.StartsWith("PING ")) { output.Write(buf.Replace("PING", "PONG") + "\r\n"); output.Flush(); }
             if (buf[0] != ':') continue;

             //After server sends 001 command, we can set mode to bot and join a channel
             if (buf.Split(' ')[1] == "001")
             {
                 output.Write(
                   "MODE " + nick + " +B\r\n" +
                   "JOIN " + chan + "\r\n" + "PRIVMSG " + chan + " :hello"
                 );
                 output.Flush();
             }
             buf = input.ReadLine();
         }

       }

我将这部分添加到代码中:+“PRIVMSG”+ chan +“:hello” 但它没有做任何我在通道中看不到任何“你好”。

我在这个网站上使用了这个例子:

http://jakash3.wordpress.com/2012/02/13/simple-vb-net-and-csharp-irc-client/

C#示例。

如何在加入频道后向频道发送消息?我想发送一条消息,如果它的下一步工作是在队列中添加消息,它们将逐个自动发送。

我该怎么做?

2 个答案:

答案 0 :(得分:1)

只是因为你发送到服务器上你加入了它,并不意味着你立即进入了频道。你需要等到实际的通道,(等待从服务器返回JOIN命令),然后发送privmsg。

答案 1 :(得分:0)

您在PRIVMSG命令结束时忘记了\ r \ n。