Search in sources :

Example 1 with Activity

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());
}
Also used : Nameable(org.javacord.api.entity.Nameable) ObjectNode(com.fasterxml.jackson.databind.node.ObjectNode) Activity(org.javacord.api.entity.activity.Activity) RestEndpoint(org.javacord.core.util.rest.RestEndpoint)

Example 2 with Activity

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);
}
Also used : DiscordClient(org.javacord.api.entity.DiscordClient) ActivityImpl(org.javacord.core.entity.activity.ActivityImpl) Activity(org.javacord.api.entity.activity.Activity) AtomicReference(java.util.concurrent.atomic.AtomicReference) JsonNode(com.fasterxml.jackson.databind.JsonNode) UserPresence(org.javacord.core.entity.user.UserPresence) UserStatus(org.javacord.api.entity.user.UserStatus) HashSet(java.util.HashSet)

Aggregations

Activity (org.javacord.api.entity.activity.Activity)2 JsonNode (com.fasterxml.jackson.databind.JsonNode)1 ObjectNode (com.fasterxml.jackson.databind.node.ObjectNode)1 HashSet (java.util.HashSet)1 AtomicReference (java.util.concurrent.atomic.AtomicReference)1 DiscordClient (org.javacord.api.entity.DiscordClient)1 Nameable (org.javacord.api.entity.Nameable)1 UserStatus (org.javacord.api.entity.user.UserStatus)1 ActivityImpl (org.javacord.core.entity.activity.ActivityImpl)1 UserPresence (org.javacord.core.entity.user.UserPresence)1 RestEndpoint (org.javacord.core.util.rest.RestEndpoint)1