The David Walsh Blog

Build IRC Bots with NodeJS

Written by David Walsh on October 26, 2012

One of the tasks on my WebDev bucket list has always been creating a functional IRC bot.  I have no clue why it's been high on my list, but ... it just has.  IRC bots are used for a variety of things:  Google search, keyword detection and information retrieval (i.e. a message with a bug number in Mozilla's IRC will fetch the title and link of the given bug), and more.  I recently found a NodeJS library which allowed me to listen to common IRC events and respond to them;  better yet, it's incredibly easy to do.  Let me show you how to create your own IRC bot with JavaScript!

Node IRC Install

Use NPM to install the IRC library:

npm install irc

This the only external library you will need!

Bot Configuration and Retrieval

Setting up a config object is recommended as many settings may be shared throughout the bot's event listeners:

// Create the configuration
var config = {
	channels: ["#davidwalshblog", "#mootools"],
	server: "irc.freenode.net",
	botName: "walshbot"
};

Then require the IRC library and create your bot:

// Get the lib
var irc = require("irc");

// Create the bot name
var bot = new irc.Client(config.server, config.botName, {
	channels: config.channels
});

Now you're setup is complete;  time to make things happen!

Bot Listeners and Responses

Once a configuration is created, the library is available, and the bot has been created, the possibilities are endless.  The majority of interactions will start with an event listener and a basic response:

// Listen for joins
bot.addListener("join", function(channel, who) {
	// Welcome them in!
	bot.say(channel, who + "...dude...welcome back!");
});

Whenever a user enters the room, they'll be greeted with the response above.  If I want to listen to each message and respond accordingly, I could add:

// Listen for any message, PM said user when he posts
bot.addListener("message", function(from, to, text, message) {
	bot.say(from, "¿Que?");
});

// Listen for any message, say to him/her in the room
bot.addListener("message", function(from, to, text, message) {
	bot.say(config.channels[0], "¿Public que?");
});

There are many more events to listen for too;  the IRC API is quite extensive.  In the case of the Mozilla bug bot I mentioned above, one would simply need to parse the message for a 6 digit number to trigger a bug detail lookup, then message the response when the desired information was retrieved. With these event listeners in place, you can use any other NodeJS lib or custom code to perform any function you'd like.

Running the Bot

Running the bot is simple too -- simply open a new shell and execute:

node bot.js

Thanks to this awesome NodeJS IRC library and its incredibly easy to use API, I can cross off creating an IRC bot from my WebDev bucket list.  Better yet, I got to create the bot using JavaScript.  If you want to create a useful bot, or simply one to annoy people, give the IRC library a try!

  • LightFace: Facebook Lightbox for MooTools

  • How to Create a Twitter Card

  • Create Spinning Rays with CSS3: Revisited

Comments

  1. Christopher McCulloh October 26, 2012 @ 1:23 pm

    This irc library is great, but, it’s more of a barebones client library than it is a bot.

    –however, I’ve begun to write a bot on top of it. It’s pretty fast and modular. Check it out:

    https://github.com/cmcculloh/FUELBot

  2. Nathan Mynarcik October 26, 2012 @ 5:01 pm

    Great post! With that and Chris’ code, it has helped me start a new project for a Twitch.tv IRC Chat bot! Thanks guys!

  3. Krzysztof Lenda October 27, 2012 @ 12:26 pm

    Hi,
    Really great post. Just like Nathan, I decided to start a new project after reading your post. Instead of Nathan’s Chat bot, I’m going to write simple IRC Stat bot. I was thinking about it for a couple of months and now, with irc module and your article, it’s became very easy :) Thanks for that!
    Link to my chat bot repo: https://github.com/foull/irc-stat-bot (I just started working on it, so it’s quite empty for now ;))

  4. Avinash October 28, 2012 @ 5:42 am

    nice one dude…

  5. Callum Macrae October 29, 2012 @ 4:52 pm

    Nice article! Back when I used Node a lot, I built a few IRC bots and ended up writing my own library. It’s actually fairly simple; you can see a very basic example in a tutorial I wrote here: webdevrefinery.com/forums/topic/8762-writing-a-very-simple-irc-bot-in-nodejs/

    Oh, also:

    Now you’re setup is complete; time to make things happen!

Be Heard

Tip: Wrap your code in <pre> tags or link to a GitHub Gist!

Use Code Editor
Older
Rolling Your Own RSS Feed with Express and Jade
Newer
CSS Kwicks
gipoco.com is neither affiliated with the authors of this page nor responsible for its contents. This is a safe-cache copy of the original web site.