/** * Checks if the user with the given id has the required permission. * * @param userId The id of the user. * @param permission The permission to check. * @return If the user has the given permission. */ public boolean hasPermission(String userId, String permission) { if (permission.equals("none") || permission.equals("")) { return true; } List<String> permissions = this.permissions.get(userId); if (permissions == null) { return false; } for (String perm : permissions) { // user has the permission if (checkPermission(perm, permission)) { return true; } } // user hasn't enough permissions return false; }
/** * 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); } }
/** * 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)); } }
if (command == null || !command.getCommandAnnotation().requiresMention()) { return; Command commandAnnotation = command.getCommandAnnotation(); if (commandAnnotation.requiresMention()) { Matcher matcher = USER_MENTION.matcher(commandString); if (!matcher.find() || !matcher.group("id").equals(jda.getSelfUser().getId())) { if (event.isFromType(ChannelType.PRIVATE) && !commandAnnotation.privateMessages()) { return; if (!event.isFromType(ChannelType.PRIVATE) && !commandAnnotation.channelMessages()) { return; if (!hasPermission(event.getAuthor(), commandAnnotation.requiredPermissions())) { if (Sdcf4jMessage.MISSING_PERMISSIONS.getMessage() != null) { event.getChannel().sendMessage(Sdcf4jMessage.MISSING_PERMISSIONS.getMessage()).queue(); final Object[] parameters = getParameters(splitMessage, command, event); if (commandAnnotation.async()) { final SimpleCommand commandFinal = command; Thread t = new Thread(() -> invokeMethod(commandFinal, event, parameters)); t.setDaemon(true); t.start(); } else { invokeMethod(command, event, parameters);
if (command == null || !command.getCommandAnnotation().requiresMention()) { return; Command commandAnnotation = command.getCommandAnnotation(); if (commandAnnotation.requiresMention()) { Matcher matcher = USER_MENTION.matcher(commandString); if (!matcher.find() || !matcher.group("id").equals(api.getYourself().getIdAsString())) { if (message.getPrivateChannel().isPresent() && !commandAnnotation.privateMessages()) { return; if (!message.getPrivateChannel().isPresent() && !commandAnnotation.channelMessages()) { return; if (!hasPermission(message.getUserAuthor().map(User::getId).map(String::valueOf).orElse("-1"), commandAnnotation.requiredPermissions())) { if (Sdcf4jMessage.MISSING_PERMISSIONS.getMessage() != null) { message.getChannel().sendMessage(Sdcf4jMessage.MISSING_PERMISSIONS.getMessage()); final Object[] parameters = getParameters(splitMessage, command, event, api); if (commandAnnotation.async()) { final SimpleCommand commandFinal = command; api.getThreadPool().getExecutorService().submit(() -> invokeMethod(commandFinal, message, parameters)); } else { invokeMethod(command, message, parameters);
if (command == null || !command.getCommandAnnotation().requiresMention()) { return; Command commandAnnotation = command.getCommandAnnotation(); if (commandAnnotation.requiresMention()) { Matcher matcher = USER_MENTION.matcher(commandString); if (!matcher.find() || !matcher.group("id").equals(event.getClient().getOurUser().getStringID())) { if (event.getMessage().getChannel().isPrivate() && !commandAnnotation.privateMessages()) { return; if (!event.getMessage().getChannel().isPrivate() && !commandAnnotation.channelMessages()) { return; if (!hasPermission(event.getMessage().getAuthor(), commandAnnotation.requiredPermissions())) { if (Sdcf4jMessage.MISSING_PERMISSIONS.getMessage() != null) { try { event.getMessage().getChannel().sendMessage(Sdcf4jMessage.MISSING_PERMISSIONS.getMessage()); } catch (MissingPermissionsException | RateLimitException | DiscordException ignored) { } final Object[] parameters = getParameters(splitMessage, command, event); if (commandAnnotation.async()) { final SimpleCommand commandFinal = command; Thread t = new Thread(() -> { invokeMethod(commandFinal, event, parameters); }); t.setDaemon(true); t.start(); } else {
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; parameters[i] = message.getServerTextChannel().map(ServerTextChannel::getServer).orElse(null); } else if (type == Object[].class) { parameters[i] = getObjectsFromString(api, args); } else {
Class<?>[] parameterTypes = command.getMethod().getParameterTypes(); final Object[] parameters = new Object[parameterTypes.length]; int stringCounter = 0; parameters[i] = event.getResponseNumber(); } else if (type == Object[].class) { parameters[i] = getObjectsFromString(event.getJDA(), args); } else {
Class<?>[] parameterTypes = command.getMethod().getParameterTypes(); final Object[] parameters = new Object[parameterTypes.length]; int stringCounter = 0; parameters[i] = event.getMessage().getChannel().getGuild(); } else if (type == Object[].class) { parameters[i] = getObjectsFromString(event.getClient(), args); } else {
/** * 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 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) { } } }