Login Register






Basic C# IRC Bot filter_list
Author
Message
Basic C# IRC Bot #1
Just spent a little bit working on a couple of IRC classes, figured I would share them with you. Not really much error-handling at this point though, so you might want to add that yourself.

IRC.cs
Code:
using System; using System.Linq; using System.Net.Sockets; using System.IO; namespace SLIRC { class IRC { private int m_port; private string m_serv; private string m_nick; private string m_chan; private string m_user; public int Port { get { return m_port; } set { m_port = value; } } public string Server { get { return m_serv; } set { m_serv = value; } } public string Nickname { get { return m_nick; } set { m_nick = value; } } public string Channel { get { return m_chan; } set { m_chan = value; } } public string User { get { return m_user; } set { m_user = value; } } public IRC(string server, int port, string nick, string user, string channel, string botm = "Default bot message.") { this.Server = server; this.Port = port; this.Nickname = nick; this.Channel = channel; this.User = String.Format("USER {0} 8 * : {1}", user, botm); } public void Connect() { try { var v_irc = new TcpClient(Server, Port); var v_str = v_irc.GetStream(); var v_rdr = new StreamReader(v_str); var v_wtr = new StreamWriter(v_str); try { Send(v_wtr, new IRCMessage(User)); Send(v_wtr, new IRCMessage(IRCType.NICK, Nickname)); Send(v_wtr, new IRCMessage(IRCType.JOIN, Channel)); while (true) { var input = String.Empty; while ((input = v_rdr.ReadLine()) != String.Empty) { Process(v_wtr, input); } v_rdr.Close(); v_wtr.Close(); v_irc.Close(); } } catch (Exception ex) { Console.WriteLine(String.Concat("Unable to join: ", ex.Message)); return; } } catch (Exception ex) { Console.WriteLine(String.Concat("Unable to connect: ", ex.Message)); return; } } public void Process(StreamWriter wtr, string input) { var split = String.Join(" ", IRCType.PRIVMSG, Channel, ":"); if (input.Contains(split)) { var cmd_s = input.Split(new string[] { split }, StringSplitOptions.None)[1]; var args_ = cmd_s.Split(' '); var args = args_.Skip(1).Take(args_.Length - 1).ToList(); var cmd = args_[0]; if (cmd == "+say") { Send(wtr, new IRCMessage(IRCType.PRIVMSG, Channel, String.Join(" ", args))); } else if (cmd == "+divide") { var a = double.Parse(args[0]); var b = double.Parse(args[1]); Send(wtr, new IRCMessage(IRCType.PRIVMSG, Channel, String.Format("{0} / {1} = {2}", a, b, a / b))); } else if (cmd == "+multiply") { var a = double.Parse(args[0]); var b = double.Parse(args[1]); Send(wtr, new IRCMessage(IRCType.PRIVMSG, Channel, String.Format("{0} * {1} = {2}", a, b, a * b))); } else if (cmd == "+add") { var a = double.Parse(args[0]); var b = double.Parse(args[1]); Send(wtr, new IRCMessage(IRCType.PRIVMSG, Channel, String.Format("{0} + {1} = {2}", a, b, a + b))); } else if (cmd == "+subtract") { var a = double.Parse(args[0]); var b = double.Parse(args[1]); Send(wtr, new IRCMessage(IRCType.PRIVMSG, Channel, String.Format("{0} - {1} = {2}", a, b, a - b))); } } } public void Send(StreamWriter wtr, string msg) { wtr.WriteLine(msg); wtr.Flush(); } public void Send(StreamWriter wtr, IRCMessage msg) { wtr.WriteLine(msg.ToString()); wtr.Flush(); } } }

IRCMessage.cs
Code:
using System; namespace SLIRC { class IRCMessage { private string m_type; private string m_chan; private string m_data; public string Type { get { return m_type; } set { m_type = value; } } public string Channel { get { return m_chan; } set { m_chan = value; } } public string Data { get { return m_data; } set { m_data = value; } } public IRCMessage(string type, string channel, string data) { this.Type = type; this.Channel = channel; this.Data = data; } public IRCMessage(string fullmessage) : this(String.Empty, String.Empty, fullmessage) { } public IRCMessage(string type, string data) : this(type, String.Empty, data) { } public override string ToString() { if (this.Type == String.Empty && this.Channel == String.Empty) { return this.Data; } else if (this.Channel == String.Empty) { return String.Concat(this.Type, " ", this.Data); } else { return String.Format("{0} {1} :{2}", this.Type, this.Channel, this.Data); } } } }

IRCType.cs
Code:
namespace SLIRC { class IRCType { public const string NICK = "NICK"; public const string JOIN = "JOIN"; public const string PRIVMSG = "PRIVMSG"; } }

Usage:
Code:
var irc = new IRC("server.com", 6667, "botnick", "botname", "channel"); irc.Connect();
[Image: CDUAq9d.png]

Reply

RE: Basic C# IRC Bot #2
This is nice and clean, however you could go in more depth with your code why don't you?
Please roman, i do not wish to go bowling.

Reply

RE: Basic C# IRC Bot #3
(08-31-2015, 02:00 AM)Niko Bellic Wrote: This is nice and clean, however you could go in more depth with your code why don't you?

Explain how he could go "more in depth"

Reply