Search in sources :

Example 1 with PresenceUpdateEvent

use of sx.blah.discord.handle.impl.events.user.PresenceUpdateEvent in project Discord4J by Discord4J.

the class ShardImpl method updatePresence.

private void updatePresence(StatusType status, ActivityType type, String text, String streamUrl) {
    checkLoggedIn("update presence");
    if (streamUrl != null) {
        if (!DiscordUtils.STREAM_URL_PATTERN.matcher(streamUrl).matches()) {
            throw new IllegalArgumentException("Stream URL must be a twitch.tv url.");
        }
    }
    IUser ourUser = getClient().getOurUser();
    IPresence oldPresence = ourUser.getPresence();
    IPresence newPresence = new Presence(text, streamUrl, status, type);
    if (!newPresence.equals(oldPresence)) {
        ((User) ourUser).setPresence(newPresence);
        getClient().getDispatcher().dispatch(new PresenceUpdateEvent(ourUser, oldPresence, newPresence));
    }
    ws.send(GatewayOps.STATUS_UPDATE, new PresenceUpdateRequest(status, type, text, streamUrl));
}
Also used : User(sx.blah.discord.handle.impl.obj.User) PresenceUpdateRequest(sx.blah.discord.api.internal.json.requests.PresenceUpdateRequest) Presence(sx.blah.discord.handle.impl.obj.Presence) PresenceUpdateEvent(sx.blah.discord.handle.impl.events.user.PresenceUpdateEvent)

Example 2 with PresenceUpdateEvent

use of sx.blah.discord.handle.impl.events.user.PresenceUpdateEvent in project Discord4J by Discord4J.

the class DispatchHandler method presenceUpdate.

private void presenceUpdate(PresenceUpdateEventResponse event) {
    IPresence presence = DiscordUtils.getPresenceFromJSON(event);
    Guild guild = event.guild_id == null ? null : (Guild) client.getGuildByID(Long.parseUnsignedLong(event.guild_id));
    if (guild != null) {
        User user = (User) guild.getUserByID(Long.parseUnsignedLong(event.user.id));
        if (user != null) {
            if (event.user.username != null) {
                // Full object was sent so there is a user change, otherwise all user fields but id would be null
                IUser oldUser = user.copy();
                user = DiscordUtils.getUserFromJSON(shard, event.user);
                client.dispatcher.dispatch(new UserUpdateEvent(oldUser, user));
            }
            if (!user.getPresence().equals(presence)) {
                IPresence oldPresence = user.getPresence();
                user.setPresence(presence);
                client.dispatcher.dispatch(new PresenceUpdateEvent(user, oldPresence, presence));
                Discord4J.LOGGER.debug(LogMarkers.PRESENCES, "User \"{}\" changed presence to {}", user.getName(), user.getPresence());
            }
        }
    }
}
Also used : UserUpdateEvent(sx.blah.discord.handle.impl.events.user.UserUpdateEvent) PresenceUpdateEvent(sx.blah.discord.handle.impl.events.user.PresenceUpdateEvent)

Aggregations

PresenceUpdateEvent (sx.blah.discord.handle.impl.events.user.PresenceUpdateEvent)2 PresenceUpdateRequest (sx.blah.discord.api.internal.json.requests.PresenceUpdateRequest)1 UserUpdateEvent (sx.blah.discord.handle.impl.events.user.UserUpdateEvent)1 Presence (sx.blah.discord.handle.impl.obj.Presence)1 User (sx.blah.discord.handle.impl.obj.User)1