From d6cc8697662f3391f369bdb5b9bc683886224be5 Mon Sep 17 00:00:00 2001 From: Score_Under Date: Thu, 5 Aug 2021 00:03:04 +0100 Subject: [PATCH 1/3] Create ID alternatives to permission override methods in ChannelManager --- .../jda/api/managers/ChannelManager.java | 125 +++++++++++++++++- .../internal/managers/ChannelManagerImpl.java | 55 ++++++-- 2 files changed, 165 insertions(+), 15 deletions(-) diff --git a/src/main/java/net/dv8tion/jda/api/managers/ChannelManager.java b/src/main/java/net/dv8tion/jda/api/managers/ChannelManager.java index 3c5cd25a73..280b582f81 100644 --- a/src/main/java/net/dv8tion/jda/api/managers/ChannelManager.java +++ b/src/main/java/net/dv8tion/jda/api/managers/ChannelManager.java @@ -235,10 +235,114 @@ 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 allow, @Nullable Collection 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 allow, @Nullable Collection 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 @@ -255,6 +359,25 @@ 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 java.lang.IllegalArgumentException + * If the provided permission holder is {@code 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 + * + * @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}). diff --git a/src/main/java/net/dv8tion/jda/internal/managers/ChannelManagerImpl.java b/src/main/java/net/dv8tion/jda/internal/managers/ChannelManagerImpl.java index 620e5468f4..add9ceba1a 100644 --- a/src/main/java/net/dv8tion/jda/internal/managers/ChannelManagerImpl.java +++ b/src/main/java/net/dv8tion/jda/internal/managers/ChannelManagerImpl.java @@ -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)) { @@ -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 @@ -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); @@ -477,12 +512,4 @@ protected Collection getOverrides() }); return data.valueCollection(); } - - protected long getId(IPermissionHolder holder) - { - if (holder instanceof Role) - return ((Role) holder).getIdLong(); - else - return ((Member) holder).getUser().getIdLong(); - } } From 1075a571cea45503aa816c111d2e5cb6633920ec Mon Sep 17 00:00:00 2001 From: score Date: Thu, 5 Aug 2021 11:01:07 +0100 Subject: [PATCH 2/3] Reformat changes (from code review) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Co-authored-by: Florian Spieß --- .../jda/api/managers/ChannelManager.java | 22 ++++++++++--------- 1 file changed, 12 insertions(+), 10 deletions(-) diff --git a/src/main/java/net/dv8tion/jda/api/managers/ChannelManager.java b/src/main/java/net/dv8tion/jda/api/managers/ChannelManager.java index 280b582f81..9aa0905052 100644 --- a/src/main/java/net/dv8tion/jda/api/managers/ChannelManager.java +++ b/src/main/java/net/dv8tion/jda/api/managers/ChannelManager.java @@ -236,8 +236,8 @@ default ChannelManager putPermissionOverride(@Nonnull IPermissionHolder permHold } /** - * 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. + * 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 @@ -260,8 +260,8 @@ default ChannelManager putPermissionOverride(@Nonnull IPermissionHolder permHold 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. + * 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 @@ -281,15 +281,16 @@ default ChannelManager putPermissionOverride(@Nonnull IPermissionHolder permHold */ @Nonnull @CheckReturnValue - default ChannelManager putRolePermissionOverride(long roleId, @Nullable Collection allow, @Nullable Collection deny) { + default ChannelManager putRolePermissionOverride(long roleId, @Nullable Collection allow, @Nullable Collection 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. + * 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 @@ -312,8 +313,8 @@ default ChannelManager putRolePermissionOverride(long roleId, @Nullable Collecti 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. + * 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 @@ -333,7 +334,8 @@ default ChannelManager putRolePermissionOverride(long roleId, @Nullable Collecti */ @Nonnull @CheckReturnValue - default ChannelManager putMemberPermissionOverride(long memberId, @Nullable Collection allow, @Nullable Collection deny) { + default ChannelManager putMemberPermissionOverride(long memberId, @Nullable Collection allow, @Nullable Collection deny) + { long allowRaw = allow == null ? 0 : Permission.getRaw(allow); long denyRaw = deny == null ? 0 : Permission.getRaw(deny); return putMemberPermissionOverride(memberId, allowRaw, denyRaw); From bc10ea0cc055105a3fdbb492cca3b8a30b9a3962 Mon Sep 17 00:00:00 2001 From: score Date: Tue, 7 Sep 2021 18:19:13 +0100 Subject: [PATCH 3/3] Remove superfluous exception from javadoc MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Co-authored-by: Florian Spieß --- src/main/java/net/dv8tion/jda/api/managers/ChannelManager.java | 2 -- 1 file changed, 2 deletions(-) diff --git a/src/main/java/net/dv8tion/jda/api/managers/ChannelManager.java b/src/main/java/net/dv8tion/jda/api/managers/ChannelManager.java index 9aa0905052..cda1fcb15b 100644 --- a/src/main/java/net/dv8tion/jda/api/managers/ChannelManager.java +++ b/src/main/java/net/dv8tion/jda/api/managers/ChannelManager.java @@ -368,8 +368,6 @@ default ChannelManager putMemberPermissionOverride(long memberId, @Nullable Coll * @param id * The ID of the permission holder * - * @throws java.lang.IllegalArgumentException - * If the provided permission holder is {@code 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