Build IRC Bots with Node.js

By  on  

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 Node.js 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 Node.js 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!

Recent Features

  • By
    5 Awesome New Mozilla Technologies You’ve Never Heard Of

    My trip to Mozilla Summit 2013 was incredible.  I've spent so much time focusing on my project that I had lost sight of all of the great work Mozillians were putting out.  MozSummit provided the perfect reminder of how brilliant my colleagues are and how much...

  • By
    Create a CSS Cube

    CSS cubes really showcase what CSS has become over the years, evolving from simple color and dimension directives to a language capable of creating deep, creative visuals.  Add animation and you've got something really neat.  Unfortunately each CSS cube tutorial I've read is a bit...

Incredible Demos

  • By
    Vertically Centering with Flexbox

    Vertically centering sibling child contents is a task we've long needed on the web but has always seemed way more difficult than it should be.  We initially used tables to accomplish the task, then moved on to CSS and JavaScript tricks because table layout was horribly...

  • By
    Chris Coyier’s Favorite CodePen Demos

    David asked me if I'd be up for a guest post picking out some of my favorite Pens from CodePen. A daunting task! There are so many! I managed to pick a few though that have blown me away over the past few months. If you...

Discussion

  1. 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. 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. 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. nice one dude…

  5. 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: http://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!

  6. the guy

    where do you input all of this stuff

  7. Hello David, great how-to.
    This explain very well the basis to lead into any irc project we want to do.
    In my case what I want is to build an answering machine for my user, so when I’m away from keyboard this bot will record any message, and send them to me when I’m back again.
    I’m in the zero phase of this so any link would be helpful.
    regards !

  8. Hi,

    Great post ! However, isn’t there a better way to get the current channel’s name when making the bot speak into it, than getting the first indice of config.channels ? What if we want to reply in the right channel only ?

    Thanks however, I’m having great fun right now thanks to this article :)

  9. Ryan Guill

    We used node-irc to build our irc-bot zoidbox – https://github.com/atuttle/zoidbox – hope it can be useful to others as well!

Wrap your code in <pre class="{language}"></pre> tags, link to a GitHub gist, JSFiddle fiddle, or CodePen pen to embed!