use of org.javacord.api.entity.activity.Activity in project Javacord by BtoBastian.
the class DiscordWebSocketAdapter method updateStatus.
/**
* Sends the update status packet.
*/
public void updateStatus() {
Optional<Activity> activity = api.getActivity();
ObjectNode updateStatus = JsonNodeFactory.instance.objectNode().put("op", GatewayOpcode.STATUS_UPDATE.getCode());
ObjectNode data = updateStatus.putObject("d").put("status", api.getStatus().getStatusString()).put("afk", false).putNull("since");
ObjectNode activityJson = data.putObject("game");
activityJson.put("name", activity.map(Nameable::getName).orElse(null));
activityJson.put("type", activity.flatMap(g -> {
int type = g.getType().getId();
if (type == 4) {
logger.warn("Can't set the activity to ActivityType.CUSTOM" + ", using ActivityType.PLAYING instead");
return Optional.empty();
} else {
return Optional.of(type);
}
}).orElse(0));
activity.flatMap(Activity::getStreamingUrl).ifPresent(url -> activityJson.put("url", url));
logger.debug("Updating status (content: {})", updateStatus);
sendTextFrame(updateStatus.toString());
}
use of org.javacord.api.entity.activity.Activity in project Javacord by BtoBastian.
the class PresenceUpdateHandler method handle.
@Override
public void handle(JsonNode packet) {
// ignore the guild_id and send to all mutual servers instead, or we must track the properties per server
// or all packets after the first do not detect a change and will not send around an event for the server
long userId = packet.get("user").get("id").asLong();
AtomicReference<UserPresence> presence = new AtomicReference<>(api.getEntityCache().get().getUserPresenceCache().getPresenceByUserId(userId).orElseGet(() -> new UserPresence(userId, null, null, io.vavr.collection.HashMap.empty())));
if (packet.hasNonNull("activities")) {
Set<Activity> newActivities = new HashSet<>();
for (JsonNode activityJson : packet.get("activities")) {
if (!activityJson.isNull()) {
newActivities.add(new ActivityImpl(api, activityJson));
}
}
Set<Activity> oldActivities = api.getEntityCache().get().getUserPresenceCache().getPresenceByUserId(userId).map(UserPresence::getActivities).orElse(Collections.emptySet());
presence.set(presence.get().setActivities(newActivities));
if (!Objects.deepEquals(newActivities.toArray(), oldActivities.toArray())) {
dispatchUserActivityChangeEvent(userId, newActivities, oldActivities);
}
}
UserStatus oldStatus = api.getEntityCache().get().getUserPresenceCache().getPresenceByUserId(userId).map(UserPresence::getStatus).orElse(UserStatus.OFFLINE);
UserStatus newStatus;
if (packet.has("status")) {
newStatus = UserStatus.fromString(packet.get("status").asText(null));
presence.set(presence.get().setStatus(newStatus));
} else {
newStatus = oldStatus;
}
Map<DiscordClient, UserStatus> oldClientStatus = api.getEntityCache().get().getUserPresenceCache().getPresenceByUserId(userId).map(UserPresence::getClientStatus).orElse(HashMap.empty());
for (DiscordClient client : DiscordClient.values()) {
if (packet.has("client_status")) {
JsonNode clientStatus = packet.get("client_status");
if (clientStatus.hasNonNull(client.getName())) {
UserStatus status = UserStatus.fromString(clientStatus.get(client.getName()).asText());
presence.set(presence.get().setClientStatus(presence.get().getClientStatus().put(client, status)));
} else {
presence.set(presence.get().setClientStatus(presence.get().getClientStatus().put(client, UserStatus.OFFLINE)));
}
}
}
Map<DiscordClient, UserStatus> newClientStatus = api.getEntityCache().get().getUserPresenceCache().getPresenceByUserId(userId).map(UserPresence::getClientStatus).orElse(HashMap.empty());
api.updateUserPresence(userId, p -> presence.get());
dispatchUserStatusChangeEventIfChangeDetected(userId, newStatus, oldStatus, newClientStatus, oldClientStatus);
}
Aggregations