/** Away command */ private void cmdAway(MessageEvent me) { boolean found = false; for (String nick : awayList) { if (me.getNick().equals(nick)) { found = true; } } if (!found) { awayList.add(me.getNick()); me.getChannel().say(me.getNick() + " is now tagged as 'away'."); } }
/** Language checker */ private void checkLanguage(MessageEvent me, User user) { String msg = me.getMessage().toLowerCase(); int count = 0; for (String word : msg.split(" ")) { for (String badword : data.badWords) { if (word.toLowerCase().equals(badword)) { count++; } } } if (count > 0) { user.warnings++; if (user.warnings == 1) { me.getChannel().say(me.getNick() + ": please watch your language!"); } else if (user.warnings >= 2) { me.getChannel().kick(me.getNick(), "Language"); } } }
/** Away checker */ private void checkAway(MessageEvent me) { String msg = me.getMessage().toLowerCase(); for (int i = 0; i < awayList.size(); i++) { String nick = awayList.get(i); // Check if msg sender is nick if (nick.equals(me.getNick())) { for (int j = 0; j < data.notices.size(); j++) { Notice n = data.notices.get(j); if (n.to.toLowerCase().equals(me.getNick().toLowerCase())) { me.getChannel().say(me.getNick() + ", " + n.from + " says: " + n.msg); data.notices.remove(n); } } awayList.remove(nick); } // Check if nick is contained in msg if (msg.contains(nick.toLowerCase())) { me.getChannel().say(nick + " is currently away."); } } }
/** Notice command */ private void cmdNotice(MessageEvent me) { String msg = me.getMessage(); int start = msg.indexOf(' '); if (start == -1) { me.getChannel().say("There are " + data.notices.size() + " notice(s) waiting!"); return; } int end = msg.indexOf(' ', start + 1); if (end == -1) { me.getChannel().say("Wrong syntax!"); return; } Notice n = new Notice(); n.to = msg.substring(start + 1, end); if (getTrueName(me.getChannel(), n.to) != null && !isAway(n.to)) { me.getChannel().say(getTrueName(me.getChannel(), n.to) + " is present and not away!"); return; } n.from = me.getNick(); n.msg = msg.substring(end + 1); data.notices.add(n); me.getChannel().say("Notice added!"); }
@Override /** Callback whenever the Bot receives data. */ public void receiveEvent(IRCEvent e) { Type type = e.getType(); // Connects to server successfully if (type == Type.CONNECT_COMPLETE) { e.getSession().join(data.channel); // Connects to channel successfully } else if (type == Type.JOIN_COMPLETE) { channel = ((JoinCompleteEvent) e).getChannel(); e.getSession().sayPrivate("nickserv", "identify " + data.botPassword); // User joins channel } else if (type == Type.JOIN) { JoinEvent je = (JoinEvent) e; int x = new Random().nextInt(data.greetings.size()); je.getChannel().say(String.format(data.greetings.get(x), je.getNick())); for (int i = 0; i < data.notices.size(); i++) { Notice n = data.notices.get(i); if (n.to.toLowerCase().equals(je.getNick().toLowerCase())) { je.getChannel().say(je.getNick() + ", " + n.from + " says: " + n.msg); data.notices.remove(n); } } // User leaves channel } else if (type == Type.QUIT) { QuitEvent qe = (QuitEvent) e; users.remove(qe.getNick()); // Receives message in channel } else if (type == Type.CHANNEL_MESSAGE) { MessageEvent me = (MessageEvent) e; String msg = me.getMessage(); User user = loadUser(me.getNick()); boolean found = true; if (isCommand(msg)) { if (isAdmin(me.getNick())) { String cmd = getCommand(msg); if (cmd.equals("help")) cmdHelp(me); else if (cmd.equals("kick")) cmdKick(me); else if (cmd.equals("admins")) cmdAdmins(me); else if (cmd.equals("roll")) cmdRoll(me); else if (cmd.equals("announcer")) cmdAnnounce(me); else if (cmd.equals("badwords")) cmdBadWord(me); else if (cmd.equals("greetings")) cmdGreetings(me); else if (cmd.equals("quit")) cmdQuit(); else if (cmd.equals("save")) cmdSave(); else if (cmd.equals("notice")) cmdNotice(me); else if (cmd.equals("kill")) cmdKill(me); else if (cmd.equals("time")) cmdTime(me); else found = false; } String cmd = getCommand(msg); if (cmd.equals("away")) cmdAway(me); else if (!found) me.getChannel().say("Unknown command."); } if (me.getMessage().contains("lol")) { me.getChannel().say("lol"); } else { checkLanguage(me, user); checkAway(me); } } }