Search in sources :

Example 1 with PermissionState

use of org.javacord.api.entity.permission.PermissionState in project Javacord by BtoBastian.

the class RegularServerChannel method getEffectivePermissions.

/**
 * Gets the effective permissions of a user in this channel.
 * The returned permission object will only have {@link PermissionState#ALLOWED} and
 * {@link PermissionState#DENIED} states!
 * It takes into account global permissions and the effective overwritten permissions of a user.
 * Remember, that some permissions affect others!
 * E.g. a user who has {@link PermissionType#SEND_MESSAGES} but not {@link PermissionType#READ_MESSAGES} cannot
 * send messages, even though he has the {@link PermissionType#SEND_MESSAGES} permission.
 *
 * @param user The user.
 * @return The effective permissions of the user in this channel.
 */
default Permissions getEffectivePermissions(User user) {
    if (getServer().isOwner(user)) {
        return getServer().getPermissions(user);
    }
    PermissionsBuilder builder = new PermissionsBuilder(getServer().getPermissions(user));
    Permissions effectiveOverwrittenPermissions = getEffectiveOverwrittenPermissions(user);
    Arrays.stream(PermissionType.values()).filter(type -> effectiveOverwrittenPermissions.getState(type) != PermissionState.UNSET).forEachOrdered(type -> builder.setState(type, effectiveOverwrittenPermissions.getState(type)));
    Arrays.stream(PermissionType.values()).filter(type -> builder.getState(type) == PermissionState.UNSET).forEachOrdered(type -> builder.setState(type, PermissionState.DENIED));
    return builder.build();
}
Also used : PermissionType(org.javacord.api.entity.permission.PermissionType) Arrays(java.util.Arrays) PermissionsBuilder(org.javacord.api.entity.permission.PermissionsBuilder) Collection(java.util.Collection) HashMap(java.util.HashMap) CompletableFuture(java.util.concurrent.CompletableFuture) Permissionable(org.javacord.api.entity.Permissionable) Permissions(org.javacord.api.entity.permission.Permissions) DiscordEntity(org.javacord.api.entity.DiscordEntity) PermissionState(org.javacord.api.entity.permission.PermissionState) User(org.javacord.api.entity.user.User) Map(java.util.Map) Optional(java.util.Optional) NoSuchElementException(java.util.NoSuchElementException) Collections(java.util.Collections) Permissions(org.javacord.api.entity.permission.Permissions) PermissionsBuilder(org.javacord.api.entity.permission.PermissionsBuilder)

Aggregations

Arrays (java.util.Arrays)1 Collection (java.util.Collection)1 Collections (java.util.Collections)1 HashMap (java.util.HashMap)1 Map (java.util.Map)1 NoSuchElementException (java.util.NoSuchElementException)1 Optional (java.util.Optional)1 CompletableFuture (java.util.concurrent.CompletableFuture)1 DiscordEntity (org.javacord.api.entity.DiscordEntity)1 Permissionable (org.javacord.api.entity.Permissionable)1 PermissionState (org.javacord.api.entity.permission.PermissionState)1 PermissionType (org.javacord.api.entity.permission.PermissionType)1 Permissions (org.javacord.api.entity.permission.Permissions)1 PermissionsBuilder (org.javacord.api.entity.permission.PermissionsBuilder)1 User (org.javacord.api.entity.user.User)1