Headlines News :
Home » » Programming - create your own IRC bot using Java

Programming - create your own IRC bot using Java

Written By Meelan on Friday, May 9, 2014 | 5:04 AM

This tutorial is going to teach you how to create your own IRC bot in java.
Please note that this is NOT a step-by-step guide to complete programming challenge #8, but i will try to give you some basic hints.

First of all, the usual table of contents:

I.....What do I need to start?
II....Getting started
III...The bot class
IV....Tha main class
V.....Upgrading the bot class
VI....The version request
VII...Other Operations
VIII..Hints


I - What do I need to start?
There are some things you will need for this tutorial:

1) A basic knowledge of Java - I am not going to teach you the programming language itself!
So you should at least know the meaning of classes, objects, attributes and methods - and how to use them.

2) Of course you will need the usual Java developing tools which in fact are the JDK and a simple editor.
Although even vi or notepad will work as an editor, i strongly recommend something that at least supports syntax highlighting.
In this tutorial we will be using the Eclipse IDE which you can get HERE

3) The PircBot framework -> get it HERE
I also recommend to read its JavaDoc Documentation.

4) Some knowledge about IRC and its commands.
Reading the 'IRC Command Reference'on HTS should do the job ;)

5) Brain.exe, located in your head's system folder.


II - Getting started
1) Start Eclipse (Orly? Thx Captain Obvious!) and create a new project by following the wizard.

2) In the 'Libraries' window choose 'Add External JARs' and select the .jar file containing the PircBot framework.
Done? Ok, klick finish and you're ready to go!


III - The Bot Class
So here's where the actual programming begins.
First we create a new class for our bot. In this exampel we will simply name it 'BotClass'.
Since PircBot provides us with a superclass for this purpose, we may want to use it.
But to do so we first need to import the proper package.
Now we can use the super class by using the 'extends' keyword in our class definition, followed by the name of the super class.
Our code should look like this now:
CODE :

import org.jibble.pircbot.*;

public class BotClass extends PircBot{

}

To give our bot a nickname upon creation we should add a proper constructor - And there's even a built-in function to assign a nickname to the bot!
CODE :

import org.jibble.pircbot.*;

public class BotClass extends PircBot{

//Constructor
public BotClass(String name){
setName(name);
}

}

Well, thats the first (and most simple) version of our bot. Now we need to use it!


IV - The main class
The goal of our main class is pretty simple:
1) Create an instance of our bot class
2) Let it connect to the IRC server

That is what i call the static startup. After these two steps our bot is online and able to receive commands.
But there may be some additional actions you want the bot to perform on startup, so we will add 2 more actions:

3) Login @ NickServ - as we know the challenge requires the bot to use a nick that is linked with your HTS account
4) Join a channel
5) Turn on the console output of our bot (REALLY useful!)

Hey, today is our lucky day! Why? Because PircBot provides us with built-in functions for all of those actions!

these are:
CODE :

connect(String servername) //connect to a server
sendMessage(String channel_or_nick, String Message) //send message to channel or nickname
joinChannel(String channelName) //join a channel
setVerbose(boolean on_off) //tunrs output on/off

Be aware that the connect-method may throw 3 types of exceptions, so we have to catch them.
But since this is only a basic tutorial we may catch all 3 of them at once.
So with all of these method calls we get a nice small main class, which looks something like this:
CODE :

public class MainClass {

public static void main(String[] args){
BotClass bot=new BotClass("nickname123");

try{
bot.setVerbose(true);
bot.connect("irc.hackthissite.org");
bot.sendMessage("nickserv","IDENTIFY <your password>" );
bot.joinChannel("#bottest");
}
catch (Exception e){e.printStackTrace();}
}
}

Now we can run our bot for the first time!
What will happen?

The Bot will connect to the HTS IRC server, message nickserv to login with your registered account and finally will join the channel #bottest.
Congratz, you just created the king of idle-bots!
But of course this is kinda lame, our bot should at least respond to a few commands. So let&#039;s have a look at the bot class again!


V - Upgrading the bot class
To make our bot a little bit more interesting we need to make it respond to certain messages.
To do so we have to override some of the methods we are provided with.

The methods we are looking for are
CODE :

onMessage(String channel, String sender, String login, String hostname, String message)
onNotice(String sourceNick, String sourceLogin, String sourceHostname, String target, String notice)
onPrivateMessage(String sender, String login, String hostname, String message)

since these are the main types of messages the bot will receive. You already knew that? Well, i guess then you are one of the few who actually payed attention to the PircBot documentation ;)

So, lets override one of this methods together - you may consult your Brain.exe to do this with the other methods since they are not that different.
So, what do we want the bot to do? Exactly, respond to received commands. We will call that &#039;message handling&#039;.
In fact it takes the received message and compares it with the commands it supports. If the Command is supported, it will answer.
So lets override the onMessage method to make the bot reply to the &#039;!hello&#039; command:
CODE :

public void onMessage(String channel, String sender, String login, String hostname, String message){

if(message.equalsIgnoreCase("!hello")) //compares the received Message with the String &#039;!hello&#039;
{
sendMessage(channel, "Hello "+sender); //Replies with &#039;Hello <username>&#039;
return;
}
}

Pretty simple, isn&#039;t it?
Now everytime you greet your bot with &#039;!hello&#039; it will respond. Awesome!

HINT: The whole conversation with moo in the Prog8 challenge is done via notice.
So you might want to override one of the other mentioned methods. Again: Use your Brain.exe ;)



VI - The version request
As you probably know, there&#039;s something called version request. To respond to such a request simply override the method called onVersion.
But what does a version response look like?
Well it looks something like this
CODE :

VERSION name:version:OS

and is wrapped in unicode.
Let&#039;s say we want to respond with &#039;VERSION myBot:1.0:YOURMOM&#039;
Then our string would read "\001VERSION myBot:1.0:YOURMOM\001".

so our onVersion method should look like this:
CODE :

public void onVersion(String sourceNick, String sourceLogin, String sourceHostname, String target){
sendNotice(sourceNick,"\001VERSION myBot:1.0:YOURMOM\001");
}


VII - Other Operations
Our bot is not limited to send text-based messages.
In fact there are tons of actions it could perform, like kicking, banning, inviting etc.
I once again recommend you to give the documentation a look for a quick overview of the implemented methods and how to use them.


VIII - Hints
With this tutorial you should be able to easily beat Prog8, but if you still have trouble, here are some hints:

1) Keep in mind that the whole conversation with moo is done via notify!
2) Keep in mind that the version-response must be wrapped in unicode!
3) Having trouble creating the hash? Get familiar with the java.security package ;)
4) Don&#039;t underestimate the overwhelming power of your own personal Brain.exe
Share this post :

Post a Comment

 
Support : VoidCircle | MoviezCine | Join Us
Copyright © 2011. VoidCircle - All Rights Reserved
Template Created by VoidCircle Published by VoidCircle
Proudly powered by Blogger