Advertisement
Guest User

Untitled

a guest
Jul 31st, 2017
114
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 8.21 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.  
  26.         public Form1()
  27.         {
  28.             InitializeComponent();
  29.         }
  30.  
  31.         private void Form1_Load(object sender, EventArgs e)
  32.         {
  33.             //Seperate thread to keep GUI happy
  34.             ircThread = new Thread(new ThreadStart(delegate
  35.             {
  36.                 // Attach event handlers
  37.                 irc.OnConnected += new EventHandler(irc_OnConnected);                            //Hook the OnConnected event
  38.                 irc.OnChannelMessage += new EventHandler<IrcEventArgs>(irc_OnChannelMessage);    //Hook the OnChannelMessage event
  39.                 irc.OnDisconnected += new EventHandler(irc_OnDisconnected);                      //Hook the OnDisconnected Event
  40.                 irc.OnQueryMessage += new EventHandler<IrcEventArgs>(irc_OnQueryMessage);
  41.                 irc.OnCtcpReply +=new EventHandler<CtcpEventArgs>(irc_OnCtcpReply);
  42.  
  43.                 irc.AutoReconnect = false;
  44.                 irc.AutoRejoin = false;
  45.                 irc.AutoRetry = true;
  46.                 irc.AutoNickHandling = true;
  47.                 irc.AutoRejoinOnKick = true;
  48.                 irc.ActiveChannelSyncing = true;
  49.  
  50.                 // Attempt to connect to the IRC server
  51.                 try { irc.Connect(server, port); }
  52.                 catch (Exception ex) { Console.WriteLine("Unable to connect to IRC server: {0}", ex.Message); }
  53.             }));
  54.             ircThread.Start();
  55.  
  56.             this.Invoke((MethodInvoker)delegate { label1.Text = "Connecting"; });           //Updates the GUI objects, a little tricky since the IRC bot runs on a seperate thread
  57.         }
  58.  
  59.         void irc_OnQueryMessage(object sender, IrcEventArgs e)
  60.         {
  61.             string message = e.Data.Message;  //This is a local variable
  62.             if (message.StartsWith("!auth"))
  63.             {
  64.                 string password = message.Replace("!auth", "").Trim();      //Removes !auth from the message to get only the password
  65.                 if (password == authPassword)                       //If the password sent is equal to the authPassword global variable from above
  66.                 {
  67.                     IrcUser user = irc.GetIrcUser(e.Data.Nick);     //Have to find the user to get the users hostname
  68.                     authedHosts.Add(user.Host);                     //Adds the authorized hostname to the list
  69.                     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
  70.                 }
  71.             }
  72.         }
  73.  
  74.         void irc_OnDisconnected(object sender, EventArgs e)
  75.         {
  76.             this.Invoke((MethodInvoker)delegate { label1.Text = "Disconnected"; });
  77.         }
  78.  
  79.         void irc_OnChannelMessage(object sender, IrcEventArgs e)
  80.         {
  81.             IrcUser user = irc.GetIrcUser(e.Data.Nick);     //Find the user so we can get all details about them (like the hostname)
  82.  
  83.             if (e.Data.Message.StartsWith("!echo"))         //Example (!echo this is a test)
  84.             {
  85.                 string message = e.Data.Message;            //Create a string object and assign it the message from the user     (message = "!echo this is a test")
  86.                 message = message.Replace("!echo ", "");    //Replace the command part of the message with nothing.              (message = "this is a test")
  87.  
  88.                 irc.SendMessage(SendType.Message, channel, message);    //Send the message to the channel
  89.             }
  90.             else if (e.Data.Message.StartsWith("!me"))
  91.             {
  92.                 string message = e.Data.Message;
  93.                 message = message.Replace("!me ", "");
  94.                 irc.SendMessage(SendType.Action, channel, message);
  95.             }
  96.  
  97.             else if (e.Data.Message.StartsWith("!eat"))
  98.             {
  99.                 string message = e.Data.Message;
  100.                 message = message.Replace("!eat ", "eats " + "");
  101.                 irc.SendMessage(SendType.Action, channel, message);
  102.             }
  103.             else if (e.Data.Message == "lol" || e.Data.Message.Contains(" lol ") || e.Data.Message.Contains("lol ") || e.Data.Message.Contains(" lol"))
  104.             {
  105.                 irc.SendMessage(SendType.Message, channel, "What's so funny?");
  106.             }
  107.             else if (e.Data.Message.StartsWith("!commands"))
  108.             {
  109.                 irc.SendMessage(SendType.Message, channel, "Normal users: !eat, !me    Authed users: !op, !deop, !voice, !ban, !unban");
  110.             }
  111.             else if (e.Data.Message.StartsWith("!op"))
  112.             {
  113.                 if (authedHosts.Contains(user.Host))
  114.                 {
  115.                     string who = e.Data.Message.Replace("!op", "");
  116.                     irc.Op(channel, who);
  117.                 }
  118.                 else
  119.                 {
  120.                     irc.SendMessage(SendType.Message, channel, "You are not authorized!");
  121.                 }
  122.             }
  123.             else if (e.Data.Message.StartsWith("!deop"))
  124.             {
  125.                 if (authedHosts.Contains(user.Host))
  126.                 {
  127.                     string who = e.Data.Message.Replace("!deop", "");
  128.                     irc.Deop(channel, who);
  129.                 }
  130.             }
  131.             else if (e.Data.Message.StartsWith("!ban"))
  132.             {
  133.                 if (authedHosts.Contains(user.Host))
  134.                 {
  135.                     string who = e.Data.Message.Replace("!ban", "");
  136.                     irc.Ban(channel, who);
  137.                 }
  138.             }
  139.             else if (e.Data.Message.StartsWith("!unban"))
  140.             {
  141.                 if (authedHosts.Contains(user.Host))
  142.                 {
  143.                     string who = e.Data.Message.Replace("!unban", "");
  144.                     irc.Unban(channel, who);
  145.                 }
  146.             }
  147.             else if (e.Data.Message.StartsWith("!voice"))
  148.             {
  149.                 if (authedHosts.Contains(user.Host))
  150.                 {
  151.                     string who = e.Data.Message.Replace("!voice", "");
  152.                     irc.Voice(channel, who);
  153.                 }
  154.                 else if (e.Data.Message.StartsWith("!msg"))
  155.                 {
  156.                     if (e.Data.Message.Contains(user.Nick))
  157.                     {
  158.                     string msg = e.Data.Message.Replace("!msg", "");
  159.                     irc.SendMessage(SendType.Message, user.Nick, "From:" + user + msg);
  160.                     }
  161.                 }
  162.                 else if (e.Data.Message.StartsWith("!time"))
  163.                 {
  164.                     string person = e.Data.Message.Replace("!time ", "");
  165.                     irc.SendMessage(SendType.CtcpRequest, person, "TIME");
  166.                 }
  167.             }
  168.         }
  169.  
  170.         void irc_OnCtcpReply(object sender, CtcpEventArgs e);
  171.     {
  172.         if (e.Data.Message.Contains(""))
  173.         irc.SendMessage(SendType.Message, channel, message);
  174.     }
  175.  
  176.         void irc_OnConnected(object sender, EventArgs e)
  177.         {
  178.             irc.Login(nick, nick, 0, nick);     //Send the nick the bot is going to use
  179.             irc.SendMessage(SendType.Message, "NickServ", "IDENTIFY minecraft");
  180.             irc.SendMessage(SendType.Message, "Masher", "You are trusted with being authorized-type /msg MashBot !auth minecraft to auth");
  181.             irc.SendMessage(SendType.Message, "Tharm_Bood", "You are trusted with being authorized-type /msg MashBot !auth minecraft to auth");
  182.             irc.SendMessage(SendType.Message, "Shnaw", "You are trusted with being authorized-type /msg MashBot !auth minecraft to auth");
  183.             irc.RfcJoin(channel);               //Join the channel
  184.  
  185.             this.Invoke((MethodInvoker)delegate { label1.Text = "Connected"; });
  186.  
  187.             irc.Listen();                       //Listen
  188.         }
  189.     }
  190. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement