Skip to content

Commit

Permalink
Fix deny/grant on override action (#1560)
Browse files Browse the repository at this point in the history
  • Loading branch information
MinnDevelopment committed Mar 27, 2021
1 parent c6f4a9d commit d7f37f1
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 28 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -322,10 +322,7 @@ default PermissionOverrideAction setAllow(@Nullable Permission... permissions)
*/
@Nonnull
@CheckReturnValue
default PermissionOverrideAction grant(long allowBits)
{
return setAllow(getAllow() | allowBits);
}
PermissionOverrideAction grant(long allowBits);

/**
* Grants the specified permissions.
Expand All @@ -346,7 +343,7 @@ default PermissionOverrideAction grant(long allowBits)
@CheckReturnValue
default PermissionOverrideAction grant(@Nonnull Collection<Permission> permissions)
{
return setAllow(getAllow() | Permission.getRaw(permissions));
return grant(Permission.getRaw(permissions));
}

/**
Expand All @@ -368,7 +365,7 @@ default PermissionOverrideAction grant(@Nonnull Collection<Permission> permissio
@CheckReturnValue
default PermissionOverrideAction grant(@Nonnull Permission... permissions)
{
return setAllow(getAllow() | Permission.getRaw(permissions));
return grant(Permission.getRaw(permissions));
}


Expand Down Expand Up @@ -474,10 +471,7 @@ default PermissionOverrideAction setDeny(@Nullable Permission... permissions)
*/
@Nonnull
@CheckReturnValue
default PermissionOverrideAction deny(long denyBits)
{
return setDeny(getDeny() | denyBits);
}
PermissionOverrideAction deny(long denyBits);

/**
* Denies the specified permissions.
Expand All @@ -498,7 +492,7 @@ default PermissionOverrideAction deny(long denyBits)
@CheckReturnValue
default PermissionOverrideAction deny(@Nonnull Collection<Permission> permissions)
{
return setDeny(getDeny() | Permission.getRaw(permissions));
return deny(Permission.getRaw(permissions));
}

/**
Expand All @@ -520,7 +514,7 @@ default PermissionOverrideAction deny(@Nonnull Collection<Permission> permission
@CheckReturnValue
default PermissionOverrideAction deny(@Nonnull Permission... permissions)
{
return setDeny(getDeny() | Permission.getRaw(permissions));
return deny(Permission.getRaw(permissions));
}

/**
Expand All @@ -539,10 +533,7 @@ default PermissionOverrideAction deny(@Nonnull Permission... permissions)
*/
@Nonnull
@CheckReturnValue
default PermissionOverrideAction clear(long inheritedBits)
{
return setDeny(getDeny() & ~inheritedBits).setAllow(getAllow() & ~inheritedBits);
}
PermissionOverrideAction clear(long inheritedBits);

/**
* Clears the provided {@link net.dv8tion.jda.api.Permission Permissions} bits
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,7 @@ public PermissionOverrideActionImpl deadline(long timestamp)
@Override
public PermissionOverrideAction resetAllow()
{
allow = getCurrentAllow();
allow = getOriginalAllow();
allowSet = false;
return this;
}
Expand All @@ -127,7 +127,7 @@ public PermissionOverrideAction resetAllow()
@Override
public PermissionOverrideAction resetDeny()
{
deny = getCurrentDeny();
deny = getOriginalDeny();
denySet = false;
return this;
}
Expand Down Expand Up @@ -186,25 +186,46 @@ public boolean isRole()
@CheckReturnValue
public PermissionOverrideActionImpl setAllow(long allowBits)
{
checkPermissions(getCurrentAllow() ^ allowBits);
checkPermissions(getOriginalAllow() ^ allowBits);
this.allow = allowBits;
this.deny &= ~allowBits;
allowSet = denySet = true;
return this;
}

@Nonnull
@Override
public PermissionOverrideAction grant(long allowBits)
{
return setAllow(getCurrentAllow() | allowBits);
}

@Nonnull
@Override
@CheckReturnValue
public PermissionOverrideActionImpl setDeny(long denyBits)
{
checkPermissions(getCurrentDeny() ^ denyBits);
checkPermissions(getOriginalDeny() ^ denyBits);
this.deny = denyBits;
this.allow &= ~denyBits;
allowSet = denySet = true;
return this;
}

@Nonnull
@Override
public PermissionOverrideAction deny(long denyBits)
{
return setDeny(getCurrentDeny() | denyBits);
}

@Nonnull
@Override
public PermissionOverrideAction clear(long inheritedBits)
{
return setAllow(getCurrentAllow() & ~inheritedBits).setDeny(getCurrentDeny() & ~inheritedBits);
}

protected void checkPermissions(long changed)
{
Member selfMember = getGuild().getSelfMember();
Expand Down Expand Up @@ -232,27 +253,37 @@ public PermissionOverrideActionImpl setPermissions(long allowBits, long denyBits

private long getCurrentAllow()
{
if (isOverride)
return 0;
PermissionOverride override = channel.getOverrideMap().get(id);
return override == null ? 0 : override.getAllowedRaw();
if (allowSet)
return allow;
return isOverride ? 0 : getOriginalAllow();
}

private long getCurrentDeny()
{
if (isOverride)
return 0;
if (denySet)
return deny;
return isOverride ? 0 : getOriginalDeny();
}

private long getOriginalDeny()
{
PermissionOverride override = channel.getOverrideMap().get(id);
return override == null ? 0 : override.getDeniedRaw();
}

private long getOriginalAllow()
{
PermissionOverride override = channel.getOverrideMap().get(id);
return override == null ? 0 : override.getAllowedRaw();
}

@Override
protected RequestBody finalizeData()
{
DataObject object = DataObject.empty();
object.put("type", isRole() ? "role" : "member");
object.put("allow", allowSet ? allow : getCurrentAllow());
object.put("deny", denySet ? deny : getCurrentDeny());
object.put("allow", getCurrentAllow());
object.put("deny", getCurrentDeny());
reset();
return getRequestBody(object);
}
Expand Down

0 comments on commit d7f37f1

Please sign in to comment.