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();
}
Aggregations