Parsi Coders

نسخه‌ی کامل: c# irc bot
شما در حال مشاهده نسخه آرشیو هستید. برای مشاهده نسخه کامل کلیک کنید.
اینم یه بوتر به زبان سی شارپ برای سرویس ای ار سی

کد:
//coded by Jeandre Pentz on 23/06/2011
//a IRC bot with a lot of features. To change the bot's details just edit the file server_config.txt contained in the installed folder.
//Got a help from Pasihavia Havia guide, http://www.c-sharpcorner.com/UploadFile/pasihavia/IrcBot11222005231107PM/IrcBot.aspx to start my bot -Thanks!
//email: jeandrepentzx@yahoo.com
using System;
using System.Collections;
using System.Linq;
using System.Text;
using System.Net;
using System.Net.Sockets;
using System.IO;
using System.Threading;
//
namespace IRC_BOT_CONSOLE
{


    class Program
    {
        #region static var
        
        
        public static ArrayList notifyPickup = new ArrayList();
        public static string SERVER; // this defines the server you want your bot to connect to
        private static int PORT;// you can leave this the same
        private static string USER = "USER coolbot 8 * coolbot1"; // ident of the bot and real name
        private static string NICK; // nickname of your bot
        private static string CHANNEL; // channel you want your bot to join
        private static string HOST; //Admin of the bot, can control the bot using the !message command(needs to /msg eva !message message)
        public static string PASSWORD;//Password to identify the bot's nick
        public static StreamWriter writer; // used to write to files.
        public static string inputline = "";// line that the bot recieves from the server
        public static string input = "";// inputline that doesnt get converted to lower
        public static string orgMsg;// the original msg inputed
        public static bool isAdmin;// determines if specified player has admin status in channel or not
        public static string match = "NONE NONE NONE NONE NONE";// the current match that needs to be played. SYNTAX: <opponent> <server> <password> <date> <time>
        //public static string capsNICK;
        #endregion

        #region custom methods\

      //  static bool isAdmin(string channel, string nick)
       // {
       //     write("PRIVMSG chanserv STATUS " + channel + nick);
       //     reader.ReadLine
       // }
        
        
        static string getNick()
        {
            int length = input.IndexOf("!");
            string usernick = input.Substring(1, (length-1));
            return usernick;

        }

