Пример #1
0
  public static void registerCommandClass(Class clazz) {
    for (Method method : clazz.getDeclaredMethods()) {
      // Channel commands
      if (method.isAnnotationPresent(ChannelCommand.class)) {
        Annotation annotation = method.getAnnotation(ChannelCommand.class);
        ChannelCommand command = (ChannelCommand) annotation;
        commandMap.put(COMMAND_PREFIX + command.name(), method);
      }

      // Private message
      if (method.isAnnotationPresent(PrivateMessageCommand.class)) {
        Annotation annotation = method.getAnnotation(PrivateMessageCommand.class);
        PrivateMessageCommand command = (PrivateMessageCommand) annotation;
        privateMessageCommandMap.put(COMMAND_PREFIX + command.name(), method);
      }
    }
  }
Пример #2
0
  // Private message commands
  public void onPrivateMessage(PrivateMessageEvent event) throws Exception {
    String message = event.getMessage();
    String[] seperated;
    if (event.getMessage().contains(" ")) seperated = event.getMessage().split(" ");
    else seperated = new String[] {event.getMessage()};

    // If it's a command, let's execute it!
    if (privateMessageCommandMap.containsKey(seperated[0])) {
      String commandString = seperated[0];
      Method method = privateMessageCommandMap.get(commandString);
      PrivateMessageCommand command = method.getAnnotation(PrivateMessageCommand.class);

      // Build args
      String[] args = buildArgs(seperated);

      // Execute command. (null represents object to invoke on, since method is static no object)
      // Check permissions
      if (hasPermission(command.permissionLevel(), event.getUser(), null)) {
        if (args.length < command.minArgs()) {
          event.respond("Not enough arguements! Usage: " + command.usage());
        } else if (args.length > command.maxArgs()) {
          event.respond("Too many arguements! Usage: " + command.usage());
        } else {
          // Okay, everything's all good, lets run the command!
          method.invoke(null, event, args);
        }
      } else {
        event.respond("You do not have permission to execute this command!");
      }
    }
  }