Sunday, April 6, 2008

an IRC Bot written using BGGA

I was curious how hard it'd be to write an IRC Bot using BGGA closures; now to compile this you'll need to grab the prototype and the jerklib jar. So here's the code:

import jerklib.ConnectionManager;
import jerklib.ProfileImpl;
import jerklib.Session;
import jerklib.events.IRCEvent;
import jerklib.events.JoinCompleteEvent;

/**
* Created: Apr 6, 2008 9:19:13 PM
*
* @author Robert O'Connor
*/
public class BGGAIRCBot {
ConnectionManager manager = new ConnectionManager(new ProfileImpl("bggabot","bggabot","bggabot2","bggabot3"));
Session s = manager.requestConnection("irc.freenode.org");

public BGGAIRCBot() {
s.addIRCEventListener({IRCEvent e =>
if(e.getType() == IRCEvent.Type.CONNECT_COMPLETE) {
e.getSession().join("#jerklib");
} else if(e.getType() == IRCEvent.Type.JOIN_COMPLETE) {
JoinCompleteEvent jce = (JoinCompleteEvent)e;
jce.getSession().sayChannel(jce.getChannel(),"Hi from BGGA!!!");
}
});



}

public static void main(String[] args) {
new BGGAIRCBot();

}


}


Now, what would it look like without BGGA? It would like something like this:


import jerklib.ConnectionManager;
import jerklib.ProfileImpl;
import jerklib.Session;
import jerklib.events.IRCEvent;
import jerklib.events.JoinCompleteEvent;
import jerklib.events.listeners.IRCEventListener;

/**
* Created: Apr 6, 2008 10:05:09 PM
*
* @author Robert O'Connor
*/
public class JavaBot {
ConnectionManager manager = new ConnectionManager(new ProfileImpl("javabot", "javabot", "javabot2", "javabot3"));
Session s = manager.requestConnection("irc.freenode.org");

public JavaBot() {
s.addIRCEventListener(new IRCEventListener() {
public void receiveEvent(IRCEvent e) {
if (e.getType() == IRCEvent.Type.CONNECT_COMPLETE) {
e.getSession().join("#jerklib");
} else if (e.getType() == IRCEvent.Type.JOIN_COMPLETE) {
JoinCompleteEvent jce = (JoinCompleteEvent) e;
jce.getSession().sayChannel(jce.getChannel(), "Hi from Java!!!");
}

}


});
}

public static void main(String[] args) {
new JavaBot();
}

}

No comments: