コード例 #1
0
ファイル: HistoryTest.java プロジェクト: Jimtrim/OnlineGuru
  @Test
  public void testLastActionByRockj() {
    Event event = history.getLastEvents(rockj).get(0);
    assertEquals((PrivMsgEvent) event, event);
    PrivMsgEvent privMsgEvent = (PrivMsgEvent) event;

    assertEquals("This is a trolling message, what's up?!", privMsgEvent.getMessage());
    assertEquals(2, history.getLastEvents(rockj).size());
  }
コード例 #2
0
  /*
   * Event handling
   */
  private void handleMessage(Event e) {
    PrivMsgEvent pme = (PrivMsgEvent) e;
    String message = pme.getMessage();

    // Process the trigger
    if (message.split(" ")[0].equals("!help")
        || message.split(" ")[0].equals("!hjelp")
        || message.split(" ")[0].equals("??")) {
      String sender = pme.getSender();
      Network network = pme.getNetwork();

      Set<Flag> userFlags;

      if (flagsPlugin == null) {
        // If this dependency isn't loaded properly, fallback to show only triggers that anyone can
        // see
        logger.debug("Missing dependency; FlagsPlugin. Showing basic triggers.");
        userFlags = new HashSet<Flag>();
        userFlags.add(Flag.ANYONE);
      }
      // Fetch flags for the user
      else {
        if (pme.isChannelMessage()) {
          userFlags = flagsPlugin.getFlags(network, pme.getChannel(), sender);
        } else {
          userFlags = flagsPlugin.getFlags(network, sender);
        }
      }

      // Remove the first word
      String helpTrigger = message.replaceAll("^[^\\s]+\\s?", "");

      // If there is no argument other than the help trigger, display all the triggers
      if (helpTrigger.isEmpty()) {

        wand.sendMessageToTarget(
            network,
            sender,
            "Here is a list of available triggers, use !help <trigger> for more info;");

        String output = "";

        for (HelpItem helpItem : helpItems) {
          if (userFlags.contains(helpItem.getFlagRequired())) {
            if (!output.isEmpty()) {
              output += ", ";
            }
            output += helpItem.getTrigger();

            // If length of the current output is more than 100 chars, send it
            if (output.length() > 100) {
              wand.sendMessageToTarget(network, sender, output);
              output = "";
            }
          }
        }

        // Send the rest if there's anything
        if (!output.isEmpty()) {
          wand.sendMessageToTarget(network, sender, output);
        }
      }
      // If there are more words, display help text for the supplied trigger
      else {
        boolean found = false;

        for (HelpItem helpItem : helpItems) {
          if (helpItem.getTrigger().equals(helpTrigger)) {
            if (userFlags.contains(helpItem.getFlagRequired())) {
              for (String helpText : helpItem.getHelpText()) {
                wand.sendMessageToTarget(network, sender, helpText);
              }
            } else {
              wand.sendMessageToTarget(
                  network, sender, "You do not have the flag required to use that command.");
            }

            found = true;
          }
        }

        // If no matching and allowed trigger
        if (!found) {
          wand.sendMessageToTarget(
              network, sender, String.format("No help item for '%s'", helpTrigger));
        }
      }
    }
  }