Some time ago I needed a bot for rolling dices. I found only one, written in Ruby, which I don’t know at all. That determined me to write my own bot. In that time I was working on Bukkit plugin so I decided to stick to the Java. I found out that there is magnificent API called PircBot. I’m going to show how use it to write your own bot.
Environment I use is Eclipse. I’ll also show here how to configure it.
Let’s begin with creating new project.
Name it and click Finish.
Now you need to right click on src and choose New > Package.
Name it as you wish and click Finish.
Now click with your right button on fresh package and select New > Class. Let’s call it Main to keep everything clear.
Before we start writing our bot we still need to do couple formal tasks. We’ll start from importing PircBot jar to our project. Download newest version of it HERE. When I am writing that article, the newest version is 1.5.0. In archive you will find jar file and folder called javadocs. Let’s start from importing jar to our project.
Click with right mouse button on the top of DiceRoller tree and choose from context menu properties. Then go to Java Build Path and click on Add External JARs… there.
You’ll see window in which you need to show Eclipse localization of pircbot.jar. To maintain order keep it in Eclipse workspace folder. After doing that you should see something like this:
Apply if asked for and go to Javadoc Location now. You’ll find it in same window on left side. Browse again to find its folder.
And there is last thing to do in project properties. We need to show Eclipse which class is our main. But we can’t do yet, we need to write our class.
Close that window and make sure that you are editing Main.java. Inside public class paste this:
public static void main(String[] args) throws Exception {
MyBot bot = new MyBot();
bot.setVerbose(true);
bot.connect(“irc.server.you.want.connect.to”);
bot.joinChannel(“#somechannel”);
}
Eclipse will show you that error occurred but nothing to worry about. It’s because we are referring to non-existent class. It begins with throwing exception mechanism. Java needs to have some instructions held inside it in case there will happen exception. Then we invoke new class, then turns on errors visibility, then connecting to server, then join specific channel.
That is everything we need in our Main class. Now let’s create new one called MyBot.
Begin with adding extension to public class MyBot, modify it like this:
public class MyBot extends PircBot {
}
Now Eclipse should highlight PircBot and show such message:
Click on Import ‘PircBot’ (org.jibble.pircbot) and… It’s done! New import just appeared:
import org.jibble.pircbot.PircBot;
If you copied public class code from here Eclipse could automatically did that import, there is nothing to worry about. Just be sure that you’ve got such import.
Now we need to name our bot. But this inside public class MyBot:
public MyBot() {
this.setName(“Bot”);
}
So whole code should look like:
package roller;
import org.jibble.pircbot.PircBot;
public class MyBot extends PircBot
{
public MyBot()
{
this.setName(“Bot”);
}
}
We’ll. That’s the moment when we can finally check how our bot works.
Remember how we were editing project properties? Let’s go there again. Choose from the menu Run/Debug Settings and then click on New… button.
Select Java Application and click OK.
New window will appear. Call it somehow and then click on Search… button.
In new window you should see you main class. Double click it. Apply and exit. Now we are ready to test our bot out. Right click on your project in Package Explorer on left side and choose Export. Double click Runnable JAR file.
From launch configuration chose class we set as main. In library handling select the option in middle. Choose your export destination and click Finish. That’s it.
Now we need to run it. Connect to server and channel you selected for that test and run command line. Go to catalogue you’ve saved your bot and type:
java -jar bot.jar
In couple seconds bot should appear on channel.
Whole code of MyBot.java:
package roller;
import org.jibble.pircbot.PircBot;
public class MyBot extends PircBot {
public MyBot()
{
this.setName("Bot");
}
public void onMessage(String channel, String sender,
String login, String hostname, String message) {
}
}
Mechanism of rolling dices will be shown in part 2.