Advertisement
Guest User

Untitled

a guest
Sep 27th, 2017
426
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 11.65 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. using System.IO;
  12.  
  13. namespace IrcBot
  14. {
  15.     public partial class Form1 : Form
  16.     {
  17.         Thread ircThread;
  18.         IrcClient irc = new IrcClient();
  19.  
  20.         string nick = "MashBot";
  21.         string server = "irc.tchalo.net";
  22.         int port = 6667;
  23.         string channel = "#game";
  24.         string authPassword = "minecraft";
  25.         List<string> authedHosts = new List<string>();      //These are all global variables
  26.  
  27.         public Form1()
  28.         {
  29.             InitializeComponent();
  30.         }
  31.  
  32.         private void Form1_Load(object sender, EventArgs e)
  33.         {
  34.             //Seperate thread to keep GUI happy
  35.             ircThread = new Thread(new ThreadStart(delegate
  36.             {
  37.                 // Attach event handlers
  38.                 irc.OnConnected += new EventHandler(irc_OnConnected);                            //Hook the OnConnected event
  39.                 irc.OnChannelMessage += new EventHandler<IrcEventArgs>(irc_OnChannelMessage);    //Hook the OnChannelMessage event
  40.                 irc.OnDisconnected += new EventHandler(irc_OnDisconnected);                      //Hook the OnDisconnected Event
  41.                 irc.OnQueryMessage += new EventHandler<IrcEventArgs>(irc_OnQueryMessage);
  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.                     irc.Op(channel, user.Nick);
  71.                 }
  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.  
  85.             if (e.Data.Message.StartsWith("!echo"))         //Example (!echo this is a test)
  86.             {
  87.                 string message = e.Data.Message;            //Create a string object and assign it the message from the user     (message = "!echo this is a test")
  88.                 message = message.Replace("!echo ", "");    //Replace the command part of the message with nothing.              (message = "this is a test")
  89.  
  90.                 irc.SendMessage(SendType.Message, channel, message);    //Send the message to the channel
  91.             }
  92.             else if (e.Data.Message.StartsWith("!me"))
  93.             {
  94.                 string message = e.Data.Message;
  95.                 message = message.Replace("!me ", "");
  96.                 irc.SendMessage(SendType.Action, channel, message);
  97.             }
  98.  
  99.             else if (e.Data.Message.StartsWith("!eat"))
  100.             {
  101.                 string message = e.Data.Message;
  102.                 message = message.Replace("!eat ", "eats " + "");
  103.                 irc.SendMessage(SendType.Action, channel, message);
  104.             }
  105.             else if (e.Data.Message.StartsWith("!time"))
  106.             {
  107.                 string message = e.Data.Message;
  108.                 message = message.Replace("!time ", "");
  109.                 irc.SendMessage(SendType.CtcpRequest, message, "TIME");
  110.             }
  111.             else if (e.Data.Message == "lol" || e.Data.Message.Contains(" lol ") || e.Data.Message.Contains("lol ") || e.Data.Message.Contains(" lol"))
  112.             {
  113.                 irc.SendMessage(SendType.Message, channel, "What's so funny?");
  114.             }
  115.             else if (e.Data.Message.StartsWith("!op"))
  116.             {
  117.                 if (authedHosts.Contains(user.Host))
  118.                 {
  119.                     string who = e.Data.Message.Replace("!op", "");
  120.                     irc.Op(channel, who);
  121.                 }
  122.                 else
  123.                 {
  124.                     irc.SendMessage(SendType.Message, channel, "You are not authorized!");
  125.                 }
  126.             }
  127.             else if (e.Data.Message.StartsWith("!deop"))
  128.             {
  129.                 if (authedHosts.Contains(user.Host))
  130.                 {
  131.                     string who = e.Data.Message.Replace("!deop", "");
  132.                     irc.Deop(channel, who);
  133.                 }
  134.             }
  135.             else if (e.Data.Message.StartsWith("!ban"))
  136.             {
  137.                 if (authedHosts.Contains("host86-161-160-126.range86-161.btcentralplus.com"))
  138.                 {
  139.                     string who = e.Data.Message.Replace("!ban", "");
  140.                     irc.Ban(channel, who);
  141.                 }
  142.             }
  143.             else if (e.Data.Message.StartsWith("!unban"))
  144.             {
  145.                 if (authedHosts.Contains("host86-161-160-126.range86-161.btcentralplus.com"))
  146.                 {
  147.                     string who = e.Data.Message.Replace("!unban ", "");
  148.                     irc.Unban(channel, who);
  149.                 }
  150.             }
  151.             else if (e.Data.Message.StartsWith("!voice"))
  152.             {
  153.                 if (authedHosts.Contains(user.Host))
  154.                 {
  155.                     string who = e.Data.Message.Replace("!voice ", "");
  156.                     irc.Voice(channel, who);
  157.                 }
  158.             }
  159.             else if (e.Data.Message.StartsWith("!kill"))
  160.             {
  161.                 if (e.Data.Message.EndsWith("Tharm_Bood"))
  162.                 {
  163.                     irc.SendMessage(SendType.Action, channel, "kills Tharm_Bood");
  164.                 }
  165.                 else
  166.                 {
  167.                     irc.SendMessage(SendType.Action, channel, "is a peaceful bot!");
  168.                 }
  169.             }
  170.             else if (authedHosts.Contains(user.Host))
  171.             {
  172.                 irc.Op("#game", user.Nick);
  173.             }
  174.             else if (e.Data.Message.StartsWith("!poll"))
  175.             {
  176.                 if (e.Data.Message.StartsWith("!poll start"))
  177.                 {
  178.                     string poll = e.Data.Message.Replace("!poll start ", "");
  179.                     irc.SendMessage(SendType.Message, channel, user.Nick + " has started the poll; " + poll);
  180.                     irc.SendMessage(SendType.Message, channel, "type !poll 1 to vote yes, type !poll 2 to vote no");
  181.                 }
  182.                 if (e.Data.Message == "!poll 1")
  183.                 {
  184.                     try
  185.                     {
  186.  
  187.                         //Pass the filepath and filename to the StreamWriter Constructor
  188.                         StreamWriter sw = new StreamWriter("C:\\users\\matthew\\downloads\\IrcBot\\IrcBot\\IrcBot\\Yes.txt");
  189.  
  190.                         //Write a line of text
  191.                         sw.WriteLine("1+");
  192.  
  193.                         //Close the file
  194.                         sw.Close();
  195.                     }
  196.  
  197.                     finally
  198.                     {
  199.                         irc.SendMessage(SendType.Message, channel, user.Nick + " has voted yes.");
  200.                     }
  201.                 }
  202.                 if (e.Data.Message == "!poll 2")
  203.                 {
  204.                     try
  205.                     {
  206.  
  207.                         //Pass the filepath and filename to the StreamWriter Constructor
  208.                         StreamWriter sw = new StreamWriter("C:\\users\\matthew\\downloads\\IrcBot\\IrcBot\\IrcBot\\no.txt");
  209.  
  210.                         //Write a line of text
  211.                         sw.WriteLine("1+");
  212.  
  213.                         //Close the file
  214.                         sw.Close();
  215.                     }
  216.  
  217.                     finally
  218.                     {
  219.                         irc.SendMessage(SendType.Message, channel, user.Nick + " has voted no.");
  220.                     }
  221.                 }
  222.                 if (e.Data.Message == "!poll end")
  223.                 {
  224.                     try
  225.                     {
  226.                         //Pass the file path and file name to the StreamReader constructor
  227.                         StreamReader sr = new StreamReader("C:\\users\\matthew\\downloads\\IrcBot\\IrcBot\\IrcBot\\no.txt");
  228.  
  229.                         //Read the first line of text
  230.                         string resultsNo = sr.ReadLine();
  231.  
  232.                         //Continue to read until you reach end of file
  233.                         while (resultsNo != null)
  234.                         {
  235.                             irc.SendMessage(SendType.Message, channel, "No had " + resultsNo + " votes");
  236.                         }
  237.  
  238.                         //close the file
  239.                         sr.Close();
  240.                     }
  241.                     finally
  242.                     {
  243.                         //Pass the file path and file name to the StreamReader constructor
  244.                         StreamReader sr = new StreamReader("C:\\users\\matthew\\downloads\\IrcBot\\IrcBot\\IrcBot\\yes.txt");
  245.  
  246.                         //Read the first line of text
  247.                         string resultsYes = sr.ReadLine();
  248.  
  249.                         //Continue to read until you reach end of file
  250.                         while (resultsYes != null)
  251.                         {
  252.                             irc.SendMessage(SendType.Message, channel, "Yes had " + resultsYes + " votes");
  253.                         }
  254.  
  255.                         //close the file
  256.                         sr.Close();
  257.                     }
  258.                 }
  259.             }
  260.         }
  261.  
  262.         public delegate void UpdateStat();
  263.         void irc_OnConnected(object sender, EventArgs e)
  264.         {
  265.             irc.Login(nick, nick, 0, nick);     //Send the nick the bot is going to use
  266.             irc.SendMessage(SendType.Message, "NickServ", "IDENTIFY minecraft");
  267.             irc.RfcJoin(channel);               //Join the channel
  268.             authedHosts.Add("host86-161-160-126.range86-161.btcentralplus.com");
  269.             authedHosts.Add("Tharm_Bood@boomingthunder.x10.mx");
  270.             authedHosts.Add("bman@netadmin.tchalo.net");
  271.             authedHosts.Add("quassel@ApertureScience.com");
  272.  
  273.             this.Invoke((MethodInvoker)delegate { label1.Text = "Connected"; });
  274.  
  275.             irc.Listen();                       //Listen
  276.         }
  277.     }
  278. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement