Vaerys_Dawn

Command Handler

Sep 25th, 2016
134
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 5.82 KB | None | 0 0
  1. private void handleCommand() {
  2.         boolean channelCorrect;
  3.         boolean permsCorrect;
  4.  
  5.         Method[] methods = this.getClass().getMethods();
  6.  
  7.         for (Method m : methods) {
  8.             if (m.isAnnotationPresent(CommandAnnotation.class)) {
  9.                 CommandAnnotation commandAnno = m.getAnnotation(CommandAnnotation.class);
  10.                 List<String> aliases = new ArrayList<>();
  11.                 if (m.isAnnotationPresent(AliasAnnotation.class)) {
  12.                     AliasAnnotation aliasAnno = m.getAnnotation(AliasAnnotation.class);
  13.                     aliases = new ArrayList<>(Arrays.asList(aliasAnno.alias()));
  14.                 }
  15.                 aliases.add(commandAnno.name());
  16.                 for (String s : aliases) {
  17.                     if ((Constants.PREFIX_COMMAND + s).equalsIgnoreCase(command)) {
  18.  
  19.                         //init files
  20.                         if (commandAnno.requiresArgs() && args.equals("")) {
  21.                             Utility.sendMessage("Command is missing Arguments: \n" + Utility.getCommandInfo(commandAnno), channel);
  22.                             return;
  23.                         }
  24.                         if (commandAnno.type().equals(Constants.TYPE_SERVERS) || commandAnno.type().equals(Constants.TYPE_ADMIN)) {
  25.                             loadServers();
  26.                         }
  27.                         if (commandAnno.type().equals(Constants.TYPE_ROLE_SELECT) || commandAnno.type().equals(Constants.TYPE_ADMIN)) {
  28.                             loadCharacters();
  29.                         }
  30.                         if (commandAnno.type().equals(Constants.TYPE_CC) || commandAnno.type().equals(Constants.TYPE_ADMIN)) {
  31.                             loadCC();
  32.                         }
  33.  
  34.                         //Logging Handling
  35.                         if (guildConfig.doLogging) {
  36.                             IChannel loggingChannel;
  37.                             if (commandAnno.doLogging() && guildConfig.getChannelTypeID(Constants.CHANNEL_ADMIN_LOG) != null) {
  38.                                 loggingChannel = guild.getChannelByID(guildConfig.getChannelTypeID(Constants.CHANNEL_ADMIN_LOG));
  39.                                 handleLogging(loggingChannel, commandAnno);
  40.                             } else if (guildConfig.getChannelTypeID(Constants.CHANNEL_SERVER_LOG) != null) {
  41.                                 loggingChannel = guild.getChannelByID(guildConfig.getChannelTypeID(Constants.CHANNEL_SERVER_LOG));
  42.                                 handleLogging(loggingChannel, commandAnno);
  43.                             }
  44.                         }
  45.  
  46.                         //Channel Validation
  47.                         if (!commandAnno.channel().equals(Constants.CHANNEL_ANY)) {
  48.                             if (guildConfig.getChannelTypeID(commandAnno.channel()) == null || channel.getID().equals(guildConfig.getChannelTypeID(commandAnno.channel()))) {
  49.                                 channelCorrect = true;
  50.                             } else channelCorrect = false;
  51.                         } else channelCorrect = true;
  52.                         if (guild.getOwner().equals(author) || author.getID().equals(Globals.creatorID)) {
  53.                             channelCorrect = true;
  54.                         }
  55.  
  56.                         //Permission Validation
  57.                         if (channelCorrect) {
  58.                             if (!Arrays.equals(commandAnno.perms(), new Permissions[]{Permissions.SEND_MESSAGES})) {
  59.                                 Permissions[] compiledPerms = new Permissions[commandAnno.perms().length];
  60.                                 int permsIndex = 0;
  61.                                 for (Permissions aP : commandAnno.perms()) {
  62.                                     for (IRole r : author.getRolesForGuild(guild)) {
  63.                                         for (Permissions p : r.getPermissions()) {
  64.                                             if (aP.equals(p)) {
  65.                                                 compiledPerms[permsIndex] = p;
  66.                                             }
  67.                                         }
  68.                                     }
  69.                                     permsIndex++;
  70.                                 }
  71.                                 if (Arrays.equals(compiledPerms, commandAnno.perms())) {
  72.                                     permsCorrect = true;
  73.                                 } else permsCorrect = false;
  74.                             } else permsCorrect = true;
  75.  
  76.                             //bypasses
  77.                             if (guild.getOwner().equals(author) || author.getID().equals(Globals.creatorID)) {
  78.                                 permsCorrect = true;
  79.                             }
  80.  
  81.                             //message sending
  82.                             if (channelCorrect && permsCorrect) {
  83.                                 try {
  84.                                     if (commandAnno.doResponseGeneral()) {
  85.                                         channel = guild.getChannelByID(guildConfig.getChannelTypeID(Constants.CHANNEL_GENERAL));
  86.                                     }
  87.                                     Utility.sendMessage((String) m.invoke(this), channel);
  88.                                 } catch (IllegalAccessException | InvocationTargetException e) {
  89.                                     e.printStackTrace();
  90.                                 }
  91.                             } else
  92.                                 Utility.sendMessage(noAllowed, channel);
  93.                         } else {
  94.                             Utility.sendMessage("> Command must be performed in the " + guild.getChannelByID(guildConfig.getChannelTypeID(commandAnno.channel())).mention() + " channel.", channel);
  95.                         }
  96.                         flushFiles();
  97.                     }
  98.                 }
  99.             }
  100.         }
  101.  
  102.     }
Advertisement
Add Comment
Please, Sign In to add comment