PircBot Not Responding to Certain Commands in Twitch Chat

I’m developing a chat bot for my Twitch stream, but I’m facing a strange issue. It seems that only the !song command is responding, while all my other commands don’t seem to work. I’ve gone through my code multiple times but can’t identify the problem.

Here’s the code for my bot:

import org.jibble.pircbot.*;
import java.io.*;
import java.lang.System.*;

public class HyperBotZ extends PircBot {

    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;
    }

    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 can't do that right now. HyperGainZ needs 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);
        }
    }
}

Can anyone help me figure out what’s going wrong?

You’ve got two problems here. First, your conditional structure is broken, but there’s also a bigger issue - your getSong method opens FileReader and BufferedReader without closing them. That’ll cause memory leaks and eventually crash your bot. Mine went down after a few hours because of the same thing.

For the quick fix: change your if-else chains to independent if statements. With if-else, the second condition can’t be reached in some cases. Also wrap your file operations in try-with-resources blocks like try (BufferedReader reader = new BufferedReader(new FileReader(file))) - it’ll auto-handle cleanup and stop your bot from crashing during longer streams.

Your conditional logic is messed up. You’re chaining if-else statements wrong, so commands get skipped. When someone types !ip test, it hits the first condition but then jumps to the else block checking for just !ip. This means the second condition never runs properly. I ran into the same thing building my first Twitch bot with PircBot. Fix it by making each command a separate if statement instead of chaining them with else. Also, close your FileReader in getSong or you’ll get resource leaks - learned that when my bot started hanging after running too long.

Your if-else structure is broken for the !ip and !dual commands. When you run !ip something, it hits the first if block but then falls through to the else because the structure’s wrong. Same thing happens with !dual. Try using separate if statements instead, or fix how you’re chaining the conditions. I ran into this exact issue with my IRC bot - cleaning up the logic flow fixed everything.

your if-else chain is broken. when u run !ip test, it hits the first condition but then the else kicks in and skips the second check. u need separate if statements for each command.

hey, check your if conditions - they need to be separate if statements instead of chained. otherwise it’ll skip commands when it hits the else blocks. same thing happened with my bot at first!

This topic was automatically closed 4 days after the last reply. New replies are no longer allowed.