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
    Creating Scrolling Parallax Effects with CSS

    Introduction For quite a long time now websites with the so called "parallax" effect have been really popular. In case you have not heard of this effect, it basically includes different layers of images that are moving in different directions or with different speed. This leads to a...

  • By
    Write Better JavaScript with Promises

    You've probably heard the talk around the water cooler about how promises are the future. All of the cool kids are using them, but you don't see what makes them so special. Can't you just use a callback? What's the big deal? In this article, we'll...

Incredible Demos

  • By
    iPhone-Style Passwords Using MooTools PassShark

    Every once in a while I come across a plugin that blows me out of the water and the most recent culprit is PassShark: a MooTools plugin that duplicates the iPhone's method of showing/hiding the last character in a password field. This gem of...

  • By
    Create a Download Package Using MooTools Moousture

    Zohaib Sibt-e-Hassan recently released a great mouse gestures library for MooTools called Moousture. Moousture allows you to trigger functionality by moving your mouse in specified custom patterns. Too illustrate Moousture's value, I've created an image download builder using Mooustures and PHP. The XHTML We provide...

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!