Search in sources :

Example 16 with EntityBuilder

use of net.dv8tion.jda.internal.entities.EntityBuilder 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 17 with EntityBuilder

use of net.dv8tion.jda.internal.entities.EntityBuilder 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)

Example 18 with EntityBuilder

use of net.dv8tion.jda.internal.entities.EntityBuilder in project JDA by DV8FromTheWorld.

the class MessageHistory method retrieveFuture.

/**
 * Retrieves messages from Discord that were sent more recently than the most recently sent message in
 * MessageHistory's history cache ({@link #getRetrievedHistory()}).
 * Use case for this method is for getting more recent messages after jumping to a specific point in history
 * using something like {@link MessageChannel#getHistoryAround(String, int)}.
 * <br>This method works in the same way as {@link #retrievePast(int)}'s Additional Retrieval mode.
 * <p>
 * <b>Note:</b> This method can only be used after {@link net.dv8tion.jda.api.entities.Message Messages} have already
 * been retrieved from Discord.
 * <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}.
 * @throws java.lang.IllegalStateException
 *         If no messages have been retrieved by this MessageHistory.
 *
 * @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>> retrieveFuture(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);
    if (history.isEmpty())
        throw new IllegalStateException("No messages have been retrieved yet, so there is no message to act as a marker to retrieve more recent messages based on.");
    Route.CompiledRoute route = Route.Messages.GET_MESSAGE_HISTORY.compile(channel.getId()).withQueryParams("limit", Integer.toString(amount), "after", String.valueOf(history.firstKey()));
    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);
            }
        }
        for (Iterator<Message> it = messages.descendingIterator(); it.hasNext(); ) {
            Message m = it.next();
            history.put(0, m.getIdLong(), m);
        }
        return messages;
    });
}
Also used : JDAImpl(net.dv8tion.jda.internal.JDAImpl) EntityBuilder(net.dv8tion.jda.internal.entities.EntityBuilder) DataArray(net.dv8tion.jda.api.utils.data.DataArray) InsufficientPermissionException(net.dv8tion.jda.api.exceptions.InsufficientPermissionException) MissingAccessException(net.dv8tion.jda.api.exceptions.MissingAccessException) RestActionImpl(net.dv8tion.jda.internal.requests.RestActionImpl) Route(net.dv8tion.jda.internal.requests.Route) CheckReturnValue(javax.annotation.CheckReturnValue) Nonnull(javax.annotation.Nonnull)

Example 19 with EntityBuilder

use of net.dv8tion.jda.internal.entities.EntityBuilder in project JDA by DV8FromTheWorld.

the class ChannelActionImpl method handleSuccess.

@Override
protected void handleSuccess(Response response, Request<T> request) {
    EntityBuilder builder = api.getEntityBuilder();
    GuildChannel channel;
    switch(type) {
        case TEXT:
            channel = builder.createTextChannel(response.getObject(), guild.getIdLong());
            break;
        case NEWS:
            channel = builder.createNewsChannel(response.getObject(), guild.getIdLong());
            break;
        case VOICE:
            channel = builder.createVoiceChannel(response.getObject(), guild.getIdLong());
            break;
        case STAGE:
            channel = builder.createStageChannel(response.getObject(), guild.getIdLong());
            break;
        case CATEGORY:
            channel = builder.createCategory(response.getObject(), guild.getIdLong());
            break;
        default:
            request.onFailure(new IllegalStateException("Created channel of unknown type!"));
            return;
    }
    request.onSuccess(clazz.cast(channel));
}
Also used : EntityBuilder(net.dv8tion.jda.internal.entities.EntityBuilder)

Aggregations

EntityBuilder (net.dv8tion.jda.internal.entities.EntityBuilder)19 DataArray (net.dv8tion.jda.api.utils.data.DataArray)15 DataObject (net.dv8tion.jda.api.utils.data.DataObject)9 ArrayList (java.util.ArrayList)7 Nonnull (javax.annotation.Nonnull)7 JDAImpl (net.dv8tion.jda.internal.JDAImpl)7 TLongObjectHashMap (gnu.trove.map.hash.TLongObjectHashMap)5 RestActionImpl (net.dv8tion.jda.internal.requests.RestActionImpl)5 Route (net.dv8tion.jda.internal.requests.Route)5 ParsingException (net.dv8tion.jda.api.exceptions.ParsingException)4 CheckReturnValue (javax.annotation.CheckReturnValue)3 GuildImpl (net.dv8tion.jda.internal.entities.GuildImpl)3 MemberImpl (net.dv8tion.jda.internal.entities.MemberImpl)3 TLongObjectMap (gnu.trove.map.TLongObjectMap)2 LinkedList (java.util.LinkedList)2 MessageChannel (net.dv8tion.jda.api.entities.MessageChannel)2 ThreadChannel (net.dv8tion.jda.api.entities.ThreadChannel)2 User (net.dv8tion.jda.api.entities.User)2 InsufficientPermissionException (net.dv8tion.jda.api.exceptions.InsufficientPermissionException)2 MissingAccessException (net.dv8tion.jda.api.exceptions.MissingAccessException)2