Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- @Override
- public String execute(String args, CommandObject command) {
- for (CharacterObject c : command.characters.getCharacters()) {
- if (c.getName().equalsIgnoreCase(args)) {
- if (c.getUserID().equals(command.authorID)) {
- List<IRole> userRoles = command.guild.getRolesForUser(command.author);
- int roleCount = 0;
- int postRoleCount = 0;
- //resets User roles back to scratch.
- for (int i = 0; i < userRoles.size(); i++) {
- if (command.guildConfig.isRoleCosmetic(userRoles.get(i).getID())) {
- userRoles.remove(i);
- } else if (userRoles.get(i) != null) {
- roleCount++;
- }
- }
- //loads new roles.
- for (RoleTypeObject r : c.getRoles()) {
- userRoles.add(command.guild.getRoleByID(r.getRoleID()));
- }
- for (IRole role : command.guild.getRolesForUser(command.author)) {
- if (!command.guildConfig.isRoleCosmetic(role.getID())) {
- postRoleCount++;
- }
- }
- //validates only if non cosmetic roles were not removed.
- logger.debug("Prior to Edit Role Count :" + roleCount);
- logger.debug("Post Edit Role Count :" + postRoleCount);
- if (postRoleCount >= roleCount) {
- Utility.roleManagement(command.author, command.guild, userRoles);
- Utility.updateUserNickName(command.author, command.guild, c.getNickname());
- return "> Character " + c.getNickname() + " Loaded.";
- } else {
- return "> An error occurred while attempting to load your character.";
- }
- } else {
- return "> " + c.getName() + " is not your character.";
- }
- }
- }
- return Constants.ERROR_CHAR_NOT_FOUND;
- }
- //UTILITY CODE
- public static RequestBuffer.RequestFuture<Boolean> roleManagement(IUser author, IGuild guild, List<IRole> userRoles) {
- return RequestBuffer.request(() -> {
- try {
- IRole[] roles = new IRole[userRoles.size()];
- int i = 0;
- for (IRole r : userRoles) {
- if (r == null) {
- logger.error("ROLE RETURNED NULL");
- }
- roles[i] = r;
- i++;
- }
- for (IRole r : roles){
- System.out.print(r.getName() + ", ");
- }
- System.out.print("\n");
- guild.editUserRoles(author, roles);
- System.out.println("this.");
- } catch (MissingPermissionsException e) {
- if (e.getMessage().contains("Edited roles hierarchy is too high.")) {
- logger.debug("Error Editing roles of user with id: " + author.getID() + " on guild with id: " + guild.getID() +
- ".\n" + Constants.PREFIX_EDT_LOGGER_INDENT + "Reason: Edited roles hierarchy is too high.");
- return true;
- } else {
- e.printStackTrace();
- return true;
- }
- } catch (DiscordException e) {
- if (e.getMessage().contains("CloudFlare")) {
- roleManagement(author, guild, userRoles);
- } else {
- e.printStackTrace();
- return true;
- }
- } catch (RuntimeException e){
- e.printStackTrace();
- }
- return false;
- });
- }
- public static RequestBuffer.RequestFuture<Boolean> updateUserNickName(IUser author, IGuild guild, String nickname) {
- return RequestBuffer.request(() -> {
- try {
- guild.setUserNickname(author, nickname);
- } catch (MissingPermissionsException e) {
- if (e.getMessage().toLowerCase().contains("hierarchy")) {
- logger.debug("Could not Update Nickname. User's position in hierarchy is higher than mine.");
- } else {
- e.printStackTrace();
- }
- return true;
- } catch (DiscordException e) {
- if (e.getMessage().contains("CloudFlare")) {
- updateUserNickName(author, guild, nickname);
- } else {
- e.printStackTrace();
- return true;
- }
- }
- return false;
- });
- }
Advertisement
Add Comment
Please, Sign In to add comment