CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 4 of 4
  1. #1
    Join Date
    Jun 2012
    Posts
    4

    Basic IRC Bot help

    Hey guys,

    First post here and looking for a little bit of help. Recently started learning C# - and in the past i used to code IRC bots to help learn various things, socket programming, data parsing and so on. So i decided to do the same with C#.

    All of the code im using is by no means 100% mine, i've used other sources as references to learn from and have used snippets from here and there.

    I know the codes messy, the commands could be put into a switch statement and so on, just for now i'm trying to get what i have working.

    The main issue im having is sending raw data to the server, i can't get the !Quit command working properly. - If possible, i'd like to see how i can get a successful !Say Message Here command so i can see how to parse data etc.

    The code im using is below, any comments, criticism or extremely simple examples would be hugely appreciated.

    Code:
    using System;
    using System.Collections.Generic;
    using System.IO;
    using System.Linq;
    using System.Net;
    using System.Net.Sockets;
    using System.Text;
    using System.Threading.Tasks;
    using System.Diagnostics;
    
    namespace iBot
    {
        class Program
        {
            static void Main(string[] args)
            {
                
                string owner = "Ice_Dragon";
                string nickname = "iBot";
                string server = "irc.greyhack.org";
                int port = 6667;
                string channel = "#greyhack";
    
                
                StreamWriter swrite;
                StreamReader sread;
                NetworkStream sstream;
                TcpClient irc;
    
               
                bool botOn = true;
                string line; 
                string[] splitLine;
                string ip;
                Random rng = new Random();
    
                try
                {
    
               // Connect to server
    
                    irc = new TcpClient(server, port);
                    sstream = irc.GetStream();
                    sread = new StreamReader(sstream);
                    swrite = new StreamWriter(sstream);
                }
                catch (Exception e)
                {
                    Console.WriteLine("Error connecting to {0}: {1}", server, e);
                    return;
                }
    
                // Identify to server
                
                
                swrite.WriteLine("USER {0} {0} {0} :{1}", nickname, nickname);
                swrite.Flush();
                swrite.WriteLine("NICK {0}", nickname);
                swrite.Flush();
    
                while (botOn)
                {
    
                    if ((line = sread.ReadLine()) != null)
                    {
    
                        Console.WriteLine(line);
    
                        splitLine = line.Split(' ');
    
                        if (splitLine.Length > 0)
                        {
                            switch (splitLine[1])
                            {
                                case "376":
                                case "422":
                                    swrite.WriteLine("JOIN {0}", channel);
                                    swrite.Flush();
                                    break;
                            }
    
                            if (splitLine[0] == "PING")
                            {
                                swrite.WriteLine("PONG {0}", splitLine[1]);
                                swrite.Flush();
                            }
                        }
    
                        
                        // ** Commands Start Here ** //
    
                        
                        string command = line;
                        command = command.ToLower();
                        
                      
    
    
                        if (command.Contains("!about"))
                        {
            
                            swrite.WriteLine("PRIVMSG {0} :" + "I'm a shitty little bot coded by " + owner, channel);
                            swrite.Flush();
                        }
    
                        if (command.Contains("!join"))
                        {
                            swrite.WriteLine("JOIN {1} :");
                            swrite.Flush();
    
                        }
    
                        if (command.Contains("!Quit"))
                        {
    
                            swrite.WriteLine("QUIT :");
                            swrite.Flush();
          
                        }
    
                        if (command.Contains("!time"))
                        {
    
                            swrite.WriteLine("PRIVMSG {0} :" + "The Time is " + DateTime.Now.ToLongTimeString(), channel); 
                            swrite.Flush();
                        }
    
                        if (command.Contains("!date"))
                        {
                            swrite.WriteLine("PRIVMSG {0} :" + "The Date is " + DateTime.Now.ToLongDateString(), channel);
                            swrite.Flush();
                        }
    
                        // ** End of Command Block** //
                    
    
                    }
                }
    
                
                swrite.Close();
                sread.Close();
                
            }    
    
        }
    }
    Thanks in advance.

  2. #2
    Join Date
    Feb 2012
    Location
    Strasbourg, France
    Posts
    116

    Re: Basic IRC Bot help

    if (command.Contains("!Quit"))
    {

    I checked what recives the bot when I write !Quit in my irc server
    it reads !quit

    don't ask me why it goes lowercase but that's why it's not working
    (by the way don't forget to add botOn = false inside it, or your bot will loop continously)

  3. #3
    Join Date
    Jun 2012
    Posts
    4

    Re: Basic IRC Bot help

    it automatically converts any command to lowercase:

    command = command.ToLower();

    is that not why?

    Didn't think it would make a difference.. And what does botOn = false do?

    Thanks for the reply.

  4. #4
    Join Date
    Feb 2012
    Location
    Strasbourg, France
    Posts
    116

    Re: Basic IRC Bot help

    well if your parse all input to lower case then

    if (command.Contains("!Quit"))
    will never trigger as Contains is case sensitive
    just write
    if (command.Contains("!quit"))


    When your bot is asked to quit IRC, then it should also close it's connections. How do you do that in your code ?

    by leaving you while (botOn) loop
    so botOn = false

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured