/** * Invokes the method of the command. * * @param command The command. * @param event The event. * @param parameters The parameters for the method. */ private void invokeMethod(SimpleCommand command, MessageReceivedEvent event, Object[] parameters) { Method method = command.getMethod(); Object reply = null; try { method.setAccessible(true); reply = method.invoke(command.getExecutor(), parameters); } catch (IllegalAccessException | InvocationTargetException e) { Discord4J.LOGGER.warn("Cannot invoke method {}!", method.getName(), e); } if (reply != null) { try { event.getMessage().getChannel().sendMessage(String.valueOf(reply)); } catch (MissingPermissionsException | RateLimitException | DiscordException ignored) { } } }
/** * Registers an executor. * * @param executor The executor to register. */ public void registerCommand(CommandExecutor executor) { for (Method method : executor.getClass().getMethods()) { Command annotation = method.getAnnotation(Command.class); if (annotation == null) { continue; } if (annotation.aliases().length == 0) { throw new IllegalArgumentException("Aliases array cannot be empty!"); } SimpleCommand command = new SimpleCommand(annotation, method, executor); for (String alias : annotation.aliases()) { // add command to map. It's faster to access it from the map than iterating to the whole list commands.put(defaultPrefix + alias.toLowerCase().replace(" ", ""), command); } // we need a list, too, because a HashMap is not ordered. commandList.add(command); } }
Message message = event.getMessage(); String[] args = Arrays.copyOfRange(splitMessage, 1, splitMessage.length); Class<?>[] parameterTypes = command.getMethod().getParameterTypes(); final Object[] parameters = new Object[parameterTypes.length]; int stringCounter = 0;
Class<?>[] parameterTypes = command.getMethod().getParameterTypes(); final Object[] parameters = new Object[parameterTypes.length]; int stringCounter = 0;
if (command == null || !command.getCommandAnnotation().requiresMention()) { return; Command commandAnnotation = command.getCommandAnnotation(); if (commandAnnotation.requiresMention()) { Matcher matcher = USER_MENTION.matcher(commandString);
if (command == null || !command.getCommandAnnotation().requiresMention()) { return; Command commandAnnotation = command.getCommandAnnotation(); if (commandAnnotation.requiresMention()) { Matcher matcher = USER_MENTION.matcher(commandString);
if (command == null || !command.getCommandAnnotation().requiresMention()) { return; Command commandAnnotation = command.getCommandAnnotation(); if (commandAnnotation.requiresMention()) { Matcher matcher = USER_MENTION.matcher(commandString);
Class<?>[] parameterTypes = command.getMethod().getParameterTypes(); final Object[] parameters = new Object[parameterTypes.length]; int stringCounter = 0;
/** * Invokes the method of the command. * * @param command The command. * @param event The event. * @param parameters The parameters for the method. */ private void invokeMethod(SimpleCommand command, MessageReceivedEvent event, Object[] parameters) { Method method = command.getMethod(); Object reply = null; try { method.setAccessible(true); reply = method.invoke(command.getExecutor(), parameters); } catch (IllegalAccessException | InvocationTargetException e) { logger.warn("An error occurred while invoking method {}!", method.getName(), e); } if (reply != null) { event.getChannel().sendMessage(String.valueOf(reply)).queue(); } }
/** * Invokes the method of the command. * * @param command The command. * @param message The original message. * @param parameters The parameters for the method. */ private void invokeMethod(SimpleCommand command, Message message, Object[] parameters) { Method method = command.getMethod(); Object reply = null; try { method.setAccessible(true); reply = method.invoke(command.getExecutor(), parameters); } catch (Exception e) { logger.warn("An error occurred while invoking method {}!", method.getName(), e); } if (reply != null) { message.getChannel().sendMessage(String.valueOf(reply)); } }