First,
twitch.tv doesn't use a standard IRC server and instead wrote their own non-standard, broken version. They don't support CAP LS which breaks PircBotX's CAP handling but they do support CAP REQ :
twitch.tv/tags. Add this to your listener
@Override
public void onServerResponse(ServerResponseEvent event) throws Exception {
if(event.getCode() == 376) {
event.getBot().sendRaw().rawLineNow("CAP REQ :
twitch.tv/tags");
}
}
Now every message is dispatched as an UnknownEvent as its not parsable anymore. It will be easier if I implement it in the parser cause I have to add tests to make sure it doesn't break anything. However until then, here's a basic implementation you can add to your listener:
@Override
public void onUnknown(UnknownEvent event) throws Exception {
String raw = event.getLine();
if(!raw.contains("PRIVMSG"))
return;
String[] rawParts = StringUtils.split(raw, " ", 5);
//Parse tags, starts with @
String tagsRaw = rawParts[0].substring(1);
ImmutableMap.Builder<String, String> tagsBuilder = ImmutableMap.builder();
for(String entry : StringUtils.split(tagsRaw, ";")) {
String[] entryParts = StringUtils.split(entry, "=");
String key = entryParts[0];
String value = entryParts.length > 1 ? entryParts[1] : "";
tagsBuilder.put(key, value);
}
UserChannelDao<User, Channel> userChannelDao = event.getBot().getUserChannelDao();
//Twitch doesn't support WHO and NAMES only has the ops, also starts with a colon
String userRaw = rawParts[1].substring(1);
UserHostmask user = event.getBot().getConfiguration().getBotFactory().createUserHostmask(event.getBot(), userRaw);
//Channel (skip PRIVMSG)
Channel channel = userChannelDao.getChannel(rawParts[3]);
//Message, removing colon prefix
String message = rawParts[4].substring(1);
onTwitchMessage(tagsBuilder.build(), user, channel, message);
}
private void onTwitchMessage(ImmutableMap<String, String> tags, UserHostmask user, Channel channel, String message) {
log.debug("Got message tags {} user {} channel {} message {}", tags, user, channel, message);
}
INFO org.pircbotx.InputParser - @color=;display-name=TheLQ2;emotes=;subscriber=0;turbo=0;user-type=;user_type= :thelq2!
the...@thelq2.tmi.twitch.tv PRIVMSG #thelq2 :test
DEBUG org.pircbotx.impl.PircBotXExample - Got message tags {color=, display-name=TheLQ2, emotes=, subscriber=0, turbo=0, user-type=, user_type=} user UserHostmask(extbanPrefix=null, nick=thelq2, login=thelq2, hostname=
thelq2.tmi.twitch.tv) channel Channel(name=#thelq2, channelId=ab86e0eb-e54d-4fa5-9762-8238589a5c9c, ...) message test