Check out my gitHub page! and my updated CV!

Friday, November 1, 2013

IRC BOT: node.js, why not!?

Some time ago I created an IRC channel, and what do you obviously want to do if you are the admin of an IRC channel?! create an awesome bot!

So let's create an IRC bot!
First thing was to decide the programming language:
I saw a lot of examples, from RubyPythonC, even Haskell! but all of them were a bit too complex, with tons of lines of code (except C, which was actually my first idea) that were unuseful for me.
So, what did I want?
I wanted something fast to write (I allocated no more than 2 hours for writing the whole code) and  easy to configure, with the following actions:
 - respond when consulted;
 - give a challenge, wait for some time and activate a callback when the timeout expire.

Wait a minute, something silent, event-driven, scalable, non-blocking (should give different challenges to different channel at the same time).. that's the definition written on the home page of node.js!
 Lucky me, somebody already implemented a module for implementing IRC in a really easy way, the code is basically a huge switch for doing different activities based on the message received by the client, with a few additions (a parser, a flood-avoidance), sounds good for me!

So, what is needed in order to configure it?

This:

;

Where config.json is

;

Not bad, uh?!

Then we just have to add some listeners, according to what we want the bot to listen to!

I wanted the bot to listen to the messages said on the channel, and if it sees a keyword ("@challenge"), activate a challenge!

the interface is the following:

;

really easy, so we just have to look at the message (which is a string, so we can/should parse it in a text via split), parse it and look if it contains our beloved string.

when the bot want to say something, all it has to do is just

;

Just that!

NOTE: currently I'm having an issue when a player is leaving a channel, the bot crashes with a ValueError. Quick and dirty solver, put the for content at line 503 in a try/catch statement and it won't happen again.

I just had to implement the logic for supporting challenges, with timeout etc, and that's it!

The implemented logic is really easy, the bot listen for a message starting with "@challenge", then look at the next word, and if there is a challenge with that name. If present, it then loads a challenge, write it to the channel and set a timeout.
If the bot receives a message starting with "@sol", look if there is an active challenge with that name for that channel, check the proposed solution with the actual one, and if the outcome is positive, it delete the timer and gives the prize.

I wanted an easy way to add new challenges, where I didn't have to touch the code, therefore I created a separate file (which has to be called with require), which gives to the bot the following object:



In this way we can create a challenge in a quick and easy way, all you need is just an idea of a challenge.
The only issue is that right now the challenge is intended as a question-answer, without middle-steps, but in that case is just a matter of doing a slight modification to the bot.js, by allowing an array of function as chall and solver, so that we can track the current step.

Link to the github repo.

Have fun!

No comments:

Post a Comment