I spent the last few days exploring IRC and building a few bots.
IRC stands for Internet Relay Chat. It’s an application protocol layer that facilitates the transfer of text. I wrote an emoticon bot that serves emoticons on demand, and a gif bot that searches the giphy api to provide gifs when given a certain search tag (otherwise it just returns a random gif.)
I’ll be walking through how to set up a bot using Node.
First, make the directory where your project will live.
I named mine, ircBots, so I’ll refer to the directory as that from now on.
~$ mkdir ircBots
~$ cd ircBots/
###Install Dependencies I used the IRC Node Module. It’s lightweight, and does what you need it to
~/ircBots$ npm install irc
if you already have a package.json file, add ‘–save’ to the end to automatically update your file with the dependency
Okay, that’s all we’ll need for right now!
###Let’s make a bot! First, define the irc module in your code.
Next, let’s create a way to access some of the variables that will be used.
you don’t actually have to seperate these out, I find it just makes it a little easier to control
Then, create the instance of the bot!
the third parameter is an options object. Docs
This creates the bot, however in order for it to actually connect, you’ll need to call the join function with the room you want to join as the first parameter, and an optional callback function as the second.
That will allow you to join the channel. If you want the bot to say something when it joins the room (which I personally like), you can add it in the callback function. The first parameter will be who you want to send the message to, the second will be the text to send.
This will send a message to the channel upon the bot joining. If you’d like to send a private message to a user, just pass the user name instead of the channel name
If you save and run this, your bot will connect to the irc channel and say ‘Hello!’ upon joining!
###Event Listeners Most people who make a bot, don’t just want it to say something when it enters a room, they want it to respond to a person when asked a certain question or when hearing a certain phrase.
You can do this by adding event listeners to your bot.
Here’s a list of the events you can use
First, let’s add the error event listener.
Now, the one you’ll probably want to use is the message event listener. This listens for messages in the channel you’re in, and will call your callback function when it’s triggered. For now, let’s just make it console.log all of the messages.
If you save and run this, you’ll see that anytime someone posts a message in the channel, it gets console.log’d.
So, that’s it! The rest of making the bot do cool stuff includes the callback function within the message listener.
The example code from this post is here