Vaerys_Dawn

Code in Question

Mar 15th, 2017
142
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.44 KB | None | 0 0
  1.      @Override
  2.     public String execute(String args, CommandObject command) {
  3.         for (CharacterObject c: command.characters.getCharacters()){
  4.             if (c.getUserID().equals(command.authorID)){
  5.                 List<IRole> userRoles = command.guild.getRolesForUser(command.author);
  6.                 int roleCount = 0;
  7.                 int postRoleCount =0;
  8.                 //resets User roles back to scratch.
  9.                 for (int i = 0; i < userRoles.size();i++){
  10.                     if (command.guildConfig.isRoleCosmetic(userRoles.get(i).getID())){
  11.                         userRoles.remove(i);
  12.                     } else if (userRoles.get(i).isEveryoneRole()) {
  13.                         userRoles.remove(i);
  14.                     } else if (userRoles.get(i) != null){
  15.                         roleCount++;
  16.                     }
  17.                 }
  18.                 //loads new roles.
  19.                 for (RoleTypeObject r: c.getRoles()){
  20.                     userRoles.add(command.guild.getRoleByID(r.getRoleID()));
  21.                 }
  22.                 for (IRole role: command.guild.getRolesForUser(command.author)){
  23.                     if (!command.guildConfig.isRoleCosmetic(role.getID())){
  24.                         postRoleCount++;
  25.                     }
  26.                 }
  27.                 //validates only if non cosmetic roles were not removed.
  28.                 logger.debug("Prior to Edit Role Count :" + roleCount);
  29.                 logger.debug("Post Edit Role Count :" + postRoleCount);
  30.                 if (postRoleCount >= roleCount){
  31.                     Utility.roleManagement(command.author,command.guild,userRoles);
  32.                     Utility.updateUserNickName(command.author,command.guild,c.getNickname());
  33.                     return "> Character " + c.getNickname() + " Loaded.";
  34.                 }else {
  35.                     return "> An error occurred while attempting to load your character.";
  36.                 }
  37.             }
  38.         }
  39.         return Constants.ERROR_CHAR_NOT_FOUND;
  40.     }
  41.  
  42.     public static RequestBuffer.RequestFuture<Boolean> roleManagement(IUser author, IGuild guild, List<IRole> userRoles) {
  43.         return RequestBuffer.request(() -> {
  44.             try {
  45.                 IRole[] roles = new IRole[userRoles.size()];
  46.                 int i = 0;
  47.                 for (IRole r : userRoles) {
  48.                     if (r == null) {
  49.                         logger.error("ROLE RETURNED NULL");
  50.                     }
  51.                     roles[i] = r;
  52.                     i++;
  53.                 }
  54.                 guild.editUserRoles(author, roles);
  55.             } catch (MissingPermissionsException e) {
  56.                 if (e.getMessage().contains("Edited roles hierarchy is too high.")) {
  57.                     logger.debug("Error Editing roles of user with id: " + author.getID() + " on guild with id: " + guild.getID() +
  58.                             ".\n" + Constants.PREFIX_EDT_LOGGER_INDENT + "Reason: Edited roles hierarchy is too high.");
  59.                     return true;
  60.                 } else {
  61.                     e.printStackTrace();
  62.                     return true;
  63.                 }
  64.             } catch (DiscordException e) {
  65.                 if (e.getMessage().contains("CloudFlare")) {
  66.                     roleManagement(author, guild, userRoles);
  67.                 } else {
  68.                     e.printStackTrace();
  69.                     return true;
  70.                 }
  71.             }
  72.             return false;
  73.         });
  74.     }
Advertisement
Add Comment
Please, Sign In to add comment