use of org.javacord.api.event.server.role.RoleChangeHoistEvent in project Javacord by BtoBastian.
the class GuildRoleUpdateHandler method handle.
@Override
public void handle(JsonNode packet) {
JsonNode roleJson = packet.get("role");
long roleId = roleJson.get("id").asLong();
api.getRoleById(roleId).map(role -> (RoleImpl) role).ifPresent(role -> {
Color oldColorObject = role.getColor().orElse(null);
int oldColor = role.getColorAsInt();
int newColor = roleJson.get("color").asInt(0);
if (oldColor != newColor) {
role.setColor(newColor);
RoleChangeColorEvent event = new RoleChangeColorEventImpl(role, role.getColor().orElse(null), oldColorObject);
api.getEventDispatcher().dispatchRoleChangeColorEvent((DispatchQueueSelector) role.getServer(), role, role.getServer(), event);
}
boolean oldHoist = role.isDisplayedSeparately();
boolean newHoist = roleJson.get("hoist").asBoolean(false);
if (oldHoist != newHoist) {
role.setHoist(newHoist);
RoleChangeHoistEvent event = new RoleChangeHoistEventImpl(role, oldHoist);
api.getEventDispatcher().dispatchRoleChangeHoistEvent((DispatchQueueSelector) role.getServer(), role, role.getServer(), event);
}
boolean oldMentionable = role.isMentionable();
boolean newMentionable = roleJson.get("mentionable").asBoolean(false);
if (oldMentionable != newMentionable) {
role.setMentionable(newMentionable);
RoleChangeMentionableEvent event = new RoleChangeMentionableEventImpl(role, oldMentionable);
api.getEventDispatcher().dispatchRoleChangeMentionableEvent((DispatchQueueSelector) role.getServer(), role, role.getServer(), event);
}
String oldName = role.getName();
String newName = roleJson.get("name").asText();
if (!oldName.equals(newName)) {
role.setName(newName);
RoleChangeNameEvent event = new RoleChangeNameEventImpl(role, newName, oldName);
api.getEventDispatcher().dispatchRoleChangeNameEvent((DispatchQueueSelector) role.getServer(), role, role.getServer(), event);
}
Permissions oldPermissions = role.getPermissions();
PermissionsImpl newPermissions = new PermissionsImpl(roleJson.get("permissions").asLong(), 0);
if (!oldPermissions.equals(newPermissions)) {
role.setPermissions(newPermissions);
RoleChangePermissionsEvent event = new RoleChangePermissionsEventImpl(role, newPermissions, oldPermissions);
api.getEventDispatcher().dispatchRoleChangePermissionsEvent((DispatchQueueSelector) role.getServer(), role, role.getServer(), event);
// If bot is affected remove messages from cache that are no longer visible
if (role.getUsers().stream().anyMatch(User::isYourself)) {
Set<Long> unreadableChannels = role.getServer().getTextChannels().stream().filter(((Predicate<ServerTextChannel>) ServerTextChannel::canYouSee).negate()).map(ServerTextChannel::getId).collect(Collectors.toSet());
api.forEachCachedMessageWhere(msg -> unreadableChannels.contains(msg.getChannel().getId()), msg -> {
api.removeMessageFromCache(msg.getId());
((MessageCacheImpl) msg.getChannel().getMessageCache()).removeMessage(msg);
});
}
}
int oldPosition = role.getPosition();
int oldRawPosition = role.getRawPosition();
int newRawPosition = roleJson.get("position").asInt();
if (oldRawPosition != newRawPosition) {
role.setRawPosition(newRawPosition);
int newPosition = role.getPosition();
RoleChangePositionEvent event = new RoleChangePositionEventImpl(role, newRawPosition, oldRawPosition, newPosition, oldPosition);
api.getEventDispatcher().dispatchRoleChangePositionEvent((DispatchQueueSelector) role.getServer(), role, role.getServer(), event);
}
});
}
Aggregations