The challenge is scratch code an IRC Bot in C#, Hence why I call it an IRC# Bot lol.
Anyways, I've got my bot up and running (staying connected) and it has a basic test command which works... Now I just want to clean up the code by quite a bit.
I'd like to move all of the IRC Stuff into a seperate class lib so I can have the main form for commands etc so I only need to do stuff like
Connection.Connect(txtServer.Text, nudPort.Value); for the connection vaule, but I'm quite new to C# so I'm a bit stuck... Here's my code as it stands now...
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Net;
using System.Net.Sockets;
using System.Threading;
using System.Text.RegularExpressions;
using System.IO;
namespace IRCBot
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
BGW.WorkerSupportsCancellation = true;
}
private void Form1_Resize(object sender, EventArgs e)
{
if (WindowState == FormWindowState.Minimized)
{
Hide();
notifyIcon1.BalloonTipTitle = "SpyBot IRC Bot Hidden";
notifyIcon1.BalloonTipText = "Your application has been minimized to the taskbar.";
notifyIcon1.ShowBalloonTip(3000);
}
}
private void notifyIcon1_DoubleClick(object sender, EventArgs e)
{
Show();
WindowState = FormWindowState.Normal;
}
private void CMS_Opening(object sender, CancelEventArgs e)
{
Show();
WindowState = FormWindowState.Normal;
}
private void exitToolStripMenuItem1_Click(object sender, EventArgs e)
{
Close();
}
private void exitToolStripMenuItem_Click(object sender, EventArgs e)
{
Close();
}
TcpClient SERVER = new TcpClient();
string readline;
private void btnConnect_Click(object sender, EventArgs e)
{
if (btnConnect.Text == "Connect")
{
lblStatus.Text = "Connected";
lblStatus.ForeColor = System.Drawing.Color.Green;
SERVER.Connect(txtServer.Text, 6667);
BGW.RunWorkerAsync();
btnConnect.Text = "Disconnect";
}
else if (btnConnect.Text == "Disconnect")
{
lblStatus.Text = "Not Connected";
lblStatus.ForeColor = System.Drawing.Color.Red;
SERVER.Close();
BGW.CancelAsync();
btnConnect.Text = "Connect";
}
}
private void IRCWINDOW()
{
txtIRC.AppendText(readline + Environment.NewLine);
}
private void pong()
{
txtIRC.AppendText("PONG " + readline.Substring(5) + "\r\n");
}
private void BGW_DoWork(object sender, DoWorkEventArgs e)
{
StreamReader Reader = new StreamReader(SERVER.GetStream());
StreamWriter Writer = new StreamWriter(SERVER.GetStream());
Writer.WriteLine("USER IRCSharpBot 8 * IRCSharpBot");
Writer.WriteLine("NICK " + txtBotNick.Text);
Writer.Flush();
while ((readline = Reader.ReadLine()) != null)
{
Writer.WriteLine("JOIN " + txtChannel.Text);
Writer.Flush();
txtIRC.Invoke(new MethodInvoker(IRCWINDOW));
if (readline.StartsWith("PING"))
{
Writer.WriteLine("PONG " + readline.Substring(5) + "\r\n");
txtIRC.Invoke(new MethodInvoker(pong));
Writer.Flush();
}
if (readline.EndsWith("!Hello"))
{
Writer.WriteLine("PRIVMSG " + txtChannel.Text + " \u0002Hello World!\u000F");
Writer.Flush();
}
}
}
}
}
If someone is able to help me I will be greatful and will include you in the credits when I release the program.


Sign In
Create Account

Back to top









