Advertisement
Guest User

Untitled

a guest
Aug 6th, 2017
91
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 12.71 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.ComponentModel;
  4. using System.Data;
  5. using System.Drawing;
  6. using System.Linq;
  7. using System.Text;
  8. using System.Windows.Forms;
  9. using Meebey.SmartIrc4net;
  10. using System.Threading;
  11.  
  12. namespace IrcBot
  13. {
  14.     public partial class Form1 : Form
  15.     {
  16.         Thread ircThread;
  17.         IrcClient irc = new IrcClient();
  18.  
  19.         string nick = "MashBot";
  20.         string server = "irc.tchalo.net";
  21.         int port = 6667;
  22.         string channel = "#Game";
  23.         string authPassword = "minecraft";
  24.         List<string> authedHosts = new List<string>();  //These are all global variables
  25.         string chan = "#Tchalo";
  26.         List<string> botStatus = new List<string>();
  27.  
  28.         public Form1()
  29.         {
  30.             InitializeComponent();
  31.         }
  32.  
  33.         private void Form1_Load(object sender, EventArgs e)
  34.         {
  35.             //Seperate thread to keep GUI happy
  36.             ircThread = new Thread(new ThreadStart(delegate
  37.             {
  38.                 // Attach event handlers
  39.                 irc.OnConnected += new EventHandler(irc_OnConnected);                            //Hook the OnConnected event
  40.                 irc.OnChannelMessage += new EventHandler<IrcEventArgs>(irc_OnChannelMessage);    //Hook the OnChannelMessage event
  41.                 irc.OnDisconnected += new EventHandler(irc_OnDisconnected);                      //Hook the OnDisconnected Event
  42.                 irc.OnQueryMessage += new EventHandler<IrcEventArgs>(irc_OnQueryMessage);
  43.                 irc.OnCtcpReply +=new EventHandler<CtcpEventArgs>(irc_OnCtcpReply);
  44.  
  45.                 irc.AutoReconnect = false;
  46.                 irc.AutoRejoin = false;
  47.                 irc.AutoRetry = true;
  48.                 irc.AutoNickHandling = true;
  49.                 irc.AutoRejoinOnKick = true;
  50.                 irc.ActiveChannelSyncing = true;
  51.  
  52.                 // Attempt to connect to the IRC server
  53.                 try { irc.Connect(server, port); }
  54.                 catch (Exception ex) { Console.WriteLine("Unable to connect to IRC server: {0}", ex.Message); }
  55.             }));
  56.             ircThread.Start();
  57.  
  58.             this.Invoke((MethodInvoker)delegate { label1.Text = "Connecting"; });           //Updates the GUI objects, a little tricky since the IRC bot runs on a seperate thread
  59.         }
  60.  
  61.         void irc_OnQueryMessage(object sender, IrcEventArgs e)
  62.         {
  63.             string message = e.Data.Message;  //This is a local variable
  64.             if (message.StartsWith("!auth"))
  65.             {
  66.                 string password = message.Replace("!auth", "").Trim();      //Removes !auth from the message to get only the password
  67.                 if (password == authPassword)                       //If the password sent is equal to the authPassword global variable from above
  68.                 {
  69.                     IrcUser user = irc.GetIrcUser(e.Data.Nick);     //Have to find the user to get the users hostname
  70.                     authedHosts.Add(user.Host);                     //Adds the authorized hostname to the list
  71.                     irc.SendMessage(SendType.Message, user.Nick, "You have been authorized on hostname: " + user.Host);     //Tell the user that they have been authorized and display the hostname to them
  72.                 }
  73.             }
  74.         }
  75.  
  76.         void irc_OnDisconnected(object sender, EventArgs e)
  77.         {
  78.             this.Invoke((MethodInvoker)delegate { label1.Text = "Disconnected"; });
  79.         }
  80.  
  81.         void irc_OnChannelMessage(object sender, IrcEventArgs e)
  82.         {
  83.             IrcUser user = irc.GetIrcUser(e.Data.Nick);     //Find the user so we can get all details about them (like the hostname)
  84.             if (botStatus.Contains("sleep")) { return; }
  85.             {
  86.                 if (botStatus.Contains("wake"))
  87.                 {
  88.                     if (e.Data.Message.StartsWith("!sleep"))
  89.                     {
  90.                         botStatus.Add("sleep");
  91.                     }
  92.                     else if (e.Data.Message.StartsWith("!wake"))
  93.                     {
  94.                         botStatus.Remove("sleep");
  95.                         botStatus.Add("wake");
  96.                     }
  97.                     else if (e.Data.Message.StartsWith("!echo"))         //Example (!echo this is a test)
  98.                     {
  99.                         string message = e.Data.Message;            //Create a string object and assign it the message from the user     (message = "!echo this is a test")
  100.                         message = message.Replace("!echo ", "");    //Replace the command part of the message with nothing.              (message = "this is a test")
  101.                         irc.SendMessage(SendType.Message, e.Data.Channel, message);   //Send the message to the channel + chan
  102.                     }
  103.                     else if (e.Data.Message.StartsWith("!do"))
  104.                     {
  105.                         string message = e.Data.Message;
  106.                         message = message.Replace("!do ", "");
  107.                         irc.SendMessage(SendType.Action, e.Data.Channel, message);
  108.                     }
  109.  
  110.                     else if (e.Data.Message.StartsWith("!eat"))
  111.                     {
  112.                         string message = e.Data.Message;
  113.                         message = message.Replace("!eat ", "eats " + "");
  114.                         irc.SendMessage(SendType.Action, e.Data.Channel, message);
  115.                     }
  116.                     else if (e.Data.Message == "lol" || e.Data.Message.Contains(" lol ") || e.Data.Message.Contains("lol ") || e.Data.Message.Contains(" lol"))
  117.                     {
  118.                         irc.SendMessage(SendType.Message, e.Data.Channel, "What's so funny?");
  119.                     }
  120.                     else if (e.Data.Message.StartsWith("!commands"))
  121.                     {
  122.                         irc.SendMessage(SendType.Message, e.Data.Channel, "Normal users: !do, !me, !time, !msg    Authed users: !op, !deop, !voice, !ban, !unban, !join");
  123.                     }
  124.                     else if (e.Data.Message.StartsWith("!op"))
  125.                     {
  126.                         if (authedHosts.Contains(user.Host))
  127.                         {
  128.                             string who = e.Data.Message.Replace("!op", "");
  129.                             irc.Op(e.Data.Channel, who);
  130.                         }
  131.                         else
  132.                         {
  133.                             irc.SendMessage(SendType.Message, e.Data.Channel, "You're not authorized!");
  134.                         }
  135.                     }
  136.                     else if (e.Data.Message.StartsWith("!deop"))
  137.                     {
  138.                         if (authedHosts.Contains(user.Host))
  139.                         {
  140.                             string who = e.Data.Message.Replace("!deop", "");
  141.                             irc.Deop(e.Data.Channel, who);
  142.                         }
  143.                     }
  144.                     else if (e.Data.Message.StartsWith("!ban"))
  145.                     {
  146.                         if (authedHosts.Contains(user.Host))
  147.                         {
  148.                             string who = e.Data.Message.Replace("!ban", "");
  149.                             irc.Ban(e.Data.Channel, who);
  150.                         }
  151.                     }
  152.                     else if (e.Data.Message.StartsWith("!unban"))
  153.                     {
  154.                         if (authedHosts.Contains(user.Host))
  155.                         {
  156.                             string who = e.Data.Message.Replace("!unban", "");
  157.                             irc.Unban(e.Data.Channel, who);
  158.                         }
  159.                     }
  160.                     else if (e.Data.Message.StartsWith("!voice"))
  161.                     {
  162.                         if (authedHosts.Contains(user.Host))
  163.                         {
  164.                             string who = e.Data.Message.Replace("!voice", "");
  165.                             irc.Voice(e.Data.Channel, who);
  166.                         }
  167.                     }
  168.                     else if (e.Data.Message.StartsWith("!msg"))
  169.                     {
  170.                         string[] msgArray = { "!msg", user.Nick };
  171.                         string recipient = e.Data.MessageArray[1];
  172.                         string msg = e.Data.Message.Replace("!msg " + recipient, "");
  173.                         irc.SendMessage(SendType.Message, recipient, "From: " + user.Nick + ": " + msg);
  174.                     }
  175.                     else if (e.Data.Message.StartsWith("!time"))
  176.                     {
  177.                         if (e.Data.Message.Contains(user.Nick))
  178.                         {
  179.                             string person = e.Data.Message.Replace("!time ", "");
  180.                             irc.SendMessage(SendType.CtcpRequest, person, "TIME");
  181.                         }
  182.                         else
  183.                         {
  184.                             irc.SendMessage(SendType.CtcpRequest, user.Nick, "TIME");
  185.                         }
  186.                     }
  187.                     else if (e.Data.Message.Contains("fuck") || e.Data.Message.Contains("shit") || e.Data.Message.Contains("cunt") || e.Data.Message.Contains("fucking"))
  188.                     {
  189.                         irc.RfcKick(e.Data.Channel, user.Nick);
  190.                         irc.SendMessage(SendType.Message, e.Data.Message, "Language!");
  191.                     }
  192.                     else if (e.Data.Message.StartsWith("!admin"))
  193.                     {
  194.                         if (authedHosts.Contains(user.Host))
  195.                         {
  196.                             if (e.Data.Message.Contains("minecraft"))
  197.                             {
  198.                                 if (e.Data.Message.Contains("add"))
  199.                                 {
  200.                                     string[] adminArray = { "!admin ", user.Nick, " add" };
  201.                                     string admin = adminArray[1];
  202.                                     irc.SendMessage(SendType.Message, "ChanServ", "SOP " + e.Data.Channel + "add" + admin);
  203.                                 }
  204.                                 if (e.Data.Message.Contains("del"))
  205.                                 {
  206.                                     string[] adminArray = { "!admin ", user.Nick, " del" };
  207.                                     string admin = adminArray[1];
  208.                                     irc.SendMessage(SendType.Message, "ChanServ", "SOP " + e.Data.Channel + " DEL " + admin);
  209.                                 }
  210.                             }
  211.                         }
  212.                     }
  213.                     else if (e.Data.Message.StartsWith("!kick"))
  214.                     {
  215.                         if (authedHosts.Contains(user.Host))
  216.                         {
  217.                             string kicked = e.Data.Message.Replace("!kick", "").Trim();
  218.                             irc.RfcKick(e.Data.Channel, kicked, "Kicked by: " + user.Nick);
  219.                         }
  220.                     }
  221.                     else if (e.Data.Message.StartsWith("!join"))
  222.                     {
  223.                         if (authedHosts.Contains(user.Host))
  224.                         {
  225.                             string chann = e.Data.Message.Replace("!join ", "");
  226.                             irc.RfcJoin(chann);
  227.                         }
  228.                     }
  229.                     else if (e.Data.Message.StartsWith("!invite"))
  230.                     {
  231.                         if (authedHosts.Contains(user.Host))
  232.                         {
  233.                             string invite = e.Data.Message.Replace("!invite ", "");
  234.                             irc.RfcInvite(invite, e.Data.Channel);
  235.                         }
  236.                     }
  237.                 }
  238.             }
  239.         }
  240.  
  241.  
  242.         void irc_OnCtcpReply(object sender, CtcpEventArgs e)
  243.     {
  244.         irc.SendMessage(SendType.Message, e.Data.Channel, e.CtcpParameter);
  245.     }
  246.  
  247.         void irc_OnConnected(object sender, EventArgs e)
  248.         {
  249.             irc.Login(nick, nick, 0, nick);     //Send the nick the bot is going to use
  250.             irc.SendMessage(SendType.Message, "NickServ", "IDENTIFY minecraft");
  251.             irc.SendMessage(SendType.Message, "Masher", "You are trusted with being authorized-type /msg MashBot !auth minecraft to auth");
  252.             irc.SendMessage(SendType.Message, "Tharm_Bood", "You are trusted with being authorized-type /msg MashBot !auth minecraft to auth");
  253.             irc.SendMessage(SendType.Message, "Shnaw", "You are trusted with being authorized-type /msg MashBot !auth minecraft to auth");
  254.             irc.RfcJoin("#ninjas");
  255.             irc.RfcJoin("#game");
  256.            
  257.  
  258.             this.Invoke((MethodInvoker)delegate { label1.Text = "Connected"; });
  259.  
  260.             irc.Listen();                       //Listen
  261.             }
  262.  
  263.         }
  264.     }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement