Juxhin
Juxhin

Reputation: 5600

IRC bot issue regarding channel retrieval

I would like to keep this short and simple for everyone. I've been working on a personal project just to learn something small of creating a relatively basic IRC bot using pIRC Java API.

Things were looking relatively well however I'm having(and have been having for days) issues in regards to obtaining current channel and current channel users.

The bot will only really support a single channel per instance for now but I would like to be able to display all current users on the left side of my frame as shown below:

enter image description here

The light blue is where I would like to display the users and the dark blue is where I would like to display channel messages

In PIRC you ought to call listChannels() method first, which in turn calls onChannelInfo() after every server it reads it returns (String channel, int userCount, String topic).

The issue is that I want to filter my channels only by channel name, thus @Overriding the current onChannelInfo is not enough.

If anyone has used pIRCBot before and knows what's going on please do help. I've been working on this issue for days and spent countless hours going back and fourth, nothing seemed to work.. Nothing ever returned and thus never appended on my JTextArea..

TL;DR - I would like to display users in current channel in left side and messages in right side but am having alot of issues with retrieving channel users and information!

Thanks a million in advance.

ALSO, here's the API for anyone who would like to look at - http://www.jibble.org/javadocs/pircbot/

Upvotes: 0

Views: 158

Answers (1)

Juxhin
Juxhin

Reputation: 5600

Well it's seems I've been able to fix the issue if anyone is connected.

For a start there was no need to call Pircbot.listChannels().

listChannels() automatically invokes onChannelInfo(String channel, int userCount, String topic) after each server is received.

However all that was needed is to getChannels() and then getUsers(), iterate through those users, get user.getNick() and save the String in a list, then iterate through the list and append the users.

Here is the method I used for it:

public void appendUsersConnectedToChannel(){
    List<String> users = new ArrayList<>();
    for(User i : Main.bot.getUsers(Main.bot.getChannel())){
        users.add(i.getNick());
    }
    for(String i : users){
        append(i + "\n");
    }
}

The question will obviously arise, "Why didn't you just directly append from i.getNick()? And my answer would have to be, I don't know. Whenever I tried to do that, it would never work and have no explanation for it.

Here is a more efficient way of writing(theoretically):

public void appendUsersConnectedToChannel(){
    for(User i : Main.bot.getUsers(Main.bot.getChannel())){
        append(i.getNick());
    }
}

Once again, this is using PircBot's API, which has been fairly fun to work with and would encourage anyone to try it, it's not flawless but it works very well!

Upvotes: 0

Related Questions