Library tutorials & articles

A Console IRC Bot

Responding to Commands

My plan was just to display data, but let me show you how you can respond to commands. I know this could be solved cleaner, but I'm writing this as a proof of concept and to explain what would be required and how it works.

We want to welcome everyone joining a channel. But we want it by notice. When someone joins the IrcJoin method gets called:

private void IrcJoin(string[] IrcCommand) {
      string IrcChannel = IrcCommand[2];
      string IrcUser = IrcCommand[0].Split('!')[0];
      if (eventJoin != null) { this.eventJoin(IrcChannel.Remove(0, 1), IrcUser); }
} /* IrcJoin */

Which in turns fires the join event and gets processed in our console app:

private void IrcJoin(string IrcChan, string IrcUser) {
      Console.WriteLine(String.Format("{0} joins {1}", IrcUser, IrcChan));
      IrcObject.IrcWriter.WriteLine(String.Format("NOTICE {0} :Hello {0}, welcome to {1}!", IrcUser, IrcChan));
      IrcObject.IrcWriter.Flush ();
} /* IrcJoin */

I modified our console app a bit so it would create an object of itself in the Main with our IrcObject as a private variable. That way I can access the IrcWriter I created in there. Of course it is a bad idea to make that writer public. A better practice would be to create methods like NoticeUser , KickUser , etc... to control it's behaviour. But that exceeds the purpose of this article.

This concludes how you use C# to get on IRC.

Here are some ideas where you could extend this application:

  • Listen for certain triggers in the channel, then do some action. (Example: ' !google searchterm' , have your app do a query and reply the results)
  • Make this bot an opp and listen in PRIVMSG for a user to authenticate and op him. (Authenticate him against Active Directory, that way you'll learn how to work with AD as well)
  • Detect kicks against trusted users and take action to prevent takeovers, or auto rejoin when the bot gets kicked.
  • ...
Hopefully this answered the question, feel free to contact me when you have any questions.

Comments

  1. 19 Apr 2007 at 22:25

    i have the same error as this guy, i know this was LONG ago but i cannot figure out how to get past this !

     

    can someone please contact me

    Email:  vincecarter15x15@aol.com    

    AIM : skatecrashrepeat

    Alternative Email: Punk123@myway.com

      PLEASE I NEED THIS DESPERATELY

  2. 19 Aug 2006 at 00:08

    That is not a C# error.. It's actually an IRC error, which means you connected to the server. You need to do a few commands so that you can login/etc.

    Example:

    USER Veeresh Veeresh :Veeresh Veeresh\r\n

    NICK Veeresh\r\n

    JOIN #quakenet\r\n

    PRIVMSG #quakenet :Hey, what's up\r\n

  3. 17 Jun 2005 at 07:16

    i tried to run this application, i got following error, plz help me out.


    NOTICE AUTH :* Looking up your hostname NOTICE AUTH : Checking Ident NOTICE AUTH :** Couldn't look up your hostname :blueyonder2.uk.quakenet.org 451 * :Register first. :blueyonder2.uk.quakenet.org 451 * :Register first. :blueyonder2.uk.quakenet.org 451 * :Register first. :blueyonder2.uk.quakenet.org 451 * :Register first. :blueyonder2.uk.quakenet.org 451 * :Register first. :blueyonder2.uk.quakenet.org 451 * :Register first. ERROR :Closing Link: by blueyonder2.uk.quakenet.org (Registration Timeout) Cannot read from a closed TextReader.

  4. 22 Dec 2004 at 16:26

    please put it in vb.net form :]


    or any vb

  5. 25 Mar 2004 at 05:57

    Welcome to Developer Fusion, David!

  6. 24 Mar 2004 at 14:29
    Well, hello This is my first article here, thanks to James Crowley. Nice site you have!

    Feel free to ask me anything on my blog if you want more info.
  7. 01 Jan 1999 at 00:00

    This thread is for discussions of A Console IRC Bot.

Leave a comment

Sign in or Join us (it's free).

David Cumps
AddThis

Related podcasts

  • Object-Oriented Programming in Ruby

    In this episode, I talk with Scott Bellware about object-oriented programming in Ruby, and Ruby's object model. This is taken from a private conversation, and the audio quality suffers at times. Much thanks to Scott for allowing this to be released.This episode of the Alt.NET Podcast is bro...

Want to stay in touch with what's going on? Follow us on twitter!