Skip to content

Commit

Permalink
Improve handling of Role#getPosition and canInteract (#2085)
Browse files Browse the repository at this point in the history
  • Loading branch information
MinnDevelopment committed Apr 12, 2022
1 parent 4b957b6 commit b9bb767
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 10 deletions.
8 changes: 8 additions & 0 deletions src/main/java/net/dv8tion/jda/internal/entities/RoleImpl.java
Expand Up @@ -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)
Expand All @@ -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;
Expand Down Expand Up @@ -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();
Expand Down
Expand Up @@ -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
Expand All @@ -48,14 +48,18 @@ 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));
WebSocketClient.LOG.debug("GUILD_ROLE_DELETE was received for a Role that is not yet cached: {}", 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 ->
{
Expand Down
10 changes: 2 additions & 8 deletions src/main/java/net/dv8tion/jda/internal/utils/PermissionUtil.java
Expand Up @@ -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}.
*
* <p>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.
*
* <p>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.
* <br>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.
*
* <br><b>Note</b>: This is not checking if the issuer owns the Guild or not.
*
Expand Down

0 comments on commit b9bb767

Please sign in to comment.