Search in sources :

Example 1 with PrivateChannel

use of org.javacord.api.entity.channel.PrivateChannel in project Javacord by BtoBastian.

the class PrivateChannelImpl method getOrCreatePrivateChannel.

/**
 * This function is used internally by Javacord to get create and update private channels.
 * It checks if the recipient is known and dispatches a private channel create event if the channel got created.
 *
 * @param api The discord api instance.
 * @param channelId The id of the channel to get.
 * @param userId The id of the recipient of the channel or yourself.
 * @param user The user with the user id.
 *
 * @return The private channel with the id.
 */
public static PrivateChannelImpl getOrCreatePrivateChannel(DiscordApiImpl api, long channelId, long userId, UserImpl user) {
    Optional<PrivateChannel> optionalChannel = api.getPrivateChannelById(channelId);
    if (optionalChannel.isPresent()) {
        // if necessary update private channel information
        PrivateChannelImpl channel = (PrivateChannelImpl) optionalChannel.get();
        if (!channel.getRecipientId().isPresent() && userId != api.getYourself().getId()) {
            channel.updateRecipientId(userId);
        }
        if (!channel.getRecipient().isPresent()) {
            UserImpl recipient;
            if (user == null) {
                recipient = (UserImpl) api.getCachedUserById(userId).orElse(null);
            } else {
                recipient = user;
            }
            if (recipient != null && !recipient.isYourself()) {
                channel.updateRecipient(recipient);
            }
        }
        return channel;
    }
    if (userId == api.getYourself().getId()) {
        return dispatchPrivateChannelCreateEvent(api, new PrivateChannelImpl(api, channelId, null, null));
    }
    UserImpl recipient;
    if (user == null) {
        recipient = (UserImpl) api.getCachedUserById(userId).orElse(null);
    } else {
        recipient = user;
    }
    return dispatchPrivateChannelCreateEvent(api, new PrivateChannelImpl(api, channelId, recipient, userId));
}
Also used : PrivateChannel(org.javacord.api.entity.channel.PrivateChannel) UserImpl(org.javacord.core.entity.user.UserImpl)

Example 2 with PrivateChannel

use of org.javacord.api.entity.channel.PrivateChannel in project Javacord by BtoBastian.

the class ChannelCreateHandler method handlePrivateChannel.

/**
 * Handles a private channel creation.
 *
 * @param channel The channel data.
 */
private void handlePrivateChannel(JsonNode channel) {
    // A CHANNEL_CREATE packet was sent every time a bot account receives a message, see
    // https://github.com/discord/discord-api-docs/issues/184 and
    // https://github.com/discord/discord-api-docs/issues/2248
    UserImpl recipient = new UserImpl(api, channel.get("recipients").get(0), (MemberImpl) null, null);
    if (!recipient.getPrivateChannel().isPresent()) {
        PrivateChannel privateChannel = new PrivateChannelImpl(api, channel.get("id").asText(), recipient, recipient.getId());
        PrivateChannelCreateEvent event = new PrivateChannelCreateEventImpl(privateChannel);
        api.getEventDispatcher().dispatchPrivateChannelCreateEvent(api, recipient, event);
    }
}
Also used : PrivateChannel(org.javacord.api.entity.channel.PrivateChannel) PrivateChannelCreateEventImpl(org.javacord.core.event.channel.user.PrivateChannelCreateEventImpl) UserImpl(org.javacord.core.entity.user.UserImpl) PrivateChannelCreateEvent(org.javacord.api.event.channel.user.PrivateChannelCreateEvent) PrivateChannelImpl(org.javacord.core.entity.channel.PrivateChannelImpl)

Aggregations

PrivateChannel (org.javacord.api.entity.channel.PrivateChannel)2 UserImpl (org.javacord.core.entity.user.UserImpl)2 PrivateChannelCreateEvent (org.javacord.api.event.channel.user.PrivateChannelCreateEvent)1 PrivateChannelImpl (org.javacord.core.entity.channel.PrivateChannelImpl)1 PrivateChannelCreateEventImpl (org.javacord.core.event.channel.user.PrivateChannelCreateEventImpl)1