Not recieving a Viewerlist from Twitch

Hello people,
First of all i want to point out that I am fairly new to Java and programming so I am not really experiended with these kind of projects.
Anyway,latley I have been playing around with creating an Twitch IRC Bot made with Java and the PircBot Libaray.
I can sucessfully connect to the chatroom in my channel and also send message if someone for example types commands like “!time.” in the chat.
I want to improve the bot with adding a Point System for the viewers.The Problem I have is, i cant get a correct Viewerlist from my channel. I using a testaccount and my twitchbot so i should get back a String array of 1 in theory but my array is always empty.

This is my code to recieve the viewerlist:

public String[] getUsersAsStringArray(String channel) {
User[] userArray = getUsers(channel);
int mLength = userArray.length;
String[] users = new String[mLength];
for (int i = 0; i < mLength; i++) {
users[i] = userArray[i].getPrefix() + userArray[i].getNick();
users[i]=users[i]+userArray[i].getNick();
System.out.println(users[i]);
}
System.out.println(mLength+" length of array");

    return users;
}

I cant figure out what I am doing wrong maybe you can help me would be very nice:)

You would need to post the getUsers() function block before someone could attempt to help, as that function appears to be filling the userArray with the users in the first place.

On IRC clients the viewer list is populated from /NAMES and JOIN/PART messages. TMI does JOIN/PART messages in a batch every 30 seconds or so.

To get membership state event (NAMES, JOIN, PART, or MODE) functionality. You need to execute:

CAP REQ :twitch.tv/membership

upon connect.

/NAMES is only executed on your join to the channel, but will not list everyone if the channel is large(1000+) in which case it will only populate the moderators and streamer.

You could try polling the unofficial/unsupported TMI API, https://tmi.twitch.tv/group/user/:CHANNEL:/chatters to get the information you need.

Also, you may want to look at: Pircbot, problems with retrieving a full userList. Java

1 Like

Like @matt_thomas said, we would need to see GetUsers() to see exactly how you are attempting to get the users.

Also, the official API does not natively support a way to get a viewer list through an endpoint. You get a list of all current viewers when you join a channel and membership is requested on login. However, this only happens one time. Furthermore, when a channel has over 1000 viewers it will only return a list of viewers who have op status.

Another way would be to monitor JOIN and PART with membership requested at login. However, this is also unreliable because at random times it will say a person has left a chat room when they actually didn’t leave, only to be added back after a few minutes. These notices are also only sent once every few minutes in general, so you could be awarding/not awarding points to the correct people.

The only other option would be to use third party API’s like @matt_thomas said. However since these are third party, it would be up to the independent developer to maintain the API and could stop working at any point in time.

First of all thank you all for your help I really appreciate it.

Well, I am obviously using the GetUsers method from the Pircbot library:

public final User[] getUsers(String channel) {
channel = channel.toLowerCase();
User[] userArray = new User[0];
synchronized (_channels) {
Hashtable users = (Hashtable) _channels.get(channel);
if (users != null) {
userArray = new User[users.size()];
Enumeration enumeration = users.elements();
for (int i = 0; i < userArray.length; i++) {
User user = (User) enumeration.nextElement();
userArray[i] = user;
}
}
}
return userArray;
}

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