Search code examples
javairctwitchpircbot

How do I read Twitch chat with PircBotX?


I am trying to read messages from a twitch channel using PircBotX and Java but something doesn't seem to be working properly.

TwitchBot.java:

import org.pircbotx.Configuration;
import org.pircbotx.PircBotX;
import org.pircbotx.cap.EnableCapHandler;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class TwitchBot {
    
    private static Logger LOGGER = LoggerFactory.getLogger(TwitchBot.class);
    
    public static void main(String[] args) throws Exception {
        LOGGER.info("test");
        Configuration configuration = new Configuration.Builder()
                .setAutoNickChange(false)
                .setOnJoinWhoEnabled(false)
                .setCapEnabled(true)
                .addCapHandler(new EnableCapHandler("twitch.tv/membership"))
                .addServer("irc.chat.twitch.tv", 6697)
                .setServerPassword("oauth:*oauth token here*")
                .setName("*username here")
                .addAutoJoinChannel("#*channel name here*")
                .addListener(new TwitchChatListener())
                .buildConfiguration();

        PircBotX bot = new PircBotX(configuration);
        bot.startBot();
    }
}

TwitchChatListener.java:

import org.pircbotx.hooks.ListenerAdapter;
import org.pircbotx.hooks.events.MessageEvent;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class TwitchChatListener extends ListenerAdapter {
    
    private static Logger LOGGER = LoggerFactory.getLogger(TwitchChatListener.class);
    
    @Override
    public void onMessage(MessageEvent event) {
        System.out.println(event.getMessage());
        LOGGER.info(event.getMessage());
        /*if (event.getMessage().contains("test")) {
            System.out.println("Success!");
        }*/
    }
}

Console output:

[main] INFO org.pircbotx.PircBotX - ---Starting Connect attempt 1/5---
[main] INFO org.pircbotx.PircBotX - Connected to server.
[main] INFO org.pircbotx.output.OutputRaw -  pircbotx.output CAP LS
[main] INFO org.pircbotx.output.OutputRaw -  pircbotx.output PASS oauth:*oauth token here*
[main] INFO org.pircbotx.output.OutputRaw -  pircbotx.output NICK *username here*
[main] INFO org.pircbotx.output.OutputRaw -  pircbotx.output USER PircBotX 8 * :PircBotX 2.2 Java IRC bot - github.com/thelq/pircbotx

I censored out sensitive information with asterisks but the information I am putting in their place should be valid. As you can see by the console loge messages, it seems to be connecting to the server successfully but it's not reading the messages I send using Chatterino (I don't think I can access channel chat when offline from the Twitch website itself). I expect with the code that I have written that When I send any message, it should see that, get the message, and output it to the console but that is not happening. I am not sure where I went wrong with this as I am new to using stuff like IRC.

If you need any more information from me to help me out, please let me know. Thanks!


Solution

  • I managed to figure it out. I was attempting to connect to the SSL Twitch IRC server (port 6697) and that wasn't working for whatever reason but attempting to connect to the non-SSL Twitch IRC server (port 6667) makes everything work exactly as intended.