Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections.Generic;
- using System.ComponentModel;
- using System.Data;
- using System.Drawing;
- using System.Linq;
- using System.Text;
- using System.Windows.Forms;
- using Meebey.SmartIrc4net;
- using System.Threading;
- using System.IO;
- namespace IrcBot
- {
- public partial class Form1 : Form
- {
- Thread ircThread;
- IrcClient irc = new IrcClient();
- string nick = "MashBot";
- string server = "irc.tchalo.net";
- int port = 6667;
- string channel = "#game";
- string authPassword = "minecraft";
- List<string> authedHosts = new List<string>(); //These are all global variables
- public Form1()
- {
- InitializeComponent();
- }
- private void Form1_Load(object sender, EventArgs e)
- {
- //Seperate thread to keep GUI happy
- ircThread = new Thread(new ThreadStart(delegate
- {
- // Attach event handlers
- irc.OnConnected += new EventHandler(irc_OnConnected); //Hook the OnConnected event
- irc.OnChannelMessage += new EventHandler<IrcEventArgs>(irc_OnChannelMessage); //Hook the OnChannelMessage event
- irc.OnDisconnected += new EventHandler(irc_OnDisconnected); //Hook the OnDisconnected Event
- irc.OnQueryMessage += new EventHandler<IrcEventArgs>(irc_OnQueryMessage);
- irc.AutoReconnect = false;
- irc.AutoRejoin = false;
- irc.AutoRetry = true;
- irc.AutoNickHandling = true;
- irc.AutoRejoinOnKick = true;
- irc.ActiveChannelSyncing = true;
- // Attempt to connect to the IRC server
- try { irc.Connect(server, port); }
- catch (Exception ex) { Console.WriteLine("Unable to connect to IRC server: {0}", ex.Message); }
- }));
- ircThread.Start();
- this.Invoke((MethodInvoker)delegate { label1.Text = "Connecting"; }); //Updates the GUI objects, a little tricky since the IRC bot runs on a seperate thread
- }
- void irc_OnQueryMessage(object sender, IrcEventArgs e)
- {
- string message = e.Data.Message; //This is a local variable
- if (message.StartsWith("!auth"))
- {
- string password = message.Replace("!auth", "").Trim(); //Removes !auth from the message to get only the password
- if (password == authPassword) //If the password sent is equal to the authPassword global variable from above
- {
- IrcUser user = irc.GetIrcUser(e.Data.Nick); //Have to find the user to get the users hostname
- authedHosts.Add(user.Host); //Adds the authorized hostname to the list
- 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
- irc.Op(channel, user.Nick);
- }
- }
- }
- void irc_OnDisconnected(object sender, EventArgs e)
- {
- this.Invoke((MethodInvoker)delegate { label1.Text = "Disconnected"; });
- }
- void irc_OnChannelMessage(object sender, IrcEventArgs e)
- {
- IrcUser user = irc.GetIrcUser(e.Data.Nick); //Find the user so we can get all details about them (like the hostname)
- if (e.Data.Message.StartsWith("!echo")) //Example (!echo this is a test)
- {
- string message = e.Data.Message; //Create a string object and assign it the message from the user (message = "!echo this is a test")
- message = message.Replace("!echo ", ""); //Replace the command part of the message with nothing. (message = "this is a test")
- irc.SendMessage(SendType.Message, channel, message); //Send the message to the channel
- }
- else if (e.Data.Message.StartsWith("!me"))
- {
- string message = e.Data.Message;
- message = message.Replace("!me ", "");
- irc.SendMessage(SendType.Action, channel, message);
- }
- else if (e.Data.Message.StartsWith("!eat"))
- {
- string message = e.Data.Message;
- message = message.Replace("!eat ", "eats " + "");
- irc.SendMessage(SendType.Action, channel, message);
- }
- else if (e.Data.Message.StartsWith("!time"))
- {
- string message = e.Data.Message;
- message = message.Replace("!time ", "");
- irc.SendMessage(SendType.CtcpRequest, message, "TIME");
- }
- else if (e.Data.Message == "lol" || e.Data.Message.Contains(" lol ") || e.Data.Message.Contains("lol ") || e.Data.Message.Contains(" lol"))
- {
- irc.SendMessage(SendType.Message, channel, "What's so funny?");
- }
- else if (e.Data.Message.StartsWith("!op"))
- {
- if (authedHosts.Contains(user.Host))
- {
- string who = e.Data.Message.Replace("!op", "");
- irc.Op(channel, who);
- }
- else
- {
- irc.SendMessage(SendType.Message, channel, "You are not authorized!");
- }
- }
- else if (e.Data.Message.StartsWith("!deop"))
- {
- if (authedHosts.Contains(user.Host))
- {
- string who = e.Data.Message.Replace("!deop", "");
- irc.Deop(channel, who);
- }
- }
- else if (e.Data.Message.StartsWith("!ban"))
- {
- if (authedHosts.Contains("host86-161-160-126.range86-161.btcentralplus.com"))
- {
- string who = e.Data.Message.Replace("!ban", "");
- irc.Ban(channel, who);
- }
- }
- else if (e.Data.Message.StartsWith("!unban"))
- {
- if (authedHosts.Contains("host86-161-160-126.range86-161.btcentralplus.com"))
- {
- string who = e.Data.Message.Replace("!unban ", "");
- irc.Unban(channel, who);
- }
- }
- else if (e.Data.Message.StartsWith("!voice"))
- {
- if (authedHosts.Contains(user.Host))
- {
- string who = e.Data.Message.Replace("!voice ", "");
- irc.Voice(channel, who);
- }
- }
- else if (e.Data.Message.StartsWith("!kill"))
- {
- if (e.Data.Message.EndsWith("Tharm_Bood"))
- {
- irc.SendMessage(SendType.Action, channel, "kills Tharm_Bood");
- }
- else
- {
- irc.SendMessage(SendType.Action, channel, "is a peaceful bot!");
- }
- }
- else if (authedHosts.Contains(user.Host))
- {
- irc.Op("#game", user.Nick);
- }
- else if (e.Data.Message.StartsWith("!poll"))
- {
- if (e.Data.Message.StartsWith("!poll start"))
- {
- string poll = e.Data.Message.Replace("!poll start ", "");
- irc.SendMessage(SendType.Message, channel, user.Nick + " has started the poll; " + poll);
- irc.SendMessage(SendType.Message, channel, "type !poll 1 to vote yes, type !poll 2 to vote no");
- }
- if (e.Data.Message == "!poll 1")
- {
- try
- {
- //Pass the filepath and filename to the StreamWriter Constructor
- StreamWriter sw = new StreamWriter("C:\\users\\matthew\\downloads\\IrcBot\\IrcBot\\IrcBot\\Yes.txt");
- //Write a line of text
- sw.WriteLine("1+");
- //Close the file
- sw.Close();
- }
- finally
- {
- irc.SendMessage(SendType.Message, channel, user.Nick + " has voted yes.");
- }
- }
- if (e.Data.Message == "!poll 2")
- {
- try
- {
- //Pass the filepath and filename to the StreamWriter Constructor
- StreamWriter sw = new StreamWriter("C:\\users\\matthew\\downloads\\IrcBot\\IrcBot\\IrcBot\\no.txt");
- //Write a line of text
- sw.WriteLine("1+");
- //Close the file
- sw.Close();
- }
- finally
- {
- irc.SendMessage(SendType.Message, channel, user.Nick + " has voted no.");
- }
- }
- if (e.Data.Message == "!poll end")
- {
- try
- {
- //Pass the file path and file name to the StreamReader constructor
- StreamReader sr = new StreamReader("C:\\users\\matthew\\downloads\\IrcBot\\IrcBot\\IrcBot\\no.txt");
- //Read the first line of text
- string resultsNo = sr.ReadLine();
- //Continue to read until you reach end of file
- while (resultsNo != null)
- {
- irc.SendMessage(SendType.Message, channel, "No had " + resultsNo + " votes");
- }
- //close the file
- sr.Close();
- }
- finally
- {
- //Pass the file path and file name to the StreamReader constructor
- StreamReader sr = new StreamReader("C:\\users\\matthew\\downloads\\IrcBot\\IrcBot\\IrcBot\\yes.txt");
- //Read the first line of text
- string resultsYes = sr.ReadLine();
- //Continue to read until you reach end of file
- while (resultsYes != null)
- {
- irc.SendMessage(SendType.Message, channel, "Yes had " + resultsYes + " votes");
- }
- //close the file
- sr.Close();
- }
- }
- }
- }
- public delegate void UpdateStat();
- void irc_OnConnected(object sender, EventArgs e)
- {
- irc.Login(nick, nick, 0, nick); //Send the nick the bot is going to use
- irc.SendMessage(SendType.Message, "NickServ", "IDENTIFY minecraft");
- irc.RfcJoin(channel); //Join the channel
- authedHosts.Add("host86-161-160-126.range86-161.btcentralplus.com");
- authedHosts.Add("Tharm_Bood@boomingthunder.x10.mx");
- authedHosts.Add("bman@netadmin.tchalo.net");
- authedHosts.Add("quassel@ApertureScience.com");
- this.Invoke((MethodInvoker)delegate { label1.Text = "Connected"; });
- irc.Listen(); //Listen
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement