use of org.javacord.api.entity.permission.Role in project Javacord by BtoBastian.
the class RegularServerChannelImpl method getEffectiveOverwrittenPermissions.
@Override
public Permissions getEffectiveOverwrittenPermissions(User user) {
PermissionsBuilder builder = new PermissionsBuilder(PermissionsImpl.EMPTY_PERMISSIONS);
Server server = getServer();
Role everyoneRole = server.getEveryoneRole();
Permissions everyoneRolePermissionOverwrites = getOverwrittenPermissions(everyoneRole);
for (PermissionType type : PermissionType.values()) {
if (everyoneRolePermissionOverwrites.getState(type) == PermissionState.DENIED) {
builder.setState(type, PermissionState.DENIED);
}
if (everyoneRolePermissionOverwrites.getState(type) == PermissionState.ALLOWED) {
builder.setState(type, PermissionState.ALLOWED);
}
}
List<Role> rolesOfUser = new ArrayList<>(server.getRoles(user));
rolesOfUser.remove(everyoneRole);
List<Permissions> permissionOverwrites = rolesOfUser.stream().map(this::getOverwrittenPermissions).collect(Collectors.toList());
for (Permissions permissions : permissionOverwrites) {
for (PermissionType type : PermissionType.values()) {
if (permissions.getState(type) == PermissionState.DENIED) {
builder.setState(type, PermissionState.DENIED);
}
}
}
for (Permissions permissions : permissionOverwrites) {
for (PermissionType type : PermissionType.values()) {
if (permissions.getState(type) == PermissionState.ALLOWED) {
builder.setState(type, PermissionState.ALLOWED);
}
}
}
for (PermissionType type : PermissionType.values()) {
Permissions permissions = overwrittenUserPermissions.getOrDefault(user.getId(), PermissionsImpl.EMPTY_PERMISSIONS);
if (permissions.getState(type) == PermissionState.DENIED) {
builder.setState(type, PermissionState.DENIED);
}
if (permissions.getState(type) == PermissionState.ALLOWED) {
builder.setState(type, PermissionState.ALLOWED);
}
}
return builder.build();
}
use of org.javacord.api.entity.permission.Role in project Javacord by BtoBastian.
the class CustomEmojiBuilderDelegateImpl method create.
@Override
public CompletableFuture<KnownCustomEmoji> create() {
if (name == null) {
throw new IllegalStateException("The name is no optional parameter!");
}
if (image == null) {
throw new IllegalStateException("The image is no optional parameter!");
}
ObjectNode body = JsonNodeFactory.instance.objectNode().put("name", name);
if (whitelist != null) {
ArrayNode jsonRoles = body.putArray("roles");
whitelist.stream().map(Role::getIdAsString).forEach(jsonRoles::add);
}
return image.asByteArray(server.getApi()).thenAccept(bytes -> {
String base64Icon = "data:image/" + image.getFileType() + ";base64," + Base64.getEncoder().encodeToString(bytes);
body.put("image", base64Icon);
}).thenCompose(aVoid -> new RestRequest<KnownCustomEmoji>(server.getApi(), RestMethod.POST, RestEndpoint.CUSTOM_EMOJI).setUrlParameters(server.getIdAsString()).setBody(body).setAuditLogReason(reason).execute(result -> ((DiscordApiImpl) server.getApi()).getOrCreateKnownCustomEmoji(server, result.getJsonBody())));
}
use of org.javacord.api.entity.permission.Role in project Javacord by BtoBastian.
the class ServerUpdaterDelegateImpl method update.
@Override
public CompletableFuture<Void> update() {
// A set with all members that get updates
HashSet<User> members = new HashSet<>(userRoles.keySet());
members.addAll(userNicknames.keySet());
members.addAll(userMuted.keySet());
members.addAll(userDeafened.keySet());
members.addAll(userMoveTargets.keySet());
// A list with all tasks
List<CompletableFuture<?>> tasks = new ArrayList<>();
members.forEach(member -> {
boolean patchMember = false;
ObjectNode updateNode = JsonNodeFactory.instance.objectNode();
Collection<Role> roles = userRoles.get(member);
if (roles != null) {
ArrayNode rolesJson = updateNode.putArray("roles");
roles.stream().map(DiscordEntity::getIdAsString).forEach(rolesJson::add);
patchMember = true;
}
if (userNicknames.containsKey(member)) {
String nickname = userNicknames.get(member);
if (member.isYourself()) {
tasks.add(new RestRequest<Void>(server.getApi(), RestMethod.PATCH, RestEndpoint.OWN_NICKNAME).setUrlParameters(server.getIdAsString()).setBody(JsonNodeFactory.instance.objectNode().put("nick", nickname)).setAuditLogReason(reason).execute(result -> null));
} else {
updateNode.put("nick", (nickname == null) ? "" : nickname);
patchMember = true;
}
}
if (userMuted.containsKey(member)) {
updateNode.put("mute", userMuted.get(member));
patchMember = true;
}
if (userDeafened.containsKey(member)) {
updateNode.put("deaf", userDeafened.get(member));
patchMember = true;
}
if (userMoveTargets.containsKey(member)) {
ServerVoiceChannel channel = userMoveTargets.get(member);
if (member.isYourself()) {
((DiscordApiImpl) server.getApi()).getWebSocketAdapter().sendVoiceStateUpdate(server, channel, null, null);
} else if (channel != null) {
updateNode.put("channel_id", channel.getId());
patchMember = true;
} else {
updateNode.putNull("channel_id");
patchMember = true;
}
}
if (patchMember) {
tasks.add(new RestRequest<Void>(server.getApi(), RestMethod.PATCH, RestEndpoint.SERVER_MEMBER).setUrlParameters(server.getIdAsString(), member.getIdAsString()).setBody(updateNode).setAuditLogReason(reason).execute(result -> null));
}
});
if (newRolesOrder != null) {
tasks.add(server.reorderRoles(newRolesOrder, reason));
}
// Server settings
boolean patchServer = false;
ObjectNode body = JsonNodeFactory.instance.objectNode();
if (name != null) {
body.put("name", name);
patchServer = true;
}
if (region != null) {
body.put("region", region.getKey());
patchServer = true;
}
if (explicitContentFilterLevel != null) {
body.put("explicit_content_filter", explicitContentFilterLevel.getId());
patchServer = true;
}
if (verificationLevel != null) {
body.put("verification_level", verificationLevel.getId());
patchServer = true;
}
if (defaultMessageNotificationLevel != null) {
body.put("default_message_notifications", defaultMessageNotificationLevel.getId());
patchServer = true;
}
if (updateAfkChannel) {
if (afkChannel != null) {
body.put("afk_channel_id", afkChannel.getIdAsString());
} else {
body.putNull("afk_channel_id");
}
patchServer = true;
}
if (afkTimeout != null) {
body.put("afk_timeout", afkTimeout.intValue());
patchServer = true;
}
if (updateIcon) {
if (icon == null) {
body.putNull("icon");
}
patchServer = true;
}
if (updateSplash) {
if (splash == null) {
body.putNull("splash");
}
patchServer = true;
}
if (owner != null) {
body.put("owner_id", owner.getIdAsString());
patchServer = true;
}
if (updateSystemChannel) {
if (systemChannel != null) {
body.put("system_channel_id", systemChannel.getIdAsString());
} else {
body.putNull("system_channel_id");
}
patchServer = true;
}
if (updateModeratorsOnlyChannel) {
if (moderatorsOnlyChannel != null) {
body.put("public_updates_channel_id", moderatorsOnlyChannel.getIdAsString());
} else {
body.putNull("public_updates_channel_id");
}
patchServer = true;
}
if (updateRulesChannel) {
if (rulesChannel != null) {
body.put("rules_channel_id", rulesChannel.getIdAsString());
} else {
body.putNull("rules_channel_id");
}
patchServer = true;
}
if (updateBanner) {
if (banner == null) {
body.putNull("banner");
}
patchServer = true;
}
if (updateLocale) {
if (locale == null) {
body.putNull("preferred_locale");
} else {
body.put("preferred_locale", locale.toLanguageTag());
}
patchServer = true;
}
// Only make a REST call, if we really want to update something
if (patchServer) {
if (icon != null || splash != null || banner != null) {
CompletableFuture<Void> iconFuture = null;
if (icon != null) {
iconFuture = icon.asByteArray(server.getApi()).thenAccept(bytes -> {
String base64Icon = "data:image/" + icon.getFileType() + ";base64," + Base64.getEncoder().encodeToString(bytes);
body.put("icon", base64Icon);
});
}
CompletableFuture<Void> splashFuture = null;
if (splash != null) {
splashFuture = splash.asByteArray(server.getApi()).thenAccept(bytes -> {
String base64Splash = "data:image/" + splash.getFileType() + ";base64," + Base64.getEncoder().encodeToString(bytes);
body.put("splash", base64Splash);
});
}
CompletableFuture<Void> bannerFuture = null;
if (banner != null) {
bannerFuture = banner.asByteArray(server.getApi()).thenAccept(bytes -> {
String base64Banner = "data:image/" + banner.getFileType() + ";base64," + Base64.getEncoder().encodeToString(bytes);
body.put("banner", base64Banner);
});
}
CompletableFuture<Void> future;
List<CompletableFuture<Void>> futureList = new ArrayList<>();
if (iconFuture != null) {
futureList.add(iconFuture);
}
if (splashFuture != null) {
futureList.add(splashFuture);
}
if (bannerFuture != null) {
futureList.add(bannerFuture);
}
future = CompletableFuture.allOf(futureList.toArray(new CompletableFuture[futureList.size()]));
tasks.add(future.thenCompose(aVoid -> new RestRequest<Void>(server.getApi(), RestMethod.PATCH, RestEndpoint.SERVER).setUrlParameters(server.getIdAsString()).setBody(body).setAuditLogReason(reason).execute(result -> null)));
} else {
tasks.add(new RestRequest<Void>(server.getApi(), RestMethod.PATCH, RestEndpoint.SERVER).setUrlParameters(server.getIdAsString()).setBody(body).setAuditLogReason(reason).execute(result -> null));
}
}
CompletableFuture<?>[] tasksArray = tasks.toArray(new CompletableFuture<?>[tasks.size()]);
return CompletableFuture.allOf(tasksArray);
}
use of org.javacord.api.entity.permission.Role in project Javacord by BtoBastian.
the class ServerImpl method reorderRoles.
@Override
public CompletableFuture<Void> reorderRoles(List<Role> roles, String reason) {
// Copy the list to safely modify it
roles = new ArrayList<>(roles);
ArrayNode body = JsonNodeFactory.instance.arrayNode();
roles.removeIf(Role::isEveryoneRole);
for (int i = 0; i < roles.size(); i++) {
body.addObject().put("id", roles.get(i).getIdAsString()).put("position", i + 1);
}
return new RestRequest<Void>(getApi(), RestMethod.PATCH, RestEndpoint.ROLE).setUrlParameters(getIdAsString()).setBody(body).setAuditLogReason(reason).execute(result -> null);
}
use of org.javacord.api.entity.permission.Role in project Javacord by BtoBastian.
the class Server method canKickUser.
/**
* Checks if the given user can kick the other user.
* This method also considers the position of the user roles and the owner.
* If the to-be-kicked user is not part of this server, still {@code true} is returned as Discord allows this.
*
* @param user The user which "want" to kick the other user.
* @param userToKick The user which should be kicked.
* @return Whether the given user can kick the other user or not.
*/
default boolean canKickUser(User user, User userToKick) {
// owner can always kick, regardless of role for example
if (isOwner(user)) {
return true;
}
// user cannot kick at all
if (!canKickUsers(user)) {
return false;
}
// only returns empty optional if user is not member of server
// but then canKickUsers should already have kicked in
Role ownRole = getHighestRole(user).orElseThrow(AssertionError::new);
Optional<Role> otherRole = getHighestRole(userToKick);
// otherRole empty => userToKick is not on the server => kick is allowed as Discord allows it
boolean userToKickOnServer = otherRole.isPresent();
return !userToKickOnServer || (ownRole.compareTo(otherRole.get()) > 0);
}
Aggregations