0

Hello i'm currently making a chat bot for my twitch stream but i got a problem only one command is working and that is the !song command all the other commands wont work for some reason i checked my code and i cant find anything wrong

so if some one find what is wrong with my code pleas let me know

import org.jibble.pircbot.*;

import java.io.*;
import java.lang.System.*;

public class HyperBotZ extends PircBot {

// Get song title from Txt file AND return it!
public String getSong() throws Exception {
    FileReader file = new FileReader ("H:/Stream_Soft/Snip/Snip.txt");
    BufferedReader reader = new BufferedReader(file);

    String song = "";
    String line = reader.readLine();
    while (line != null){
        song += line;
        line = reader.readLine();
    }

    return song;
}

// Write info to txt file

// IRC Commands_
public HyperBotZ() {
    this.setName("HyperBotZ");
}

public static String ip = "";
public static String dual = "";

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

    String owner = "hypergainz";

    if (message.startsWith("!songrequest")) {
        sendMessage(channel, "Sorry i cant do that atm HyperGainZ need to learn me that, to see the current song use !song :D");
    }

    if (message.startsWith("!ip ")) {
        if(sender.equals(owner))
        {
            ip = message.split(" ")[1];
            sendMessage(channel, " the ip is set to " + ip);
        } 
    } else {
        if (message.equalsIgnoreCase("!ip")){
            sendMessage(channel, "HyperGainZ is currently playing on : " + ip );
        }
    }

    if (message.startsWith("!dual ")) {
        if(sender.equals(owner))
        {
            dual = message.split(" ")[1];
        } 
    } else {
        if (message.equalsIgnoreCase("!dual")){
            sendMessage(channel, "http://multitwitch.tv/hypergainz/" + dual );
        }
    }

    if (message.equalsIgnoreCase("!song")){
        String song = "";
        try {
            song = getSong();
        } catch (Exception e) { e.printStackTrace(); }
        sendMessage(channel, song);
    }
}

}

Nicolai
  • 133
  • 1
  • 8

1 Answers1

4

For a start, you should move your

String owner = "hypergainz";

into the class variables, since you don't need to set it every time a message is received.

Next, a good idea might be to break up the message into a String array, so that you can break the commands up from the (possible) arguments. You can do that with:

String[] messageArray = message.split(" ");

Once you have done that, you can compare the very first section of the message much easier.

If you are going to be using a few commands (up to about 10) I'd suggest using a switch to try and keep it tidy. For example:

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

    String[] messageArray = message.split(" ");
    String command = messageArray[0];

    switch(command){

    default:break;

    case: "!songrequest":
        sendMessage(channel, "Sorry i cant do that atm HyperGainZ need to learn me that, to see the current song use !song :D");
        return;

    case "!song":
        String song = "";
        try {
            song = getSong();
        } catch (Exception e) { e.printStackTrace(); }
        sendMessage(channel, song);
        return;

    }

However, if you are going to be making a (big) utility bot, creating your own distribution method might be a good idea, because that onMessage(...) method fills up very quickly.

C_Sto
  • 218
  • 1
  • 7
  • ok id did what you said and still nothing the only command that works is the "!song" one here is my code : http://pastebin.com/vxzrRK2P – Nicolai May 23 '14 at 17:57
  • I've just tested your code, and it works as expected... You will need to add commands to the switch if you want more than just the !song and !songrequest commands to work. What output are you expecting? – C_Sto May 24 '14 at 00:00
  • it works on a normal irc server but not on the twitch servers – Nicolai May 24 '14 at 13:39
  • are you getting any error messages returned from the server? if you put PircBot into verbose mode you should see all interactions printed in the terminal – C_Sto May 25 '14 at 01:16
  • no error message but when i do the songrequest command is see that the bot is trying to send it but nothing is receiving in the chat it self – Nicolai May 26 '14 at 15:00
  • 1
    Hmm, not sure. Could you update your original question with the terminal output of the working message and non working message? – C_Sto May 29 '14 at 01:40
  • sure but in the terminal its seem all good but when you go int to the chat room with a irc client no message is send ( so in terminal its saying that its send but in the client it wont show up ) – Nicolai May 29 '14 at 10:53