Advertisement
Vaerys_Dawn

Old vs new Command Handler

Aug 16th, 2017
143
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 8.02 KB | None | 0 0
  1. private boolean handleCommand(CommandObject command, String args) {
  2.         List<Command> commands;
  3.         IChannel currentChannel = command.channel.get();
  4.         String commandArgs;
  5.         if (command.channel.get().isPrivate()) {
  6.             commands = new ArrayList<>(Globals.getCommands(true));
  7.         } else {
  8.             commands = new ArrayList<>(Globals.getCommands(false));
  9.         }
  10.         for (Command c : commands) {
  11.             if (c.isCall(args, command)) {
  12.                 commandArgs = c.getArgs(args, command);
  13.                 //log command
  14.                 logger.debug(Utility.loggingFormatter(command));
  15.                 //test if user has permissions
  16.                 if (!Utility.testForPerms(c.perms(), command.user.get(), command.guild.get())) {
  17.                     Utility.sendMessage(command.user.notAllowed, currentChannel);
  18.                     return true;
  19.                 }
  20.                 //check if it is a valid channel
  21.                 if (c.channel() != null && !Utility.canBypass(command.user.get(), command.guild.get())) {
  22.                     List<IChannel> channels = command.guild.config.getChannelsByType(c.channel(), command.guild);
  23.                     if (!channels.contains(command.channel.get())) {
  24.                         List<String> list = Utility.getChannelMentions(Utility.getVisibleChannels(channels, command.user));
  25.                         if (list.size() == 0) {
  26.                             Utility.sendMessage("> You do not have access to any channels that you are able to run this command in.", currentChannel);
  27.                         } else if (list.size() > 1) {
  28.                             Utility.sendMessage("> Command must be performed in any of the following channels: \n" + Utility.listFormatter(list, true), currentChannel);
  29.                         } else {
  30.                             Utility.sendMessage("> Command must be performed in: " + list.get(0), currentChannel);
  31.                         }
  32.                         return true;
  33.                     }
  34.                 }
  35.                 if (c.requiresArgs() && commandArgs.isEmpty()) {
  36.                     Utility.sendMessage(Utility.getCommandInfo(c, command), currentChannel);
  37.                     return true;
  38.                 }
  39.                 //command logging
  40.                 handleLogging(command, c, commandArgs);
  41.                 if (!command.channel.get().getTypingStatus()) {
  42.                     command.channel.get().toggleTypingStatus();
  43.                 }
  44.                 try {
  45.                     Utility.sendMessage(c.execute(args, command), currentChannel);
  46.                 } catch (Exception e) {
  47.                     Utility.sendMessage("> I caught an Error, Please send this to my **Direct Messages** so my developer can look at it.\n```\n"
  48.                             + e.getClass().getName() + ": " + e.getMessage() + "```", currentChannel);
  49.                     Utility.sendStack(e);
  50.                 }
  51.             }
  52.         }
  53.         return false;
  54.        
  55.        
  56.         //Old Command Handler
  57.         IChannel channel = commandObject.channel.get();
  58.         GuildConfig guildconfig = commandObject.guild.config;
  59.         List<Command> commands = commandObject.guild.commands;
  60.         IDiscordClient client = commandObject.client.get();
  61.         for (Command c : commands) {
  62.             for (String name : c.names()) {
  63.                 if (command.equalsIgnoreCase(guildconfig.getPrefixCommand() + name)) {
  64.                     if (c.type().equals(Command.TYPE_CREATOR) && !commandObject.user.stringID.equalsIgnoreCase(Globals.creatorID)) {
  65.                         return false;
  66.                     }
  67.  
  68.                     logger.debug(Utility.loggingFormatter("COMMAND", command, args, commandObject));
  69.  
  70.                     //permissions checking
  71.                     if (c.perms().length != 0 && !Utility.canBypass(commandObject.user.get(), commandObject.guild.get())) {
  72.                         if (!Utility.testForPerms(c.perms(), commandObject.user.get(), commandObject.guild.get())) {
  73.                             Utility.sendMessage(commandObject.user.notAllowed, channel);
  74.                             return false;
  75.                         }
  76.                     }
  77.  
  78.                     //channel checking
  79.                     if (c.channel() != null && !Utility.canBypass(commandObject.user.get(), commandObject.guild.get())) {
  80.                         boolean channelFound = false;
  81.                         List<String> channelMentions = new ArrayList<>();
  82.                         for (ChannelSetting s : commandObject.guild.channelSettings) {
  83.                             if (s.type().equals(c.channel())) {
  84.                                 if (s.getIDs(commandObject.guild.config) != null) {
  85.                                     for (String id : s.getIDs(commandObject.guild.config)) {
  86.                                         if (id.equals(commandObject.channel.stringID)) {
  87.                                             channelFound = true;
  88.                                         }
  89.                                         IChannel testPerms = client.getChannelByID(id);
  90.                                         EnumSet<Permissions> userPerms = testPerms.getModifiedPermissions(commandObject.user.get());
  91.                                         if (userPerms.contains(Permissions.SEND_MESSAGES) && userPerms.contains(Permissions.READ_MESSAGES)) {
  92.                                             channelMentions.add(commandObject.client.get().getChannelByID(id).mention());
  93.                                         }
  94.                                     }
  95.                                 } else {
  96.                                     channelFound = true;
  97.                                 }
  98.                             }
  99.                         }
  100.                         //correct channel message formatting
  101.                         if (!channelFound) {
  102.                             if (channelMentions.size() == 0) {
  103.                                 Utility.sendMessage("> You do not have access to any channels that you are able to run this command in.", channel);
  104.                             } else if (channelMentions.size() > 1) {
  105.                                 Utility.sendMessage("> Command must be performed in any of the following channels: \n" + Utility.listFormatter(channelMentions, true), channel);
  106.                             } else {
  107.                                 Utility.sendMessage("> Command must be performed in: " + channelMentions.get(0), channel);
  108.                             }
  109.                             return channelFound;
  110.                         }
  111.                     }
  112.  
  113.                     //arguments checking.
  114.                     if (c.requiresArgs() && args.isEmpty()) {
  115.                         Utility.sendMessage(Utility.getCommandInfo(c, commandObject), channel);
  116.                         return false;
  117.                     }
  118.  
  119.                     //command logging
  120.                         if (c.doAdminLogging()) {
  121.                             if (guildconfig.adminLogging) {
  122.                                 handleLogging(commandObject, args, c, true);
  123.                             }
  124.                         } else {
  125.                             if (guildconfig.generalLogging) {
  126.                                 handleLogging(commandObject, args, c, false);
  127.                             }
  128.                         }
  129.                     if (!commandObject.channel.get().getTypingStatus()) {
  130.                         commandObject.channel.get().toggleTypingStatus();
  131.                     }
  132.                     try {
  133.                         Utility.sendMessage(c.execute(args, commandObject), channel);
  134.                     } catch (Exception e) {
  135.                         Utility.sendMessage("> I caught an Error, Please send this to my **Direct Messages** so my developer can look at it.\n```\n"
  136.                                 + e.getClass().getName() + ": " + e.getMessage() + "```", channel);
  137.                         Utility.sendStack(e);
  138.                     }
  139.                 }
  140.             }
  141.         }
  142.         return true;
  143.     }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement