Skip to content

Commit

Permalink
Create ID alternatives to permission override methods in ChannelManag…
Browse files Browse the repository at this point in the history
…er (#1762)
  • Loading branch information
ScoreUnder committed Sep 7, 2021
1 parent cba6cf6 commit 39c67d1
Show file tree
Hide file tree
Showing 2 changed files with 165 additions and 15 deletions.
125 changes: 124 additions & 1 deletion src/main/java/net/dv8tion/jda/api/managers/ChannelManager.java
Original file line number Diff line number Diff line change
Expand Up @@ -235,10 +235,116 @@ default ChannelManager putPermissionOverride(@Nonnull IPermissionHolder permHold
return putPermissionOverride(permHolder, allowRaw, denyRaw);
}

/**
* Adds an override for the specified role with the provided raw bitmasks as allowed and denied permissions.
* If the role already had an override on this channel it will be replaced instead.
*
* @param roleId
* The ID of the role to set permissions for
* @param allow
* The bitmask to grant
* @param deny
* The bitmask to deny
*
* @throws net.dv8tion.jda.api.exceptions.InsufficientPermissionException
* If the currently logged in account does not have {@link net.dv8tion.jda.api.Permission#MANAGE_PERMISSIONS Permission.MANAGE_PERMISSIONS}
* in this channel, or tries to set permissions it does not have without having {@link Permission#MANAGE_PERMISSIONS Permission.MANAGE_PERMISSIONS} explicitly for this channel through an override.
*
* @return ChannelManager for chaining convenience
*
* @see #putRolePermissionOverride(long, Collection, Collection)
* @see net.dv8tion.jda.api.Permission#getRaw(Permission...) Permission.getRaw(Permission...)
*/
@Nonnull
@CheckReturnValue
ChannelManager putRolePermissionOverride(long roleId, long allow, long deny);

/**
* Adds an override for the specified role with the provided permission sets as allowed and denied permissions.
* If the role already had an override on this channel it will be replaced instead.
*
* @param roleId
* The ID of the role to set permissions for
* @param allow
* The permissions to grant, or null
* @param deny
* The permissions to deny, or null
*
* @throws net.dv8tion.jda.api.exceptions.InsufficientPermissionException
* If the currently logged in account does not have {@link net.dv8tion.jda.api.Permission#MANAGE_PERMISSIONS Permission.MANAGE_PERMISSIONS}
* in this channel, or tries to set permissions it does not have without having {@link Permission#MANAGE_PERMISSIONS Permission.MANAGE_PERMISSIONS} explicitly for this channel through an override.
*
* @return ChannelManager for chaining convenience
*
* @see #putRolePermissionOverride(long, long, long)
* @see java.util.EnumSet EnumSet
*/
@Nonnull
@CheckReturnValue
default ChannelManager putRolePermissionOverride(long roleId, @Nullable Collection<Permission> allow, @Nullable Collection<Permission> deny)
{
long allowRaw = allow == null ? 0 : Permission.getRaw(allow);
long denyRaw = deny == null ? 0 : Permission.getRaw(deny);
return putRolePermissionOverride(roleId, allowRaw, denyRaw);
}

/**
* Adds an override for the specified member with the provided raw bitmasks as allowed and denied permissions.
* If the member already had an override on this channel it will be replaced instead.
*
* @param memberId
* The ID of the member to set permissions for
* @param allow
* The bitmask to grant
* @param deny
* The bitmask to deny
*
* @throws net.dv8tion.jda.api.exceptions.InsufficientPermissionException
* If the currently logged in account does not have {@link net.dv8tion.jda.api.Permission#MANAGE_PERMISSIONS Permission.MANAGE_PERMISSIONS}
* in this channel, or tries to set permissions it does not have without having {@link Permission#MANAGE_PERMISSIONS Permission.MANAGE_PERMISSIONS} explicitly for this channel through an override.
*
* @return ChannelManager for chaining convenience
*
* @see #putMemberPermissionOverride(long, Collection, Collection)
* @see net.dv8tion.jda.api.Permission#getRaw(Permission...) Permission.getRaw(Permission...)
*/
@Nonnull
@CheckReturnValue
ChannelManager putMemberPermissionOverride(long memberId, long allow, long deny);

/**
* Adds an override for the specified member with the provided permission sets as allowed and denied permissions.
* If the member already had an override on this channel it will be replaced instead.
*
* @param memberId
* The ID of the member to set permissions for
* @param allow
* The permissions to grant, or null
* @param deny
* The permissions to deny, or null
*
* @throws net.dv8tion.jda.api.exceptions.InsufficientPermissionException
* If the currently logged in account does not have {@link net.dv8tion.jda.api.Permission#MANAGE_PERMISSIONS Permission.MANAGE_PERMISSIONS}
* in this channel, or tries to set permissions it does not have without having {@link Permission#MANAGE_PERMISSIONS Permission.MANAGE_PERMISSIONS} explicitly for this channel through an override.
*
* @return ChannelManager for chaining convenience
*
* @see #putMemberPermissionOverride(long, long, long)
* @see java.util.EnumSet EnumSet
*/
@Nonnull
@CheckReturnValue
default ChannelManager putMemberPermissionOverride(long memberId, @Nullable Collection<Permission> allow, @Nullable Collection<Permission> deny)
{
long allowRaw = allow == null ? 0 : Permission.getRaw(allow);
long denyRaw = deny == null ? 0 : Permission.getRaw(deny);
return putMemberPermissionOverride(memberId, allowRaw, denyRaw);
}

/**
* Removes the {@link net.dv8tion.jda.api.entities.PermissionOverride PermissionOverride} for the specified
* {@link net.dv8tion.jda.api.entities.IPermissionHolder IPermissionHolder}. If no override existed for this member
* this does nothing.
* or role, this does nothing.
*
* @param permHolder
* The permission holder
Expand All @@ -255,6 +361,23 @@ default ChannelManager putPermissionOverride(@Nonnull IPermissionHolder permHold
@CheckReturnValue
ChannelManager removePermissionOverride(@Nonnull IPermissionHolder permHolder);

/**
* Removes the {@link net.dv8tion.jda.api.entities.PermissionOverride PermissionOverride} for the specified
* member or role ID. If no override existed for this member or role, this does nothing.
*
* @param id
* The ID of the permission holder
*
* @throws net.dv8tion.jda.api.exceptions.InsufficientPermissionException
* If the currently logged in account does not have {@link net.dv8tion.jda.api.Permission#MANAGE_PERMISSIONS Permission.MANAGE_PERMISSIONS}
* in this channel
*
* @return ChannelManager for chaining convenience
*/
@Nonnull
@CheckReturnValue
ChannelManager removePermissionOverride(long id);

/**
* Syncs all {@link net.dv8tion.jda.api.entities.PermissionOverride PermissionOverrides} of this GuildChannel with
* its parent ({@link net.dv8tion.jda.api.entities.Category Category}).
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -178,6 +178,32 @@ public ChannelManagerImpl putPermissionOverride(@Nonnull IPermissionHolder permH
{
Checks.notNull(permHolder, "PermissionHolder");
Checks.check(permHolder.getGuild().equals(getGuild()), "PermissionHolder is not from the same Guild!");
final long id = permHolder.getIdLong();
final int type = permHolder instanceof Role ? PermOverrideData.ROLE_TYPE : PermOverrideData.MEMBER_TYPE;
putPermissionOverride(new PermOverrideData(type, id, allow, deny));
return this;
}

@Nonnull
@Override
@CheckReturnValue
public ChannelManagerImpl putMemberPermissionOverride(long memberId, long allow, long deny)
{
putPermissionOverride(new PermOverrideData(PermOverrideData.MEMBER_TYPE, memberId, allow, deny));
return this;
}

@Nonnull
@Override
@CheckReturnValue
public ChannelManagerImpl putRolePermissionOverride(long roleId, long allow, long deny)
{
putPermissionOverride(new PermOverrideData(PermOverrideData.ROLE_TYPE, roleId, allow, deny));
return this;
}

private void checkCanPutPermissions(long allow, long deny)
{
Member selfMember = getGuild().getSelfMember();
if (isPermissionChecksEnabled() && !selfMember.hasPermission(Permission.ADMINISTRATOR))
{
Expand All @@ -197,15 +223,17 @@ public ChannelManagerImpl putPermissionOverride(@Nonnull IPermissionHolder permH
throw new InsufficientPermissionException(channel, Permission.MANAGE_PERMISSIONS, "You must have Permission.MANAGE_PERMISSIONS on the channel explicitly in order to set permissions you don't already have!");
}
}
final long id = getId(permHolder);
final int type = permHolder instanceof Role ? PermOverrideData.ROLE_TYPE : PermOverrideData.MEMBER_TYPE;
}

private void putPermissionOverride(@Nonnull final PermOverrideData overrideData)
{
checkCanPutPermissions(overrideData.allow, overrideData.deny);
withLock(lock, (lock) ->
{
this.overridesRem.remove(id);
this.overridesAdd.put(id, new PermOverrideData(type, id, allow, deny));
this.overridesRem.remove(overrideData.id);
this.overridesAdd.put(overrideData.id, overrideData);
set |= PERMISSION;
});
return this;
}

@Nonnull
Expand All @@ -215,9 +243,16 @@ public ChannelManagerImpl removePermissionOverride(@Nonnull IPermissionHolder pe
{
Checks.notNull(permHolder, "PermissionHolder");
Checks.check(permHolder.getGuild().equals(getGuild()), "PermissionHolder is not from the same Guild!");
return removePermissionOverride(permHolder.getIdLong());
}

@Nonnull
@Override
@CheckReturnValue
public ChannelManagerImpl removePermissionOverride(final long id)
{
if (isPermissionChecksEnabled() && !getGuild().getSelfMember().hasPermission(getChannel(), Permission.MANAGE_PERMISSIONS))
throw new InsufficientPermissionException(getChannel(), Permission.MANAGE_PERMISSIONS);
final long id = getId(permHolder);
withLock(lock, (lock) ->
{
this.overridesRem.add(id);
Expand Down Expand Up @@ -477,12 +512,4 @@ protected Collection<PermOverrideData> getOverrides()
});
return data.valueCollection();
}

protected long getId(IPermissionHolder holder)
{
if (holder instanceof Role)
return ((Role) holder).getIdLong();
else
return ((Member) holder).getUser().getIdLong();
}
}

0 comments on commit 39c67d1

Please sign in to comment.