Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Net;
- using System.Net.Sockets;
- using System.IO;
- using System.Threading;
- using System.Linq;
- using System.Collections.Generic;
- class IrcBot
- {
- public static string SERVER = "snext.serveirc.com";
- private static int PORT = 6667;
- private static string USER = "USER predsbot 8 * :I'm a C# irc bot";
- private static string NICK = "Licos";
- private static string CHANNEL = "#preds";
- public static string AuthedNicks = "predsrule sniperX";
- private static string NickServPass = "jamie51822";
- //separate each nick with a space.
- public static StreamWriter writer;
- private static bool ispinging = false;
- //
- //
- //EDIT THIS v
- public static void SendMessage(bool pause, string Destination,string Message)
- {
- writer.WriteLine("PRIVMSG " + Destination + " :"+Message);
- writer.Flush();
- if (pause == true)
- {
- Thread.Sleep(1000);
- }
- }
- public static void SendRaw(string Data)
- {
- writer.WriteLine(Data);
- writer.Flush();
- Thread.Sleep(1000);
- }
- public static void irc_OnCommand(string message, string nick, string host, bool fromchannel)
- {
- #region Methods (dont need to modify this much)
- string Message = message;
- string Command = "";
- bool authenticated = false;
- string botnick = NICK.ToLower();
- if (Message.ToLower().StartsWith(botnick))
- {
- List<string> splitArray = new List<string>(Message.Split(new char[] { ' ' }));
- splitArray.RemoveAt(0);
- Command = splitArray[0];
- splitArray.RemoveAt(0);
- Message = String.Join(" ", splitArray.ToArray());
- }
- Command = Command.ToLower();
- string[] authuser = AuthedNicks.Split(new char[] { ' ' });
- foreach (string User in authuser)
- {
- if (User.ToLower() == nick.ToLower())
- {
- authenticated = true;
- }
- }
- #endregion
- if (Command == "about")
- {
- SendMessage(true, CHANNEL, "LicosBot V1.0, Wrote by Pasihavia Havia, Modified by predsrule.");
- }
- else if (Command == "shutdown")
- {
- if (authenticated == true)
- {
- SendMessage(true, nick, "Shutting Down...");
- Thread.Sleep(2000);
- Environment.Exit(0);
- }
- }
- if (Command == "fail")
- {
- SendMessage(true, CHANNEL, "i'm a failure");
- }
- if (Command == "restart")
- {
- if (authenticated == true)
- {
- SendMessage(true, nick, "Restarting...");
- SendRaw("QUIT :Restarting...");
- }
- }
- if (Command == "pray")
- {
- SendMessage(false, CHANNEL, "The Programmers Prayer");
- SendMessage(false, CHANNEL, "Our Program who are in Memory.");
- SendMessage(false, CHANNEL, "Hello by thy Name.");
- SendMessage(false, CHANNEL, "Thy Operating System come.");
- SendMessage(false, CHANNEL, "Thy Commands be done at the Printer as it is on the Screen.");
- SendMessage(false, CHANNEL, "Give us this day our daily Data,");
- SendMessage(false, CHANNEL, "And forgive us our I/O errors");
- SendMessage(false, CHANNEL, "As we forgive those whose logic circuits are faulty.");
- SendMessage(false, CHANNEL, "Lead us not into frustration, and deliver us from Power Surges.");
- SendMessage(false, CHANNEL, "For Thine is the Algorithm,");
- SendMessage(false, CHANNEL, "The Application,");
- SendMessage(false, CHANNEL, "And the Solution");
- SendMessage(false, CHANNEL, "Looping forever and ever.");
- SendMessage(false, CHANNEL, "Return");
- }
- if (Command == "say")
- {
- if (authenticated == true)
- {
- SendMessage(true, CHANNEL, Message);
- }
- }
- }
- //EDIT THIS ^
- //
- //
- public static void irc_OnChannelMessage(string message, string nick, string host)
- {
- irc_OnCommand(message, nick, host, true);
- }
- public static void irc_OnPrivMessage(string message, string nick, string host)
- {
- irc_OnCommand(message, nick, host, false);
- }
- static void Main(string[] args)
- {
- NetworkStream stream;
- TcpClient irc;
- string inputLine;
- StreamReader reader;
- try
- {
- if (ispinging == false)
- {
- PingSender ping = new PingSender();
- ping.Start();
- ispinging = true;
- }
- irc = new TcpClient(SERVER, PORT);
- stream = irc.GetStream();
- reader = new StreamReader(stream);
- writer = new StreamWriter(stream);
- writer.WriteLine(USER);
- writer.Flush();
- writer.WriteLine("NICK " + NICK);
- Thread.Sleep(2000);
- writer.Flush();
- writer.WriteLine("JOIN " + CHANNEL);
- writer.Flush();
- writer.WriteLine("PRIVMSG NickServ :identify "+NickServPass);
- writer.Flush();
- while (true)
- {
- while ((inputLine = reader.ReadLine()) != null)
- {
- //DONT ADD CRAP TO THIS. ADD IT TO irc_OnCommand INSTEAD
- Console.WriteLine(inputLine);
- if (inputLine.ToLower().Contains("privmsg "+CHANNEL.ToLower()))
- {
- string[] splitArray = inputLine.Split(new char[] { ':' });
- string[] splitArray2 = splitArray[1].Split(new char[] { '!' });
- string[] splitArray3 = splitArray2[1].Split(new char[] { ' ' });
- string compmsg = "";
- foreach (string msg in splitArray)
- {
- if (msg == "" || msg == splitArray[0] || msg == splitArray[1]) { }
- else if (compmsg == "") { compmsg = msg; }
- else { compmsg += ":" + msg; }
- }
- string nick = splitArray2[0];
- string host = splitArray3[0];
- string message = compmsg;
- irc_OnChannelMessage(message, nick, host);
- }
- else if (inputLine.Contains("PRIVMSG "+NICK))
- {
- string[] splitArray = inputLine.Split(new char[] { ':' });
- string[] splitArray2 = splitArray[1].Split(new char[] { '!' });
- string[] splitArray3 = splitArray2[1].Split(new char[] { ' ' });
- string compmsg = "";
- foreach (string msg in splitArray)
- {
- if (msg == "" || msg == splitArray[0] || msg == splitArray[1]) { }
- else if (compmsg == "") { compmsg = msg; }
- else { compmsg += ":" + msg; }
- }
- string nick = splitArray2[0];
- string host = splitArray3[0];
- string message = compmsg;
- irc_OnPrivMessage(message, nick, host);
- }
- }
- writer.Close();
- reader.Close();
- irc.Close();
- }
- }
- catch (Exception e)
- {
- Console.WriteLine(e.ToString());
- Thread.Sleep(5000);
- string[] argv = { };
- Main(argv);
- }
- }
- class PingSender
- {
- static string PING = "PING :";
- private Thread pingSender;
- // Empty constructor makes instance of Thread
- public PingSender()
- {
- pingSender = new Thread(new ThreadStart(this.Run));
- }
- // Starts the thread
- public void Start()
- {
- pingSender.Start();
- }
- // Send PING to irc server every 15 seconds
- public void Run()
- {
- while (true)
- {
- try
- {
- IrcBot.writer.WriteLine(PING + IrcBot.SERVER);
- IrcBot.writer.Flush();
- Thread.Sleep(15000);
- }
- catch { Console.WriteLine("Error Pinging Server."); }
- }
- }
- }
- }
Add Comment
Please, Sign In to add comment