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
- 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 (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 == "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("!commands"))
- {
- irc.SendMessage(SendType.Message, channel, "Normal users: !eat, !me Authed users: !op, !deop, !voice, !ban, !unban");
- }
- 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(user.Host))
- {
- string who = e.Data.Message.Replace("!ban", "");
- irc.Ban(channel, who);
- }
- }
- else if (e.Data.Message.StartsWith("!unban"))
- {
- if (authedHosts.Contains(user.Host))
- {
- 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("!msg"))
- {
- if (e.Data.Message.Contains(user.Nick))
- {
- string msg = e.Data.Message.Replace("!msg", "");
- irc.SendMessage(SendType.Message, user.Nick, "From:" + user + msg);
- }
- }
- else if (e.Data.Message.StartsWith("!time"))
- {
- string person = e.Data.Message.Replace("!time ", "");
- irc.SendMessage(SendType.CtcpRequest, person, "TIME");
- }
- }
- }
- void irc_OnCtcpReply(object sender, CtcpEventArgs e);
- {
- if (e.Data.Message.Contains(""))
- irc.SendMessage(SendType.Message, channel, message);
- }
- 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(channel); //Join the channel
- this.Invoke((MethodInvoker)delegate { label1.Text = "Connected"; });
- irc.Listen(); //Listen
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement