Search in sources :

Example 51 with DataArray

use of net.dv8tion.jda.api.utils.data.DataArray in project JDA by DV8FromTheWorld.

the class MessageUpdateHandler method handleMessageEmbed.

private Long handleMessageEmbed(DataObject content) {
    EntityBuilder builder = getJDA().getEntityBuilder();
    final long messageId = content.getLong("id");
    final long channelId = content.getLong("channel_id");
    LinkedList<MessageEmbed> embeds = new LinkedList<>();
    // TODO-v5-unified-channel-cache
    // TODO-v5: handle for threads.
    MessageChannel channel = getJDA().getTextChannelsView().get(channelId);
    if (channel == null)
        channel = getJDA().getNewsChannelById(channelId);
    if (channel == null)
        channel = getJDA().getPrivateChannelsView().get(channelId);
    if (channel == null) {
        getJDA().getEventCache().cache(EventCache.Type.CHANNEL, channelId, responseNumber, allContent, this::handle);
        EventCache.LOG.debug("Received message update for embeds for a channel/group that JDA does not have cached yet.");
        return null;
    }
    DataArray embedsJson = content.getArray("embeds");
    for (int i = 0; i < embedsJson.length(); i++) embeds.add(builder.createMessageEmbed(embedsJson.getObject(i)));
    getJDA().handleEvent(new MessageEmbedEvent(getJDA(), responseNumber, messageId, channel, embeds));
    return null;
}
Also used : MessageEmbedEvent(net.dv8tion.jda.api.events.message.MessageEmbedEvent) EntityBuilder(net.dv8tion.jda.internal.entities.EntityBuilder) LinkedList(java.util.LinkedList) DataArray(net.dv8tion.jda.api.utils.data.DataArray)

Example 52 with DataArray

use of net.dv8tion.jda.api.utils.data.DataArray in project JDA by DV8FromTheWorld.

the class ReadyHandler method handleInternally.

@Override
protected Long handleInternally(DataObject content) {
    EntityBuilder builder = getJDA().getEntityBuilder();
    DataArray guilds = content.getArray("guilds");
    // Make sure we don't have any duplicates here!
    TLongObjectMap<DataObject> distinctGuilds = new TLongObjectHashMap<>();
    for (int i = 0; i < guilds.length(); i++) {
        DataObject guild = guilds.getObject(i);
        long id = guild.getUnsignedLong("id");
        DataObject previous = distinctGuilds.put(id, guild);
        if (previous != null)
            WebSocketClient.LOG.warn("Found duplicate guild for id {} in ready payload", id);
    }
    DataObject selfJson = content.getObject("user");
    // Used to update SelfUser#getApplicationId
    selfJson.put(// Used to update SelfUser#getApplicationId
    "application_id", content.optObject("application").map(obj -> obj.getUnsignedLong("id")).orElse(selfJson.getUnsignedLong("id")));
    builder.createSelfUser(selfJson);
    if (getJDA().getGuildSetupController().setIncompleteCount(distinctGuilds.size())) {
        distinctGuilds.forEachEntry((id, guild) -> {
            getJDA().getGuildSetupController().onReady(id, guild);
            return true;
        });
    }
    handleReady(content);
    return null;
}
Also used : DataObject(net.dv8tion.jda.api.utils.data.DataObject) TLongObjectHashMap(gnu.trove.map.hash.TLongObjectHashMap) EntityBuilder(net.dv8tion.jda.internal.entities.EntityBuilder) DataArray(net.dv8tion.jda.api.utils.data.DataArray)

Example 53 with DataArray

use of net.dv8tion.jda.api.utils.data.DataArray in project JDA by DV8FromTheWorld.

the class IInviteContainerMixin method retrieveInvites.

@Nonnull
@Override
default RestAction<List<Invite>> retrieveInvites() {
    checkPermission(Permission.MANAGE_CHANNEL);
    final Route.CompiledRoute route = Route.Invites.GET_CHANNEL_INVITES.compile(getId());
    JDAImpl jda = (JDAImpl) getJDA();
    return new RestActionImpl<>(jda, route, (response, request) -> {
        EntityBuilder entityBuilder = jda.getEntityBuilder();
        DataArray array = response.getArray();
        List<Invite> invites = new ArrayList<>(array.length());
        for (int i = 0; i < array.length(); i++) invites.add(entityBuilder.createInvite(array.getObject(i)));
        return Collections.unmodifiableList(invites);
    });
}
Also used : RestActionImpl(net.dv8tion.jda.internal.requests.RestActionImpl) ArrayList(java.util.ArrayList) JDAImpl(net.dv8tion.jda.internal.JDAImpl) EntityBuilder(net.dv8tion.jda.internal.entities.EntityBuilder) Route(net.dv8tion.jda.internal.requests.Route) DataArray(net.dv8tion.jda.api.utils.data.DataArray) Invite(net.dv8tion.jda.api.entities.Invite) Nonnull(javax.annotation.Nonnull)

Example 54 with DataArray

use of net.dv8tion.jda.api.utils.data.DataArray in project JDA by DV8FromTheWorld.

the class MessageChannel method retrievePinnedMessages.

/**
 * Retrieves a List of {@link net.dv8tion.jda.api.entities.Message Messages} that have been pinned in this channel.
 * <br>If no messages have been pinned, this retrieves an empty List.
 *
 * <p>The following {@link net.dv8tion.jda.api.requests.ErrorResponse ErrorResponses} are possible:
 * <ul>
 *     <li>{@link net.dv8tion.jda.api.requests.ErrorResponse#MISSING_ACCESS MISSING_ACCESS}
 *     <br>The request was attempted after the account lost access to the {@link net.dv8tion.jda.api.entities.Guild Guild}
 *         typically due to being kicked or removed, or after {@link net.dv8tion.jda.api.Permission#VIEW_CHANNEL Permission.VIEW_CHANNEL}
 *         was revoked in the {@link net.dv8tion.jda.api.entities.TextChannel TextChannel}</li>
 *
 *     <li>{@link net.dv8tion.jda.api.requests.ErrorResponse#UNKNOWN_CHANNEL UNKNOWN_CHANNEL}
 *     <br>The request was attempted after the channel was deleted.</li>
 * </ul>
 *
 * @throws net.dv8tion.jda.api.exceptions.InsufficientPermissionException
 *         If this is a TextChannel and this account does not have
 *         {@link net.dv8tion.jda.api.Permission#VIEW_CHANNEL Permission.VIEW_CHANNEL}
 *
 * @return {@link net.dv8tion.jda.api.requests.RestAction RestAction} - Type: List{@literal <}{@link net.dv8tion.jda.api.entities.Message}{@literal >}
 *         <br>Retrieves an immutable list of pinned messages
 */
@Nonnull
@CheckReturnValue
default RestAction<List<Message>> retrievePinnedMessages() {
    JDAImpl jda = (JDAImpl) getJDA();
    Route.CompiledRoute route = Route.Messages.GET_PINNED_MESSAGES.compile(getId());
    return new RestActionImpl<>(jda, route, (response, request) -> {
        LinkedList<Message> pinnedMessages = new LinkedList<>();
        EntityBuilder builder = jda.getEntityBuilder();
        DataArray pins = response.getArray();
        for (int i = 0; i < pins.length(); i++) {
            pinnedMessages.add(builder.createMessageWithChannel(pins.getObject(i), MessageChannel.this, false));
        }
        return Collections.unmodifiableList(pinnedMessages);
    });
}
Also used : RestActionImpl(net.dv8tion.jda.internal.requests.RestActionImpl) AuditableRestActionImpl(net.dv8tion.jda.internal.requests.restaction.AuditableRestActionImpl) JDAImpl(net.dv8tion.jda.internal.JDAImpl) EntityBuilder(net.dv8tion.jda.internal.entities.EntityBuilder) Route(net.dv8tion.jda.internal.requests.Route) DataArray(net.dv8tion.jda.api.utils.data.DataArray) CheckReturnValue(javax.annotation.CheckReturnValue) Nonnull(javax.annotation.Nonnull)

Example 55 with DataArray

use of net.dv8tion.jda.api.utils.data.DataArray in project JDA by DV8FromTheWorld.

the class MessageHistory method retrievePast.

/**
 * Retrieves messages from Discord that were sent before the oldest sent message in MessageHistory's history cache
 * ({@link #getRetrievedHistory()}).
 * <br>Can only retrieve a <b>maximum</b> of {@code 100} messages at a time.
 * <br>This method has 2 modes of operation: initial retrieval and additional retrieval.
 * <ul>
 *     <li><b>Initial Retrieval</b>
 *     <br>This mode is what is used when no {@link net.dv8tion.jda.api.entities.Message Messages} have been retrieved
 *         yet ({@link #getRetrievedHistory()}'s size is 0). Initial retrieval starts from the most recent message sent
 *         to the channel and retrieves backwards from there. So, if 50 messages are retrieved during this mode, the
 *         most recent 50 messages will be retrieved.</li>
 *
 *     <li><b>Additional Retrieval</b>
 *     <br>This mode is used once some {@link net.dv8tion.jda.api.entities.Message Messages} have already been retrieved
 *         from Discord and are stored in MessageHistory's history ({@link #getRetrievedHistory()}). When retrieving
 *         messages in this mode, MessageHistory will retrieve previous messages starting from the oldest message
 *         stored in MessageHistory.
 *     <br>E.g: If you initially retrieved 10 messages, the next call to this method to retrieve 10 messages would
 *         retrieve the <i>next</i> 10 messages, starting from the oldest message of the 10 previously retrieved messages.</li>
 * </ul>
 * <p>
 * Possible {@link net.dv8tion.jda.api.requests.ErrorResponse ErrorResponses} include:
 * <ul>
 *     <li>{@link net.dv8tion.jda.api.requests.ErrorResponse#UNKNOWN_MESSAGE UNKNOWN_MESSAGE}
 *     <br>Can occur if retrieving in Additional Mode and the Message being used as the marker for the last retrieved
 *         Message was deleted. Currently, to fix this, you need to create a new
 *         {@link net.dv8tion.jda.api.entities.MessageHistory MessageHistory} instance.</li>
 *
 *     <li>{@link net.dv8tion.jda.api.requests.ErrorResponse#MISSING_ACCESS MISSING_ACCESS}
 *     <br>Can occur if the request for history retrieval was executed <i>after</i> JDA lost access to the Channel,
 *         typically due to the account being removed from the {@link net.dv8tion.jda.api.entities.Guild Guild}.</li>
 *
 *     <li>{@link net.dv8tion.jda.api.requests.ErrorResponse#MISSING_PERMISSIONS MISSING_PERMISSIONS}
 *     <br>Can occur if the request for history retrieval was executed <i>after</i> JDA lost the
 *         {@link net.dv8tion.jda.api.Permission#MESSAGE_HISTORY} permission.</li>
 *
 *     <li>{@link net.dv8tion.jda.api.requests.ErrorResponse#UNKNOWN_CHANNEL UNKNOWN_CHANNEL}
 *     <br>The send request was attempted after the channel was deleted.</li>
 * </ul>
 *
 * @param  amount
 *         The amount of {@link net.dv8tion.jda.api.entities.Message Messages} to retrieve.
 *
 * @throws java.lang.IllegalArgumentException
 *         The the {@code amount} is less than {@code 1} or greater than {@code 100}.
 *
 * @return {@link net.dv8tion.jda.api.requests.RestAction RestAction} -
 *         Type: {@link java.util.List List}{@literal <}{@link net.dv8tion.jda.api.entities.Message Message}{@literal >}
 *         <br>Retrieved Messages are placed in a List and provided in order of most recent to oldest with most recent
 *         starting at index 0. If the list is empty, there were no more messages left to retrieve.
 */
@Nonnull
@CheckReturnValue
public RestAction<List<Message>> retrievePast(int amount) {
    if (amount > 100 || amount < 1)
        throw new IllegalArgumentException("Message retrieval limit is between 1 and 100 messages. No more, no less. Limit provided: " + amount);
    Route.CompiledRoute route = Route.Messages.GET_MESSAGE_HISTORY.compile(channel.getId()).withQueryParams("limit", Integer.toString(amount));
    if (!history.isEmpty())
        route = route.withQueryParams("before", String.valueOf(history.lastKey()));
    JDAImpl jda = (JDAImpl) getJDA();
    return new RestActionImpl<>(jda, route, (response, request) -> {
        EntityBuilder builder = jda.getEntityBuilder();
        LinkedList<Message> messages = new LinkedList<>();
        DataArray historyJson = response.getArray();
        for (int i = 0; i < historyJson.length(); i++) {
            try {
                messages.add(builder.createMessageWithChannel(historyJson.getObject(i), channel, false));
            } catch (Exception e) {
                LOG.warn("Encountered exception when retrieving messages ", e);
            }
        }
        messages.forEach(msg -> history.put(msg.getIdLong(), msg));
        return messages;
    });
}
Also used : RestActionImpl(net.dv8tion.jda.internal.requests.RestActionImpl) JDAImpl(net.dv8tion.jda.internal.JDAImpl) EntityBuilder(net.dv8tion.jda.internal.entities.EntityBuilder) Route(net.dv8tion.jda.internal.requests.Route) DataArray(net.dv8tion.jda.api.utils.data.DataArray) InsufficientPermissionException(net.dv8tion.jda.api.exceptions.InsufficientPermissionException) MissingAccessException(net.dv8tion.jda.api.exceptions.MissingAccessException) CheckReturnValue(javax.annotation.CheckReturnValue) Nonnull(javax.annotation.Nonnull)

Aggregations

DataArray (net.dv8tion.jda.api.utils.data.DataArray)67 DataObject (net.dv8tion.jda.api.utils.data.DataObject)40 Nonnull (javax.annotation.Nonnull)21 EntityBuilder (net.dv8tion.jda.internal.entities.EntityBuilder)14 InsufficientPermissionException (net.dv8tion.jda.api.exceptions.InsufficientPermissionException)10 JDAImpl (net.dv8tion.jda.internal.JDAImpl)9 TLongObjectHashMap (gnu.trove.map.hash.TLongObjectHashMap)8 ArrayList (java.util.ArrayList)8 Route (net.dv8tion.jda.internal.requests.Route)8 RestActionImpl (net.dv8tion.jda.internal.requests.RestActionImpl)7 Collectors (java.util.stream.Collectors)6 CheckReturnValue (javax.annotation.CheckReturnValue)6 TemplateRole (net.dv8tion.jda.api.entities.templates.TemplateRole)5 ParsingException (net.dv8tion.jda.api.exceptions.ParsingException)5 GuildImpl (net.dv8tion.jda.internal.entities.GuildImpl)5 Bean (at.xirado.bean.Bean)4 java.util (java.util)4 OnlineStatus (net.dv8tion.jda.api.OnlineStatus)4 Template (net.dv8tion.jda.api.entities.templates.Template)4 CacheView (net.dv8tion.jda.api.utils.cache.CacheView)4