Search in sources :

Example 1 with Nameable

use of org.javacord.api.entity.Nameable 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)

Aggregations

ObjectNode (com.fasterxml.jackson.databind.node.ObjectNode)1 Nameable (org.javacord.api.entity.Nameable)1 Activity (org.javacord.api.entity.activity.Activity)1 RestEndpoint (org.javacord.core.util.rest.RestEndpoint)1