Java Code Examples for net.dv8tion.jda.api.OnlineStatus#fromKey()

The following examples show how to use net.dv8tion.jda.api.OnlineStatus#fromKey() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: WidgetUtil.java    From JDA with Apache License 2.0 5 votes vote down vote up
private Member(@Nonnull DataObject json, @Nonnull Widget widget)
{
    this.widget = widget;
    this.bot = json.getBoolean("bot");
    this.id = json.getLong("id");
    this.username = json.getString("username");
    this.discriminator = json.getString("discriminator");
    this.avatar = json.getString("avatar", null);
    this.nickname = json.getString("nick", null);
    this.status = OnlineStatus.fromKey(json.getString("status"));
    this.game = json.isNull("game") ? null : EntityBuilder.createActivity(json.getObject("game"));
}
 
Example 2
Source File: PresenceUpdateHandler.java    From JDA with Apache License 2.0 4 votes vote down vote up
@Override
protected Long handleInternally(DataObject content)
{
    // Ignore events for relationships, presences are guild only to us
    if (content.isNull("guild_id"))
    {
        log.debug("Received PRESENCE_UPDATE without guild_id. Ignoring event.");
        return null;
    }

    //Do a pre-check to see if this is for a Guild, and if it is, if the guild is currently locked or not cached.
    final long guildId = content.getLong("guild_id");
    if (getJDA().getGuildSetupController().isLocked(guildId))
        return guildId;
    GuildImpl guild = (GuildImpl) getJDA().getGuildById(guildId);
    if (guild == null)
    {
        getJDA().getEventCache().cache(EventCache.Type.GUILD, guildId, responseNumber, allContent, this::handle);
        EventCache.LOG.debug("Received a PRESENCE_UPDATE for a guild that is not yet cached! GuildId:{} UserId: {}",
                             guildId, content.getObject("user").get("id"));
        return null;
    }

    DataObject jsonUser = content.getObject("user");
    final long userId = jsonUser.getLong("id");
    UserImpl user = (UserImpl) getJDA().getUsersView().get(userId);

    // The user is not yet known to us, maybe due to lazy loading. Try creating it.
    if (user == null)
    {
        // If this presence update doesn't have a user or the status is offline we ignore it
        if (jsonUser.isNull("username") || "offline".equals(content.get("status")))
            return null;
        // We should have somewhat enough information to create this member, so lets do it!
        user = (UserImpl) createMember(content, guildId, guild, jsonUser).getUser();
    }

    if (jsonUser.hasKey("username"))
    {
        // username implies this is an update to a user - fire events and update properties
        getJDA().getEntityBuilder().updateUser(user, jsonUser);
    }

    //Now that we've update the User's info, lets see if we need to set the specific Presence information.
    // This is stored in the Member objects.
    //We set the activities to null to prevent parsing if the cache was disabled
    final DataArray activityArray = !getJDA().isCacheFlagSet(CacheFlag.ACTIVITY) || content.isNull("activities") ? null : content.getArray("activities");
    List<Activity> newActivities = new ArrayList<>();
    boolean parsedActivity = parseActivities(userId, activityArray, newActivities);

    MemberImpl member = (MemberImpl) guild.getMember(user);
    //Create member from presence if not offline
    if (member == null)
    {
        if (jsonUser.isNull("username") || "offline".equals(content.get("status")))
        {
            log.trace("Ignoring incomplete PRESENCE_UPDATE for member with id {} in guild with id {}", userId, guildId);
            return null;
        }
        member = createMember(content, guildId, guild, jsonUser);
    }

    if (getJDA().isCacheFlagSet(CacheFlag.CLIENT_STATUS) && !content.isNull("client_status"))
        handleClientStatus(content, member);

    // Check if activities changed
    if (parsedActivity)
        handleActivities(newActivities, member);

    //The member is already cached, so modify the presence values and fire events as needed.
    OnlineStatus status = OnlineStatus.fromKey(content.getString("status"));
    if (!member.getOnlineStatus().equals(status))
    {
        OnlineStatus oldStatus = member.getOnlineStatus();
        member.setOnlineStatus(status);
        getJDA().getEntityBuilder().updateMemberCache(member);
        getJDA().handleEvent(
            new UserUpdateOnlineStatusEvent(
                getJDA(), responseNumber,
                member, oldStatus));
    }
    return null;
}
 
Example 3
Source File: EntityBuilder.java    From JDA with Apache License 2.0 4 votes vote down vote up
public void createPresence(MemberImpl member, DataObject presenceJson)
{
    if (member == null)
        throw new NullPointerException("Provided member was null!");
    boolean cacheGame = getJDA().isCacheFlagSet(CacheFlag.ACTIVITY);
    boolean cacheStatus = getJDA().isCacheFlagSet(CacheFlag.CLIENT_STATUS);

    DataArray activityArray = !cacheGame || presenceJson.isNull("activities") ? null : presenceJson.getArray("activities");
    DataObject clientStatusJson = !cacheStatus || presenceJson.isNull("client_status") ? null : presenceJson.getObject("client_status");
    OnlineStatus onlineStatus = OnlineStatus.fromKey(presenceJson.getString("status"));
    List<Activity> activities = new ArrayList<>();
    boolean parsedActivity = false;

    if (cacheGame && activityArray != null)
    {
        for (int i = 0; i < activityArray.length(); i++)
        {
            try
            {
                activities.add(createActivity(activityArray.getObject(i)));
                parsedActivity = true;
            }
            catch (Exception ex)
            {
                String userId;
                userId = member.getUser().getId();
                if (LOG.isDebugEnabled())
                    LOG.warn("Encountered exception trying to parse a presence! UserId: {} JSON: {}", userId, activityArray, ex);
                else
                    LOG.warn("Encountered exception trying to parse a presence! UserId: {} Message: {} Enable debug for details", userId, ex.getMessage());
            }
        }
    }
    if (cacheGame && parsedActivity)
        member.setActivities(activities);
    member.setOnlineStatus(onlineStatus);
    if (clientStatusJson != null)
    {
        for (String key : clientStatusJson.keys())
        {
            ClientType type = ClientType.fromKey(key);
            OnlineStatus status = OnlineStatus.fromKey(clientStatusJson.getString(key));
            member.setOnlineStatus(type, status);
        }
    }
}