Search code examples
javairc

PIRCBOT - How to add voice to all users in the channel - Java


I'm using the pircbot to make an IRC bot. How do I make the bot add voice to all the users in the channel? Or when the users join?


Solution

  • Probably something like this:

    import org.jibble.pircbot.*;
    
    public class MyBot extends PircBot {
    
      public MyBot() {
        this.setName("MyBot");
      }
    
      public void onJoin(String channel, String sender,
                       String login, String hostname, String message) {
        this.voice(channel, sender);
      }
    
      public void voiceAll(String channel) {
        int i = 0;
        User[] users = this.getUsers(channel);
        while (i < users.length)
          this.voice(channel, users[i++].getNick());
      }
    }
    
    public class MyBotMain {
    
      public static void main(String[] args) throws Exception {
        MyBot bot = new MyBot();
        bot.connect("irc.freenode.net");
        bot.joinChannel("#chan");
        bot.voiceAll("#chan"); 
      }
    
    }
    

    You should verify some stuff before voicing (are you in the chan? are you operator?). Take a look at the API page: http://www.jibble.org/javadocs/pircbot/index.html.