O'Reilly Hacks
oreilly.comO'Reilly NetworkSafari BookshelfConferences Sign In/My Account | View Cart   
Book List Learning Lab PDFs O'Reilly Gear Newsletters Press Room Jobs  


 
Buy the book!
IRC Hacks
By Paul Mutton
July 2004
More Info

HACK
#59
An Egg Timer
Create an IRC bot that can remind you to do something after a set interval
The Code
[Discuss (0) | Link to this hack]

Some people spend far too long in front of their computers using IRC. If you are one of those people, here is a hack that may appeal to you. Never again will you forget to do something important, because you'll be able to get an IRC bot to remind you!

This hack shows how to create a simple IRC bot that sits in any number of channels and responds to the !egg command. After three minutes, the bot will remind you that your egg is done, for this is how long it takes to boil the perfect egg.

Scheduling a task for later execution is easy with Java. The java.util.Timer class allows you to schedule tasks that extend java.util.TimerTask. The Timer class is scalable, so the bot should be perfectly capable of scheduling thousands of tasks at the same time. That's a lot of eggs.

The Code

You will need to create a special class called EggTimerTask that extends TimerTask. When the egg is ready, the run method in this class will be called. In the run method, the bot must send a message to the channel to tell the user that her egg is ready to eat. Instances of this class therefore need to store a reference to the bot, channel, and nickname of the user.

Create the file EggTimerTask.java:

import java.util.TimerTask;

public class EggTimerTask extends TimerTask {
    
    private EggTimerBot bot;
    private String nick;
    private String channel;
    
    public EggTimerTask(EggTimerBot bot, String nick, String channel) {
        this.bot = bot;
        this.nick = nick;
        this.channel = channel;
    }
    
    public void run( ) {
        bot.sendMessage(channel, nick + ": Your egg is ready!");
    }
    
}

Writing the actual bot is rather straightforward, as all you need to do is make it respond to messages that look like "!egg". It needs to create a new EggTimerTask and schedule it for running three minutes later.

Create the file EggTimerBot.java:

import org.jibble.pircbot.*;
import java.util.Timer;

public class EggTimerBot extends PircBot {
    
    public static final long DURATION = 3 * 60 * 1000;
    private Timer timer = new Timer(true);
    
    public EggTimerBot(String name) {
        setName(name);
    }
    
    public void onMessage(String channel, String sender,
            String login, String hostname, String message) {
        
        if (message.trim( ).toLowerCase( ).equals("!egg")) {
            sendMessage(channel, sender + ": I am timing your 3 minutes now...");
            EggTimerTask timerTask = new EggTimerTask(this, sender, channel);
            timer.schedule(timerTask, DURATION);
        }
    }
    
}

Finally, you just need a main method to start the bot and tell it to connect to a server and join a channel. If you want, you can ask this bot to join more than one channel, and it will still happily do its job.

Save the following as EggTimerBotMain.java:

public class EggTimerBotMain {
    
    public static void main(String[] args) throws Exception {
        EggTimerBot bot = new EggTimerBot("eggcook");
        bot.setVerbose(true);
        bot.connect("irc.freenode.net");
        bot.joinChannel("#irchacks");
    }
    
}


O'Reilly Home | Privacy Policy

© 2007 O'Reilly Media, Inc.
Website: | Customer Service: | Book issues:

All trademarks and registered trademarks appearing on oreilly.com are the property of their respective owners.