Jump to content

Coding an IRC# Bot, some help please :)

- - - - -

This topic has been archived. This means that you cannot reply to this topic.
3 replies to this topic

#1
SgtPunishment

SgtPunishment

    Newbie

  • Members
  • Pip
  • 3 posts
I've got a blogspot that I've just started a week ago and got a couple of co-authors and we've set ourselves a challenge...

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.

#2
SgtPunishment

SgtPunishment

    Newbie

  • Members
  • Pip
  • 3 posts
33+ views and not a single reply... I could say that this is to advanced for the members here? Or no one actually wants to help.

#3
Davide

Davide

    Programming God

  • Members
  • PipPipPipPipPipPipPip
  • 506 posts
Instead of moving in other lib, try this:

#region Code for GUI
//code
#endregion

#region Code for Server
//code
#endregion

You will then be able to minimize the selected region by pressing the - sign on the left ;). (Assuming you are using VS of course)
Are you a newbie programmer trying to learn C#? Check out my small tutorial: Visual C# Programming Basics

#4
SgtPunishment

SgtPunishment

    Newbie

  • Members
  • Pip
  • 3 posts
One reason why I want to make it into a seperate lib with it's own event handlers is so I can use the same class file in different IRC related projects without having to junk up my main form's coding.

Also so I can share the lib file so newer coders have a nice easy base framework to work with and get their own IRCBots up and running in no time.