        static void write(string line)
        {
            writer.WriteLine(line);
            writer.Flush();
            Console.ForegroundColor = System.ConsoleColor.Green;
            Console.WriteLine(" ###"+NICK+"::: " +line);
            Console.ForegroundColor = System.ConsoleColor.White;
        }
        #endregion
        static void Main(string[] args)
        {
            //try
           // {
                #region Facts
                string[] facts = new string[51]
            {
                "1. The word 'queue' is the only word in the English language that is still pronounced the same way when the last four letters are removed.",
                "2. Beetles taste like apples, wasps like pine nuts, and worms like fried bacon.",
                "3. Of all the words in the English language, the word 'set' has the most definitions!",
                "4. What is called a 'French kiss' in the English speaking world is known as an 'English kiss' in France.",
                "5. 'Almost' is the longest word in the English language with all the letters in alphabetical order.",
                "6. 'Rhythm' is the longest English word without a vowel.",
                "7. In 1386, a pig in France was executed by public hanging for the murder of a child",
                "8. A cockroach can live several weeks with its head cut off!",
                "9. You can't kill yourself by holding your breath",
                "10. There is a city called Rome on every continent.",
                "11. It's against the law to have a pet dog in Iceland!",
                "12. Your heart beats over 100,000 times a day!",
                "13. Horatio Nelson, one of England's most illustrious admirals was throughout his life, never able to find a cure for his sea-sickness.",
                "14. The skeleton of Jeremy Bentham is present at all important meetings of the University of London",
                "15. Right handed people live, on average, nine years longer than left-handed people",
                "16. Your ribs move about 5 million times a year, everytime you breathe!",
                "17. The elephant is the only mammal that can't jump!",
                "18. One quarter of the bones in your body, are in your feet!",
                "19. Like fingerprints, everyone's tongue print is different!",
                "20. The first known transfusion of blood was performed as early as 1667, when Jean-Baptiste, transfused two pints of blood from a sheep to a young man",
                "21. Fingernails grow nearly 4 times faster than toenails!",
                "22. Most dust particles in your house are made from dead skin!",
                "23. The present population of 5 billion plus people of the world is predicted to become 15 billion by 2080.",
                "24. Women blink nearly twice as much as men.",
                "25. Adolf Hitler was a vegetarian, and had only ONE testicle.",
                "26. Honey is the only food that does not spoil. Honey found in the tombs of Egyptian pharaohs has been tasted by archaeologists and found edible.",
                "27. Months that begin on a Sunday will always have a 'Friday the 13th.'",
                "28. Coca-Cola would be green if colouring weren’t added to it.",
                "29. On average a hedgehog's heart beats 300 times a minute.",
                "30. More people are killed each year from bees than from snakes.",
                "31. The average lead pencil will draw a line 35 miles long or write approximately 50,000 English words.",
                "32. More people are allergic to cow's milk than any other food.",
                "33. Camels have three eyelids to protect themselves from blowing sand.",
                "34. The placement of a donkey's eyes in its' heads enables it to see all four feet at all times!",
                "35. The six official languages of the United Nations are: English, French, Arabic, Chinese, Russian and Spanish.",
                "36. Earth is the only planet not named after a god.",
                "37. It's against the law to burp, or sneeze in a church in Nebraska, USA.",
                "38. You're born with 300 bones, but by the time you become an adult, you only have 206.",
                "39. Some worms will eat themselves if they can't find any food!",
                "40. Dolphins sleep with one eye open!",
                "41. It is impossible to sneeze with your eyes open",
                "42. The worlds oldest piece of chewing gum is 9000 years old!",
                "43. The longest recorded flight of a chicken is 13 seconds",
                "44. Queen Elizabeth I regarded herself as a paragon of cleanliness. She declared that she bathed once every three months, whether she needed it or not.",
                "45. Slugs have 4 noses.",
                "46. Owls are the only birds who can see the colour blue.",
                "47. A man named Charles Osborne had the hiccups for 69 years!",
                "48. A giraffe can clean its ears with its 21-inch tongue!",
                "49. The average person laughs 10 times a day!",
                "50. An ostrich's eye is bigger than its brain.",
                "51. Human thigh bones are stronger than concrete."
            };


                #endregion

                NetworkStream stream; // defines the sockets
                TcpClient irc;
                StreamReader reader;
                string curNick;
                #region FileCreation
                if (!File.Exists("../../server_config.txt"))//creates a new server_config.txt if there isnt one.
                {


                    FileStream fs = new FileStream("../../server_config.txt", FileMode.Create);
                    fs.Close();
                    StreamWriter sw = new StreamWriter("../../server_config.txt");
                    sw.WriteLine("snow.Shadowfire.org");
                    sw.WriteLine("6667");
                    sw.WriteLine("Eva");
                    sw.WriteLine("#first-squad");
                    sw.WriteLine("VaguEh");
                    sw.WriteLine("evawow");
                    sw.Close();
                }
                if (!File.Exists("../../notifypickuplist.txt"))
                {
                    FileStream fs = new FileStream("../../notifypickuplist.txt", FileMode.Create);
                    fs.Close();

                }
                StreamReader npReader = new StreamReader("../../notifypickuplist.txt");
                while (!npReader.EndOfStream)
                {
                    notifyPickup.Add(npReader.ReadLine());
                }
                npReader.Close();
                #endregion

                #region Connection


                StreamReader sr = new StreamReader("../../server_config.txt");
                SERVER = sr.ReadLine().ToLower();
                PORT = Int32.Parse(sr.ReadLine());
                NICK = sr.ReadLine();
                CHANNEL = sr.ReadLine().ToLower();
                HOST = sr.ReadLine().ToLower();
                PASSWORD = sr.ReadLine();
                sr.Close();
                //Writes the server_config details to the console.
                Console.WriteLine("SERVER: " + SERVER);
                Console.WriteLine("PORT: " + PORT);
                Console.WriteLine("NICKNAME: " + NICK);
                Console.WriteLine("CHANNEL: " + CHANNEL);
                Console.WriteLine("MAIN ADMIN: " + HOST);
                Console.WriteLine("PASSOWRD: " + PASSWORD);
                Console.WriteLine();

                irc = new TcpClient(SERVER, PORT);

                stream = irc.GetStream();
                reader = new StreamReader(stream);
                writer = new StreamWriter(stream);
                PingSender ping = new PingSender();
                ping.Start();
                write(USER); // sending user information
                write("NICK " + NICK); // sending your nick
                write("PRIVMSG IDENTIFY evawow");
                Console.Title = NICK;
                NICK = NICK.ToLower();
                write("JOIN " + CHANNEL); // joining your channel
                //write("JOIN #final-resistance");
                write("IDENTIFY " + PASSWORD);


                #endregion
                while (inputline != null)
                {
                    //write("PRIVMSG IDENTIFY evawow");
                    int Length;
                    inputline = reader.ReadLine();//Reads the line that the bot recieves from the server.
                    input = inputline;//string that isnt getting converted to lowercase
                    Console.WriteLine(inputline); // this will output all server commands to the console
                    if (inputline.Contains("!"))
                    {

                        Length = inputline.IndexOf("!", ((inputline.IndexOf("!") + 1)));
                    }
                    else
                        Length = 1;
                    inputline = inputline.ToLower();
                    //inputline =((inputline.Substring(0, Length) + inputline.Substring(Length, inputline.Length - Length)).ToLower());//makes the command that the user enters to lowercase so that if the user uses caps or non caps, it will still work correctly.
                    #region COMMANDS


                    if (inputline.Contains("privmsg " + CHANNEL + " :!hello"))//Command that lets the bot greet you.
                    {
                        write("PRIVMSG " + CHANNEL + " :Hello " + getNick() + "!");

                    }
                    else if (inputline.Contains("join :" + CHANNEL)) //Messages a user when they join the channel.
                    {
                        curNick = getNick();
                        write("NOTICE " + curNick + " :Hello " + curNick + ". Welcome to " + CHANNEL + "!");
                        write("NOTICE " + curNick + " :Type !help to get a list of all the available commands.");
                    }
                    else if (inputline.Contains("privmsg " + NICK + " :!message"))//Lets the bot talk in the channel. Only the admin has rights to this command. must be private message example: /msg eva !message I'm the admin typing via Eva
                    {

                        string message = input.Remove(0, Length + 9);
                        write("PRIVMSG " + CHANNEL + " :" + message);

                    }
                    else if (inputline.Contains("privmsg " + CHANNEL + " :!help"))//displays a list of commmands.
                    {
                        curNick = getNick();
                        if (inputline.Contains("!date"))
                        {
                            write("NOTICE " + curNick + " :Shows the current date.");
                        }
                        else if (inputline.Contains("!time"))
                        {
                            write("NOTICE " + curNick + " :Shows the current time.");
                        }
                        else if (inputline.Contains("!addnotifylist"))
                        {
                            write("NOTICE " + curNick + " :Adds you the the notification system that lets you know when a pickup has started(!sg).");
                        }
                        else if (inputline.Contains("!remnotifylist"))
                        {
                            write("NOTICE " + curNick + " :Removes you from the notification system that lets you know when a pickup has started(!sg).");
                        }
                        else if (inputline.Contains("!admincommands"))
                        {
                            write("NOTICE " + curNick + " :Displays all the admin commands. only admins can use this command.");
                        }
                        else if (inputline.Contains("!ban"))
                        {
                            write("NOTICE " + curNick + " :Lets a admin ban a specified user. Syntax: !ban <username> [message]");
                        }
                        else if (inputline.Contains("!kick"))
                        {
                            write("NOTICE " + curNick + " :Lets a admin kick a specified user. Syntax: !kick <username> [message]");
                        }
                        else if (inputline.Contains("!unban"))
                        {
                            write("NOTICE " + curNick + " :Lets a admin unban a specified banned user. Syntax: !unban <username>");
                        }
                        else if (inputline.Contains("!cod4lineup"))
                        {
                            write("NOTICE " + curNick + " :Shows the lineup of the best team in SA :D");
                        }
                        else if (inputline.Contains("!fact"))
                        {
                            write("NOTICE " + curNick + " :Gives a random fact. Useless but fun command :)");
                        }
                        else if (inputline.Contains("!match"))
                        {
                            write("NOTICE " + curNick + " :Displays the current match that needs to be played.");
                        }
                        else if (inputline.Contains("!setmatch"))
                        {
                            write("NOTICE " + curNick + " :lets a admin set a new match. Must be pm'ed to the bot. Syntax: !setmatch <opponent> <server> <password> <date> <time>");
                        }
                        else
                        {
                            write("NOTICE " + curNick + " :available commands:");
                            write("NOTICE " + curNick + " :!hello, !date, !time, !addNotifyList, !remNotifyList,!adminCommands, !CoD4Lineup, !fact, !match");
                            write("NOTICE " + curNick + " :If you need help with a certain command just type !help, followed by the command. Example: !help !date");
                        }
                    }
                    else if (inputline.Contains("privmsg " + CHANNEL + " :!date"))//gets the date from the pc the bot is hosted on
                    {
                        write("PRIVMSG " + CHANNEL + " :Today's Date: " + DateTime.Now.ToShortDateString());
                    }
                    else if (inputline.Contains("privmsg " + CHANNEL + " :!time"))//gets the time from the pc the bot is hosted on
                    {
                        write("PRIVMSG " + CHANNEL + " :Current Time: " + DateTime.Now.ToLongTimeString());
                    }
                    else if (inputline.Contains("privmsg " + CHANNEL + " :!addnotifylist"))
                    {
                        curNick = getNick().ToLower();
                        if (!(notifyPickup.Contains(curNick)))
                        {
                            notifyPickup.Add(curNick);
                            StreamWriter addwriter = new StreamWriter("../../notifypickuplist.txt");
                            for (int loop = 0; loop < notifyPickup.Count; loop++)
                            {

                                addwriter.WriteLine(notifyPickup[loop]);

                            }
                            addwriter.Close();
                            write("NOTICE " + curNick + " :You have been added to the pickup notification list.");
                        }
                        else
                        {
                            write("NOTICE " + curNick + " :You are already on the list bro! :/");
                        }


                    }
                    else if (inputline.Contains("privmsg " + CHANNEL + " :!remnotifylist"))
                    {
                        curNick = getNick().ToLower();
                        if (notifyPickup.Contains(curNick))
                        {
                            notifyPickup.Remove(curNick);
                            StreamWriter remwriter = new StreamWriter("../../notifypickuplist.txt");
                            for (int loop = 0; loop < notifyPickup.Count; loop++)
                            {

                                remwriter.WriteLine(notifyPickup[loop]);

                            }
                            remwriter.Close();
                            write("NOTICE " + curNick + " :You  have been removed from the pickup notification list.");

                        }
                        else
                        {
                            write("NOTICE " + curNick + " : You are not on the list. Try to add to the list next time first so that i can remove you.FACEPALM.");
                        }

                    }
                    else if (inputline.Contains("privmsg #cod4pickup :6*amsg* 14a pickup for call of duty 4 has been started in channel #cod4pickup"))
                    {
                        for (int loop = 0; loop < notifyPickup.Count; loop++)//lets the bot notify everyone that is on the list that a pickup has started
                        {
                            write("PRIVMSG " + notifyPickup[loop] + " :Yo " + notifyPickup[loop] + "! A pickup has started in #cod4pickup!");
                            write("PRIVMSG " + notifyPickup[loop] + " :!ADD quickly! :D");
                        }
                    }
                    else if (inputline.Contains("privmsg " + NICK + " :!do")) //lets the admin send commands to the bot. only admin can use this command
                    {
                        if (getNick() == HOST | getNick() == "VaguEh")
                        {
                            string message = inputline.Remove(0, Length + 4);
                            write(message);
                            write("PRIVMSG " + getNick() + " :command '" + message + "' was executed.");
                        }
                    }
                    else if (inputline.Contains("privmsg " + CHANNEL + " :!kick") & inputline.Length > Length + 6)// kicks a user
                    {
                        string orgmsg = inputline;
                        write("WHOIS " + getNick());
                        inputline = reader.ReadLine();

                        while (!inputline.Contains("End of /WHOIS list."))
                        {
                            inputline = reader.ReadLine();
                            if (inputline.Contains("@" + CHANNEL) | inputline.Contains("~" + CHANNEL) | inputline.Contains("&" + CHANNEL))
                            {
                                orgmsg = orgmsg.Remove(0, Length + 6);
                                write("KICK " + CHANNEL + " " + orgmsg);
                            }
                        }

                    }
                    else if (inputline.Contains("privmsg " + CHANNEL + " :!ban") & inputline.Length > Length + 5)//bans a user
                    {
                        string orgmsg = inputline;
                        write("WHOIS " + getNick());
                        inputline = reader.ReadLine();
                        while (!inputline.Contains("End of /WHOIS list."))
                        {
                            inputline = reader.ReadLine();
                            if (inputline.Contains("@" + CHANNEL) | inputline.Contains("~" + CHANNEL) | inputline.Contains("&" + CHANNEL))
                            {
                                orgmsg = orgmsg.Remove(0, Length + 5);
                                write("MODE " + CHANNEL + " +b " + orgmsg);
                                write("KICK " + CHANNEL + " " + orgmsg);
                            }
                        }

                    }
                    else if (inputline.Contains("privmsg " + CHANNEL + " :!unban") & inputline.Length > Length + 7)// unbans a user
                    {
                        orgMsg = inputline;
                        write("WHOIS " + getNick());
                        inputline = reader.ReadLine();
                        while (!inputline.Contains("End of /WHOIS list."))
                        {
                            inputline = reader.ReadLine();
                            if (inputline.Contains("@" + CHANNEL) | inputline.Contains("~" + CHANNEL) | inputline.Contains("&" + CHANNEL))
                            {
                                orgMsg = orgMsg.Remove(0, Length + 7);
                                write("MODE " + CHANNEL + " -b " + orgMsg);

                            }
                        }

                    }
                    else if (inputline.Contains("privmsg " + CHANNEL + " :!admincommands"))//displays all the admincommands
                    {

                        isAdmin = false;
                        write("WHOIS " + getNick());
                        inputline = reader.ReadLine();
                        while (!inputline.Contains("End of /WHOIS list."))
                        {
                            inputline = reader.ReadLine();
                            if (inputline.Contains("@" + CHANNEL) | inputline.Contains("~" + CHANNEL) | inputline.Contains("&" + CHANNEL))
                            {
                                isAdmin = true;

                            }

                        }
                        if (isAdmin)
                        {
                            write("NOTICE " + getNick() + " :Admin Commands:");
                            write("NOTICE " + getNick() + " :!kick, !ban, !unban, !setTopic, !setmatch");
                        }
                        else
                        {
                            write("NOTICE " + getNick() + " :PERMISSION DENIED.");
                        }
                    }
                    else if (inputline.Contains("privmsg " + CHANNEL + " :!settopic"))
                    {
                        orgMsg = inputline.Remove(0, Length + 10);
                        isAdmin = false;
                        write("WHOIS " + getNick());
                        inputline = reader.ReadLine();
                        while (!inputline.Contains("End of /WHOIS list."))
                        {
                            inputline = reader.ReadLine();
                            if (inputline.Contains("@" + CHANNEL) | inputline.Contains("~" + CHANNEL) | inputline.Contains("&" + CHANNEL))
                            {
                                isAdmin = true;

                            }

                        }
                        if (isAdmin)
                        {

                            write("TOPIC " + CHANNEL + " :" + orgMsg);
                            //write("NOTICE " + getNick() + " :ADMIN COMMANDS:");
                            //write("NOTICE " + getNick() + " :!kick, !ban, !unban, !settopic");
                        }
                        else
                        {
                            write("NOTICE " + getNick() + " :PERMISSION DENIED.");
                        }
                    }
                    else if (inputline.Contains("privmsg " + CHANNEL + " :!cod4lineup"))
                    {
                        write("PRIVMSG " + CHANNEL + " :CoD4 Lineup:");
                        write("PRIVMSG " + CHANNEL + " :VaguEh(scope), SCORCH + RobbiE_(Assualt), Fastje + Natzi(SMG), Snake+Stylez+eXtacy(Reserves).");
                    }
                    else if (inputline.Contains("privmsg " + CHANNEL + " :!fact"))
                    {
                        Random rFact = new Random();
                        int number = rFact.Next(0, 52);
                        write("PRIVMSG " + CHANNEL + " :Did you know? " + facts[number]);
                    }
                    else if(inputline.Contains("privmsg "+NICK +" :!setmatch"))//sets a new match.
                     {
                         orgMsg = inputline.Remove(0, Length + 10);
                         isAdmin = false;
                         write("WHOIS " + getNick());
                         inputline = reader.ReadLine();
                         while (!inputline.Contains("End of /WHOIS list."))
                         {
                             inputline = reader.ReadLine();
                             if (inputline.Contains("@" + CHANNEL) | inputline.Contains("~" + CHANNEL) | inputline.Contains("&" + CHANNEL))
                             {
                                 isAdmin = true;

                             }

                         }
                         if (isAdmin)
                         {
                             match = orgMsg;
                             write("privmsg " + CHANNEL + " :0,1Current match updated! Type !match to see.");
                        
                         }
                         else
                         {
                             write("NOTICE " + getNick() + " :PERMISSION DENIED.");
                         }

                     }
                    else if (inputline.Contains("privmsg " + CHANNEL + " :!match"))//displays the current match
                    {
                        string curMatch = match;
                        int num;
                        num = curMatch.IndexOf(" ");
                        string opponent = curMatch.Substring(0, num);
                        curMatch = curMatch.Remove(0, num+1);
                        num = curMatch.IndexOf(" ");
                        string server = curMatch.Substring(0, num);
                        curMatch = curMatch.Remove(0, num+1);
                        num = curMatch.IndexOf(" ");
                        string pass = curMatch.Substring(0, num);
                        curMatch = curMatch.Remove(0, num+1);
                        num = curMatch.IndexOf(" ");
                        string date = curMatch.Substring(0, num);
                        curMatch = curMatch.Remove(0, num+1);
                        //num = curMatch.IndexOf(" ");
                        string time = curMatch;

                         isAdmin = false;
                         write("WHOIS " + getNick());
                         inputline = reader.ReadLine();
                         while (!inputline.Contains("End of /WHOIS list."))
                         {
                             inputline = reader.ReadLine();
                             if (inputline.Contains("@" + CHANNEL) | inputline.Contains("~" + CHANNEL) | inputline.Contains("&" + CHANNEL))
                             {
                                 isAdmin = true;

                             }
                         }
                         if (isAdmin)
                         {
                             write("NOTICE " + getNick() + " :0,1Opponent:9" + opponent + "0  Server:9" + server + "0  Password:9" + pass + "0  Date:9" + date + " 0 Time:9" + time);
                         }
                         else
                         {
                             write("NOTICE " + getNick() + " :0,1Opponent:9" + opponent + "0  Server:9" + server + "0  Password:9 **** 0  Date:9" + date + " 0 Time:9" + time);
                         }
                        

                    }
                    


                    #endregion

                }
            //}
            //catch (Exception e)
            //{
             //   Console.WriteLine(e.ToString());
               // Thread.Sleep(5000);
               // string[] argv = { };
               // Main(argv);
           // }
        

        }
    }
}