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.

You might also like...

Comments

Contribute

Why not write for us? Or you could submit an event or a user group in your area. Alternatively just tell us what you think!

Our tools

We've got automatic conversion tools to convert C# to VB.NET, VB.NET to C#. Also you can compress javascript and compress css and generate sql connection strings.

“Every language has an optimization operator. In C++ that operator is ‘//’”