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
#60
A Trivia Bot
Turn an IRC bot into an automated quiz show host. Compete with your friends to be first to answer each question
The Code
[Discuss (1) | Link to this hack]

The Code

To make your trivia bot easy to extend, you should create a Question class, so each question (and answer) can be stored in a Question object. The Question class also provides an isCorrect method, so you can see if a candidate answer is correct.

Create a file called Question.java:

public class Question {
    
    private String question;
    private String answer;
    
    public Question(String question, String answer) {
        this.question = question;
        this.answer = answer;
    }
    
    public String toString( ) {
        return question;
    }
    
    public String getAnswer( ) {
        return answer;
    }
    
    public boolean isCorrect(String a) {
        return answer.equalsIgnoreCase(a);
    }
    
}

The TriviaBot class will be used to maintain a collection of Question objects. Each time a new question is asked, it will be chosen randomly from this collection. As soon as the trivia bot joins a channel, it will ask a question.

Whenever someone sends a message to the channel, the trivia bot will check to see if it matches the answer for the current question. If the answer is correct, the trivia bot will announce the sender of that message as being the winner.

This trivia bot will also allow users to say the "clue" command. If anybody says "clue" in the channel, the trivia bot will respond by showing how many letters are in the answer. It does this by replacing each letter in the answer with a * character and sending it to the channel. So if the correct answer is "Internet Relay Chat," the bot will send the following to the channel:

<Jibbler> clue
<TriviaBot> Clue: ******** ***** ****

This class extends PircBot to connect to IRC and uses the Random class in the java.util package to generate random question numbers.

Create a file called TriviaBot.java:

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

public class TriviaBot extends PircBot {
    
    private Question currentQuestion = null;
    private ArrayList questions = new ArrayList( );
    private static Random rand = new Random( );
    
    public TriviaBot(String name) {
        setName(name);
    }
    
    public void addQuestion(Question question) {
        questions.add(question);
    }
    
    public void onJoin(String channel, String sender,
            String login, String hostname) {
        
        if (sender.equals(getNick( ))) {
            setNextQuestion(channel);
        }
    }
    
    public void onMessage(String channel, String sender, String login,
            String hostname, String message) {
        
        message = message.toLowerCase( ).trim( );
        if (currentQuestion.isCorrect(message)) {
            sendMessage(channel, sender + " is the winner, with the correct"
             + "answer of" + currentQuestion.getAnswer( ));
            setNextQuestion(channel);
        }
        else if (message.equalsIgnoreCase("clue")) {
            String clue = currentQuestion.getAnswer( );
            clue = clue.replaceAll("[^\\ ]", "*");
            sendMessage(channel, "Clue: " + clue);
        }
    }
    
    private void setNextQuestion(String channel) {
        currentQuestion = (Question) questions.get(rand.nextInt(questions.size( )));
        sendMessage(channel, "Next question: " + currentQuestion);
    }
    
}

The TriviaBot class uses an ArrayList to store each Question object. New questions can be added to the bot with the addQuestion method. The setNextQuestion method is used to pick a random question and announce it to the channel.

The onJoin method is overridden and will be called whenever the bot joins a channel. The bot will then ask the first randomly picked question.

The onMessage method is overridden and will be called whenever someone sends a message to the channel. If the message matches the correct answer for the current question, the bot will announce that the sender won and it will then pose the next question and carry on as before. If anybody sends the "clue" command, the bot will send the clue to the channel.


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.