Search in sources :

Example 1 with UserTypingEvent

use of net.dv8tion.jda.api.events.user.UserTypingEvent in project JDA by DV8FromTheWorld.

the class TypingStartHandler method handleInternally.

@Override
protected Long handleInternally(DataObject content) {
    GuildImpl guild = null;
    if (!content.isNull("guild_id")) {
        long guildId = content.getUnsignedLong("guild_id");
        guild = (GuildImpl) getJDA().getGuildById(guildId);
        if (getJDA().getGuildSetupController().isLocked(guildId))
            return guildId;
        else if (guild == null)
            // Don't cache typing events
            return null;
    }
    final long channelId = content.getLong("channel_id");
    // TODO-v5-unified-channel-cache
    MessageChannel channel = getJDA().getTextChannelsView().get(channelId);
    if (channel == null)
        channel = getJDA().getNewsChannelView().get(channelId);
    if (channel == null)
        channel = getJDA().getPrivateChannelsView().get(channelId);
    if (channel == null)
        // We don't have the channel cached yet. We chose not to cache this event
        return null;
    // because that happen very often and could easily fill up the EventCache if
    // we, for some reason, never get the channel. Especially in an active channel.
    final long userId = content.getLong("user_id");
    User user;
    MemberImpl member = null;
    if (channel instanceof PrivateChannel)
        user = ((PrivateChannel) channel).getUser();
    else
        user = getJDA().getUsersView().get(userId);
    if (!content.isNull("member")) {
        // Try to load member for the typing event
        EntityBuilder entityBuilder = getJDA().getEntityBuilder();
        member = entityBuilder.createMember(guild, content.getObject("member"));
        entityBuilder.updateMemberCache(member);
        user = member.getUser();
    }
    if (user == null)
        // Just like in the comment above, if for some reason we don't have the user
        return null;
    // then we will just throw the event away.
    OffsetDateTime timestamp = Instant.ofEpochSecond(content.getInt("timestamp")).atOffset(ZoneOffset.UTC);
    getJDA().handleEvent(new UserTypingEvent(getJDA(), responseNumber, user, channel, timestamp, member));
    return null;
}
Also used : PrivateChannel(net.dv8tion.jda.api.entities.PrivateChannel) GuildImpl(net.dv8tion.jda.internal.entities.GuildImpl) UserTypingEvent(net.dv8tion.jda.api.events.user.UserTypingEvent) User(net.dv8tion.jda.api.entities.User) MessageChannel(net.dv8tion.jda.api.entities.MessageChannel) OffsetDateTime(java.time.OffsetDateTime) MemberImpl(net.dv8tion.jda.internal.entities.MemberImpl) EntityBuilder(net.dv8tion.jda.internal.entities.EntityBuilder)

Aggregations

OffsetDateTime (java.time.OffsetDateTime)1 MessageChannel (net.dv8tion.jda.api.entities.MessageChannel)1 PrivateChannel (net.dv8tion.jda.api.entities.PrivateChannel)1 User (net.dv8tion.jda.api.entities.User)1 UserTypingEvent (net.dv8tion.jda.api.events.user.UserTypingEvent)1 EntityBuilder (net.dv8tion.jda.internal.entities.EntityBuilder)1 GuildImpl (net.dv8tion.jda.internal.entities.GuildImpl)1 MemberImpl (net.dv8tion.jda.internal.entities.MemberImpl)1