diff --git a/src/main/java/net/dv8tion/jda/internal/entities/RoleImpl.java b/src/main/java/net/dv8tion/jda/internal/entities/RoleImpl.java index 0828392c57..550d838a87 100644 --- a/src/main/java/net/dv8tion/jda/internal/entities/RoleImpl.java +++ b/src/main/java/net/dv8tion/jda/internal/entities/RoleImpl.java @@ -60,6 +60,7 @@ public class RoleImpl implements Role private long rawPermissions; private int color; private int rawPosition; + private int frozenPosition = Integer.MIN_VALUE; // this is used exclusively for delete events private RoleIcon icon; public RoleImpl(long id, Guild guild) @@ -73,6 +74,8 @@ public RoleImpl(long id, Guild guild) @Override public int getPosition() { + if (frozenPosition > Integer.MIN_VALUE) + return frozenPosition; Guild guild = getGuild(); if (equals(guild.getPublicRole())) return -1; @@ -458,6 +461,11 @@ public RoleImpl setIcon(RoleIcon icon) return this; } + public void freezePosition() + { + this.frozenPosition = getPosition(); + } + public static class RoleTagsImpl implements RoleTags { public static final RoleTags EMPTY = new RoleTagsImpl(); diff --git a/src/main/java/net/dv8tion/jda/internal/handle/GuildRoleDeleteHandler.java b/src/main/java/net/dv8tion/jda/internal/handle/GuildRoleDeleteHandler.java index 8f3c79d863..0458f364be 100644 --- a/src/main/java/net/dv8tion/jda/internal/handle/GuildRoleDeleteHandler.java +++ b/src/main/java/net/dv8tion/jda/internal/handle/GuildRoleDeleteHandler.java @@ -16,13 +16,13 @@ package net.dv8tion.jda.internal.handle; import net.dv8tion.jda.api.entities.Emote; -import net.dv8tion.jda.api.entities.Role; import net.dv8tion.jda.api.events.role.RoleDeleteEvent; import net.dv8tion.jda.api.utils.data.DataObject; import net.dv8tion.jda.internal.JDAImpl; import net.dv8tion.jda.internal.entities.EmoteImpl; import net.dv8tion.jda.internal.entities.GuildImpl; import net.dv8tion.jda.internal.entities.MemberImpl; +import net.dv8tion.jda.internal.entities.RoleImpl; import net.dv8tion.jda.internal.requests.WebSocketClient; public class GuildRoleDeleteHandler extends SocketHandler @@ -48,7 +48,7 @@ protected Long handleInternally(DataObject content) } final long roleId = content.getLong("role_id"); - Role removedRole = guild.getRolesView().remove(roleId); + RoleImpl removedRole = (RoleImpl) guild.getRolesView().get(roleId); if (removedRole == null) { //getJDA().getEventCache().cache(EventCache.Type.ROLE, roleId, () -> handle(responseNumber, allContent)); @@ -56,6 +56,10 @@ protected Long handleInternally(DataObject content) return null; } + //Allow for position to still be retrievable in event handling + removedRole.freezePosition(); + guild.getRolesView().remove(roleId); + //Now that the role is removed from the Guild, remove it from all users and emotes. guild.getMembersView().forEach(m -> { diff --git a/src/main/java/net/dv8tion/jda/internal/utils/PermissionUtil.java b/src/main/java/net/dv8tion/jda/internal/utils/PermissionUtil.java index ea3ebfd056..1772f0446c 100644 --- a/src/main/java/net/dv8tion/jda/internal/utils/PermissionUtil.java +++ b/src/main/java/net/dv8tion/jda/internal/utils/PermissionUtil.java @@ -108,20 +108,14 @@ public static boolean canInteract(Role issuer, Role target) if(!issuer.getGuild().equals(target.getGuild())) throw new IllegalArgumentException("The 2 Roles are not from same Guild!"); - return target.getPosition() < issuer.getPosition(); + return target.compareTo(issuer) < 0; } /** * Check whether the provided {@link net.dv8tion.jda.api.entities.Member Member} can use the specified {@link net.dv8tion.jda.api.entities.Emote Emote}. * *

If the specified Member is not in the emote's guild or the emote provided is from a message this will return false. - * Otherwise it will check if the emote is restricted to any roles and if that is the case if the Member has one of these. - * - *

In the case of an {@link net.dv8tion.jda.api.entities.Emote#isAnimated() animated} Emote, this will - * check if the issuer is the currently logged in account, and then check if the account has - * {@link net.dv8tion.jda.api.entities.SelfUser#isNitro() nitro}, and return false if it doesn't. - *
For other accounts, this method will not take into account whether the emote is animated, as there is - * no real way to check if the Member can interact with them. + * Otherwise, it will check if the emote is restricted to any roles and if that is the case if the Member has one of these. * *
Note: This is not checking if the issuer owns the Guild or not. *