Hack 58 A DiceBot


who likes to use IRC, make your own bot to roll the dice for you and
your friends.
Dice are used
for many games, such as yahtzee, craps, or even more elaborate
adventure games like Warhammer (http://www.games-workshop.com). Some games
even require special dice with more than six sides. If you lose your
dice, you may become stuck where you are. But with an IRC bot to roll
virtual dice for you, you'll always be able to play
such games against other players on the Internet without any risk of
anyone cheating, as you will be able to see their dice rolls.This hack will show you how to make a simple IRC bot that rolls a
single die with any number of sides.
9.2.1 The Code
The random numbers will be generated with
an instance of the Random class in Java. This can be used to generate
a pseudorandom sequence of numbers, which
is more than adequate for a dice-rolling application.The goal is to create a bot that responds to the
!roll command by rolling a die and displaying the
value.Create a file called DiceBot.java:
import org.jibble.pircbot.*;Notice that another feature has been introduced to the bot: a kind of
import java.util.Random;
public class DiceBot extends PircBot {
private Random random = new Random( );
public DiceBot(String name) {
setName(name);
}
public void onMessage(String channel, String sender,
String login, String hostname, String message) {
message = message.trim( ).toLowerCase( );
if (message.equals("!roll")) {
sendMessage(channel, "I rolled a " + (random.nextInt(6) + 1));
}
else if (message.equals("!shoot")) {
if (random.nextInt(6) == 0) {
kick(channel, sender, "BANG!");
}
else {
sendMessage(channel, sender + ": click");
}
}
}
}
Russian roulette game. If a user sends the message
!shoot to the channel, there will be a one in six
chance of the bot kicking him from the channel with the reason,
"BANG!" If the user manages to
escape the bullet, the bot will just send
"click" to the channel.For the Russian roulette feature to work properly, the bot must have
channel operator status in the appropriate channels. The bot is
capable of running in multiple channels.A main method is required to tell the bot which server to connect to
and which channel or channels to join. Save the following as
DiceBotMain.java:
public class DiceBotMain {
public static void main(String[] args) throws Exception {
DiceBot bot = new DiceBot("Dice");
bot.setVerbose(true);
bot.connect("irc.freenode.net");
bot.joinChannel("#irchacks");
}
}
9.2.2 Running the Hack
You can compile the hack by typing:
C:\java\DiceBot> javac -classpath pircbot.jar;. *.javaRun the hack with:
C:\java\DiceBot> java -classpath pircbot.jar;. DiceBotMain
9.2.3 The Results
Figure 9-1 shows users Jibbler and Monty being
lucky with their dice rolling. Jibbler then ends up getting kicked
from the channel by pushing his luck with the
!shoot command.
Figure 9-1. Using the DiceBot

than six sides. If you are feeling adventurous, you could even try to
create a bot that takes part in these games.