Advertisement
Guest User

Untitled

a guest
Mar 16th, 2017
99
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 11.46 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Net.Sockets;
  6. using System.IO;
  7.  
  8. namespace timebot1
  9. {
  10.     class Program
  11.     {
  12.         public static TcpClient socket;
  13.         public static StreamReader reader;
  14.         public static StreamWriter writer;
  15.  
  16.         static bool running = true;
  17.  
  18.         static DateTime dtLaunched = DateTime.Now;
  19.  
  20.         static string
  21.                 username = "munnbot",
  22.                 host = "irc.freenode.net",
  23.                 desc = "munnbot",
  24.                 password = "hunter2",
  25.                 admin = "ATMunn",
  26.                 command_char = "$";
  27.  
  28.         static void Main()
  29.         {
  30.             try
  31.             {
  32.                 socket = new TcpClient(host, 6667);
  33.                 socket.ReceiveBufferSize = 1024;
  34.                 Console.WriteLine("Connected");
  35.                 NetworkStream stream = socket.GetStream();
  36.                 reader = new StreamReader(stream);
  37.                 writer = new StreamWriter(stream);
  38.                 Send("USER " + username + " 8 * :" + desc);
  39.                 Send("NICK " + username);
  40.                 Send("PRIVMSG NickServ :identify " + password);
  41.                 Send("JOIN #atmunntest");
  42.                 ReadMessage(reader);
  43.                 reader.Close();
  44.                 writer.Close();
  45.                 stream.Close();
  46.             }
  47.             catch (Exception ex)
  48.             {
  49.                 Console.WriteLine("Error: " + ex.Message);
  50.             }
  51.         }
  52.  
  53.         //shorthand command for sending to a specific target
  54.         static void SendMessage(string target, string data)
  55.         {
  56.             Send("PRIVMSG " + target + " :" + data);
  57.         }
  58.  
  59.         //general "send to network" command
  60.         static void Send(string data)
  61.         {
  62.             try
  63.             {
  64.                 writer.WriteLine(data);
  65.                 Console.WriteLine(">>> " + data);
  66.                 writer.Flush();
  67.             }
  68.             catch (Exception ex)
  69.             {
  70.                 Console.WriteLine("Send error: " + ex.Message);
  71.             }
  72.         }
  73.  
  74.         static void ReadMessage(StreamReader reader)
  75.         {
  76.             try
  77.             {
  78.                 while (running)
  79.                 {
  80.                     string data = reader.ReadLine();
  81.                     Console.WriteLine(DateTime.Now.ToString("HH:mm:ss ") + data);
  82.                     string[] msgParts = data.Split(' ');
  83.  
  84.                     if (msgParts[0].ToLower() == "ping")
  85.                     { //ping request
  86.                         Send("PONG " + msgParts[1]);
  87.                     }
  88.                     else
  89.                     {
  90.                         string
  91.                                 user = msgParts[0].Split('!')[0].Substring(1),
  92.                                 cmd = msgParts[1].ToLower(),
  93.                                 target = msgParts[2];
  94.  
  95.                         switch (cmd)
  96.                         {
  97.                             case "join":
  98.                                 //user joined target
  99.                                 //if user == username, bot joined
  100.                                 break;
  101.                             case "part":
  102.                                 //user left target
  103.                                 //if user == username, bot left
  104.                                 break;
  105.                             case "nick":
  106.                                 //user changed their nick
  107.                                 break;
  108.                             case "quit":
  109.                                 //user quit
  110.                                 break;
  111.                             case "invite":
  112.                                 //bot was invited to target
  113.                                 break;
  114.                             case "notice":
  115.                                 string msg = data.Split(":".ToCharArray(), 3)[2];
  116.                                 if (msg.Contains("You are now identified for "))
  117.                                 {
  118.                                     //bot is identified
  119.                                     SendMessage(admin, "I am authenticated.");
  120.                                 }
  121.                                 break;
  122.                             case "privmsg":
  123.                                 //channel or private message  
  124.                                 if(target == "munnbot")
  125.                                 {
  126.                                     target = user;
  127.                                 }
  128.                                 ParseInput(user, target, data.Split(":".ToCharArray(), 3)[2]);
  129.                                 break;
  130.                         }
  131.                     }
  132.                 }
  133.             }
  134.             catch (Exception ex)
  135.             {
  136.                 Console.WriteLine("Error: " + ex.Message);
  137.                 System.Threading.Thread.Sleep(15000);
  138.                 ReadMessage(reader);
  139.             }
  140.         }
  141.  
  142.         static void ParseInput(string user, string target, string msg)
  143.         {
  144.             if (msg.StartsWith(command_char))
  145.             { //bot command
  146.                 string[] cmdParts = msg.Split(" ".ToCharArray(), 2);
  147.  
  148.                 switch (cmdParts[0].Substring(1))
  149.                 {
  150.                     case "uptime":
  151.                         TimeSpan ts = DateTime.Now - dtLaunched;
  152.                         SendMessage(target, string.Format("I have been running for {0}{1}{2}{3}",
  153.                                 ts.Days > 0 ? ts.Days.ToString() + "d " : "",
  154.                                 ts.Hours > 0 ? ts.Hours.ToString() + "h " : "",
  155.                                 ts.Minutes > 0 ? ts.Minutes.ToString() + "m " : "",
  156.                                 ts.Seconds.ToString() + "s"
  157.                                 ));
  158.                         break;
  159.                     case "hello":
  160.                         SendMessage(target, string.Format("Greetings, {0}!", user));
  161.                         break;
  162.                     case "test":
  163.                         SendMessage(target, string.Format("<{0}> $test", user));
  164.                         break;
  165.                     case "amiawesome":
  166.                         SendMessage(target, string.Format("Of course you are, {0}.", user));
  167.                         break;
  168.                     case "rolldie":
  169.  
  170.                         string[] argParts = cmdParts[1].Split(" ".ToCharArray(), 2);
  171.  
  172.                         if (argParts.Length < 2)
  173.                             SendMessage(target, "Not enough parameters.");
  174.                         else
  175.                         {
  176.                             Random r = new Random();
  177.                             int result = r.Next(int.Parse(argParts[0]), int.Parse(argParts[1]));
  178.                             SendMessage(target, string.Format("You rolled {0}.", result));
  179.                         }
  180.                         break;
  181.                     case "appendtofile":
  182.                         File.AppendAllText(@"C:\Users\Public\IrcBots\munnbot\textfilething.txt", " " + cmdParts[1]);
  183.                         SendMessage(target, string.Format("Appended \"{0}\" to the file.", cmdParts[1]));
  184.                         break;
  185.                     case "printfile":
  186.                         SendMessage(target, File.ReadAllText(@"C:\Users\Public\IrcBots\munnbot\textfilething.txt"));
  187.                         break;
  188.                     case "help":
  189.                         if (cmdParts.Length == 1)
  190.                         {
  191.                             switch(cmdParts[1])
  192.                             {
  193.                                 case "uptime":
  194.                                     SendMessage(target, "Sends the amount of time I have been running for.");
  195.                                     break;
  196.                                 case "hello":
  197.                                     SendMessage(target, "Greets the person who sends the command.");
  198.                                     break;
  199.                                 case "test":
  200.                                     SendMessage(target, "Just prints out whatever ATMunn decides to put there.");
  201.                                     break;
  202.                                 case "amiawesome":
  203.                                     SendMessage(target, "Tells you whether you are awesome or not.");
  204.                                     break;
  205.                                 case "rolldie":
  206.                                     SendMessage(target, "Syntax: $rolldie <min> <max>");
  207.                                     System.Threading.Thread.Sleep(500);
  208.                                     SendMessage(target, "Gives a random number between the <min> value and the <max> value.");
  209.                                     break;
  210.                                 case "appendtofile":
  211.                                     SendMessage(target, "Syntax: $appendtofile <text>");
  212.                                     System.Threading.Thread.Sleep(500);
  213.                                     SendMessage(target, "Appends whatever <text> is to the end of a file on ATMunn's computer.");
  214.                                     break;
  215.                                 case "printfile":
  216.                                     SendMessage(target, "Prints the current text in the file from appendtofile.");
  217.                                     break;
  218.                                 case "help":
  219.                                     SendMessage(target, "Gives a list of commands, or gives info on a specific command. (You're using it right now...)");
  220.                                     break;
  221.                                 case "botfight":
  222.                                     SendMessage(target, "Fights with timebot2.");
  223.                                     break;
  224.                             }
  225.                         }
  226.                         else
  227.                         {
  228.                             SendMessage(target, "Commands I respond to: uptime, hello, test, amiawesome, rolldie, appendtofile, printfile, help, botfight.");
  229.                             System.Threading.Thread.Sleep(500);
  230.                             SendMessage(target, "For more info on a specific command, type $help <commandname>");
  231.                         }
  232.                         break;
  233.                     case "botfight":
  234.                         SendMessage(target, "timebot2, you have so much code, you'd weigh 300 pounds if you were a physical bot!");
  235.                         break;
  236.                     case "quit":
  237.                         if (user == admin)
  238.                         {
  239.                             Send("QUIT");
  240.                             running = false;
  241.                         }
  242.                         break;
  243.                 }
  244.             }
  245.             else
  246.             { // any other message
  247.                 string[] cmdParts = msg.Split(" ".ToCharArray(), 2);
  248.                 switch (cmdParts[0].Substring(1))
  249.                 {
  250.                     case "HELP":
  251.                         if (cmdParts.Length == 1)
  252.                         {
  253.                             SendMessage(target, "Currently incomplete.");
  254.                         }
  255.                         else
  256.                         {
  257.                             SendMessage(target, "Commands I respond to: uptime, hello, test, amiawesome, rolldie, appendtofile, printfile");
  258.                             SendMessage(target, "For more info on a specific command, type /msg munnbot HELP <commandname>");
  259.                         }
  260.                         break;
  261.                 }
  262.             }
  263.         }
  264.     }
  265. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement