Vaerys_Dawn

Why is this happening

Mar 16th, 2017
191
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 4.87 KB | None | 0 0
  1. @Override
  2.     public String execute(String args, CommandObject command) {
  3.         for (CharacterObject c : command.characters.getCharacters()) {
  4.             if (c.getName().equalsIgnoreCase(args)) {
  5.                 if (c.getUserID().equals(command.authorID)) {
  6.                     List<IRole> userRoles = command.guild.getRolesForUser(command.author);
  7.                     int roleCount = 0;
  8.                     int postRoleCount = 0;
  9.                     //resets User roles back to scratch.
  10.                     for (int i = 0; i < userRoles.size(); i++) {
  11.                         if (command.guildConfig.isRoleCosmetic(userRoles.get(i).getID())) {
  12.                             userRoles.remove(i);
  13.                         } else if (userRoles.get(i) != null) {
  14.                             roleCount++;
  15.                         }
  16.                     }
  17.                     //loads new roles.
  18.                     for (RoleTypeObject r : c.getRoles()) {
  19.                         userRoles.add(command.guild.getRoleByID(r.getRoleID()));
  20.                     }
  21.                     for (IRole role : command.guild.getRolesForUser(command.author)) {
  22.                         if (!command.guildConfig.isRoleCosmetic(role.getID())) {
  23.                             postRoleCount++;
  24.                         }
  25.                     }
  26.                     //validates only if non cosmetic roles were not removed.
  27.                     logger.debug("Prior to Edit Role Count :" + roleCount);
  28.                     logger.debug("Post Edit Role Count :" + postRoleCount);
  29.                     if (postRoleCount >= roleCount) {
  30.                         Utility.roleManagement(command.author, command.guild, userRoles);
  31.                         Utility.updateUserNickName(command.author, command.guild, c.getNickname());
  32.                         return "> Character " + c.getNickname() + " Loaded.";
  33.                     } else {
  34.                         return "> An error occurred while attempting to load your character.";
  35.                     }
  36.                 } else {
  37.                     return "> " + c.getName() + " is not your character.";
  38.                 }
  39.             }
  40.         }
  41.         return Constants.ERROR_CHAR_NOT_FOUND;
  42.     }
  43.  
  44. //UTILITY CODE
  45.  
  46. public static RequestBuffer.RequestFuture<Boolean> roleManagement(IUser author, IGuild guild, List<IRole> userRoles) {
  47.         return RequestBuffer.request(() -> {
  48.             try {
  49.                 IRole[] roles = new IRole[userRoles.size()];
  50.                 int i = 0;
  51.                 for (IRole r : userRoles) {
  52.                     if (r == null) {
  53.                         logger.error("ROLE RETURNED NULL");
  54.                     }
  55.                     roles[i] = r;
  56.                     i++;
  57.                 }
  58.                 for (IRole r : roles){
  59.                     System.out.print(r.getName() + ", ");
  60.                 }
  61.                 System.out.print("\n");
  62.                 guild.editUserRoles(author, roles);
  63.  
  64.                 System.out.println("this.");
  65.             } catch (MissingPermissionsException e) {
  66.                 if (e.getMessage().contains("Edited roles hierarchy is too high.")) {
  67.                     logger.debug("Error Editing roles of user with id: " + author.getID() + " on guild with id: " + guild.getID() +
  68.                             ".\n" + Constants.PREFIX_EDT_LOGGER_INDENT + "Reason: Edited roles hierarchy is too high.");
  69.                     return true;
  70.                 } else {
  71.                     e.printStackTrace();
  72.                     return true;
  73.                 }
  74.             } catch (DiscordException e) {
  75.                 if (e.getMessage().contains("CloudFlare")) {
  76.                     roleManagement(author, guild, userRoles);
  77.                 } else {
  78.                     e.printStackTrace();
  79.                     return true;
  80.                 }
  81.             } catch (RuntimeException e){
  82.                 e.printStackTrace();
  83.             }
  84.             return false;
  85.         });
  86.     }
  87.  
  88. public static RequestBuffer.RequestFuture<Boolean> updateUserNickName(IUser author, IGuild guild, String nickname) {
  89.         return RequestBuffer.request(() -> {
  90.             try {
  91.                 guild.setUserNickname(author, nickname);
  92.             } catch (MissingPermissionsException e) {
  93.                 if (e.getMessage().toLowerCase().contains("hierarchy")) {
  94.                     logger.debug("Could not Update Nickname. User's position in hierarchy is higher than mine.");
  95.                 } else {
  96.                     e.printStackTrace();
  97.                 }
  98.                 return true;
  99.             } catch (DiscordException e) {
  100.                 if (e.getMessage().contains("CloudFlare")) {
  101.                     updateUserNickName(author, guild, nickname);
  102.                 } else {
  103.                     e.printStackTrace();
  104.                     return true;
  105.                 }
  106.             }
  107.             return false;
  108.         });
  109.     }
Advertisement
Add Comment
Please, Sign In to add comment