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;
- 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
- string chan = "#Tchalo";
- List<string> botStatus = new List<string>();
- 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.OnCtcpReply +=new EventHandler<CtcpEventArgs>(irc_OnCtcpReply);
- 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
- }
- }
- }
- 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 (botStatus.Contains("sleep")) { return; }
- {
- if (botStatus.Contains("wake"))
- {
- if (e.Data.Message.StartsWith("!sleep"))
- {
- botStatus.Add("sleep");
- }
- else if (e.Data.Message.StartsWith("!wake"))
- {
- botStatus.Remove("sleep");
- botStatus.Add("wake");
- }
- else 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, e.Data.Channel, message); //Send the message to the channel + chan
- }
- else if (e.Data.Message.StartsWith("!do"))
- {
- string message = e.Data.Message;
- message = message.Replace("!do ", "");
- irc.SendMessage(SendType.Action, e.Data.Channel, message);
- }
- else if (e.Data.Message.StartsWith("!eat"))
- {
- string message = e.Data.Message;
- message = message.Replace("!eat ", "eats " + "");
- irc.SendMessage(SendType.Action, e.Data.Channel, message);
- }
- 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, e.Data.Channel, "What's so funny?");
- }
- else if (e.Data.Message.StartsWith("!commands"))
- {
- irc.SendMessage(SendType.Message, e.Data.Channel, "Normal users: !do, !me, !time, !msg Authed users: !op, !deop, !voice, !ban, !unban, !join");
- }
- else if (e.Data.Message.StartsWith("!op"))
- {
- if (authedHosts.Contains(user.Host))
- {
- string who = e.Data.Message.Replace("!op", "");
- irc.Op(e.Data.Channel, who);
- }
- else
- {
- irc.SendMessage(SendType.Message, e.Data.Channel, "You're not authorized!");
- }
- }
- else if (e.Data.Message.StartsWith("!deop"))
- {
- if (authedHosts.Contains(user.Host))
- {
- string who = e.Data.Message.Replace("!deop", "");
- irc.Deop(e.Data.Channel, who);
- }
- }
- else if (e.Data.Message.StartsWith("!ban"))
- {
- if (authedHosts.Contains(user.Host))
- {
- string who = e.Data.Message.Replace("!ban", "");
- irc.Ban(e.Data.Channel, who);
- }
- }
- else if (e.Data.Message.StartsWith("!unban"))
- {
- if (authedHosts.Contains(user.Host))
- {
- string who = e.Data.Message.Replace("!unban", "");
- irc.Unban(e.Data.Channel, who);
- }
- }
- else if (e.Data.Message.StartsWith("!voice"))
- {
- if (authedHosts.Contains(user.Host))
- {
- string who = e.Data.Message.Replace("!voice", "");
- irc.Voice(e.Data.Channel, who);
- }
- }
- else if (e.Data.Message.StartsWith("!msg"))
- {
- string[] msgArray = { "!msg", user.Nick };
- string recipient = e.Data.MessageArray[1];
- string msg = e.Data.Message.Replace("!msg " + recipient, "");
- irc.SendMessage(SendType.Message, recipient, "From: " + user.Nick + ": " + msg);
- }
- else if (e.Data.Message.StartsWith("!time"))
- {
- if (e.Data.Message.Contains(user.Nick))
- {
- string person = e.Data.Message.Replace("!time ", "");
- irc.SendMessage(SendType.CtcpRequest, person, "TIME");
- }
- else
- {
- irc.SendMessage(SendType.CtcpRequest, user.Nick, "TIME");
- }
- }
- else if (e.Data.Message.Contains("fuck") || e.Data.Message.Contains("shit") || e.Data.Message.Contains("cunt") || e.Data.Message.Contains("fucking"))
- {
- irc.RfcKick(e.Data.Channel, user.Nick);
- irc.SendMessage(SendType.Message, e.Data.Message, "Language!");
- }
- else if (e.Data.Message.StartsWith("!admin"))
- {
- if (authedHosts.Contains(user.Host))
- {
- if (e.Data.Message.Contains("minecraft"))
- {
- if (e.Data.Message.Contains("add"))
- {
- string[] adminArray = { "!admin ", user.Nick, " add" };
- string admin = adminArray[1];
- irc.SendMessage(SendType.Message, "ChanServ", "SOP " + e.Data.Channel + "add" + admin);
- }
- if (e.Data.Message.Contains("del"))
- {
- string[] adminArray = { "!admin ", user.Nick, " del" };
- string admin = adminArray[1];
- irc.SendMessage(SendType.Message, "ChanServ", "SOP " + e.Data.Channel + " DEL " + admin);
- }
- }
- }
- }
- else if (e.Data.Message.StartsWith("!kick"))
- {
- if (authedHosts.Contains(user.Host))
- {
- string kicked = e.Data.Message.Replace("!kick", "").Trim();
- irc.RfcKick(e.Data.Channel, kicked, "Kicked by: " + user.Nick);
- }
- }
- else if (e.Data.Message.StartsWith("!join"))
- {
- if (authedHosts.Contains(user.Host))
- {
- string chann = e.Data.Message.Replace("!join ", "");
- irc.RfcJoin(chann);
- }
- }
- else if (e.Data.Message.StartsWith("!invite"))
- {
- if (authedHosts.Contains(user.Host))
- {
- string invite = e.Data.Message.Replace("!invite ", "");
- irc.RfcInvite(invite, e.Data.Channel);
- }
- }
- }
- }
- }
- void irc_OnCtcpReply(object sender, CtcpEventArgs e)
- {
- irc.SendMessage(SendType.Message, e.Data.Channel, e.CtcpParameter);
- }
- 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.SendMessage(SendType.Message, "Masher", "You are trusted with being authorized-type /msg MashBot !auth minecraft to auth");
- irc.SendMessage(SendType.Message, "Tharm_Bood", "You are trusted with being authorized-type /msg MashBot !auth minecraft to auth");
- irc.SendMessage(SendType.Message, "Shnaw", "You are trusted with being authorized-type /msg MashBot !auth minecraft to auth");
- irc.RfcJoin("#ninjas");
- irc.RfcJoin("#game");
- this.Invoke((MethodInvoker)delegate { label1.Text = "Connected"; });
- irc.Listen(); //Listen
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement