Search in sources :

Example 21 with Route

use of net.dv8tion.jda.internal.requests.Route 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 22 with Route

use of net.dv8tion.jda.internal.requests.Route 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 23 with Route

use of net.dv8tion.jda.internal.requests.Route in project JDA by DV8FromTheWorld.

the class MessageChannel method retrieveMessageById.

/**
 * Attempts to get a {@link net.dv8tion.jda.api.entities.Message Message} from the Discord's servers that has
 * the same id as the id provided.
 * <br>Note: when retrieving a Message, you must retrieve it from the channel it was sent in!
 *
 * <p>The {@link Message#getMember() Message.getMember()} method will always return null for the resulting message.
 * To retrieve the member you can use {@code getGuild().retrieveMember(message.getAuthor())}.
 *
 * <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#MISSING_PERMISSIONS MISSING_PERMISSIONS}
 *     <br>The request was attempted after the account lost {@link net.dv8tion.jda.api.Permission#MESSAGE_HISTORY Permission.MESSAGE_HISTORY}
 *         in the {@link net.dv8tion.jda.api.entities.TextChannel TextChannel}.</li>
 *
 *     <li>{@link net.dv8tion.jda.api.requests.ErrorResponse#UNKNOWN_MESSAGE UNKNOWN_MESSAGE}
 *     <br>The provided {@code id} does not refer to a message sent in this channel or the message has already been deleted.</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>
 *
 * @param  messageId
 *         The id of the sought after Message
 *
 * @throws net.dv8tion.jda.api.exceptions.AccountTypeException
 *         If the currently logged in account is not from {@link net.dv8tion.jda.api.AccountType#BOT AccountType.BOT}
 * @throws IllegalArgumentException
 *         if the provided {@code messageId} is null or empty.
 * @throws net.dv8tion.jda.api.exceptions.InsufficientPermissionException
 *         If this is a {@link net.dv8tion.jda.api.entities.TextChannel TextChannel} and the logged in account does not have
 *         <ul>
 *             <li>{@link net.dv8tion.jda.api.Permission#VIEW_CHANNEL Permission.VIEW_CHANNEL}</li>
 *             <li>{@link net.dv8tion.jda.api.Permission#MESSAGE_HISTORY Permission.MESSAGE_HISTORY}</li>
 *         </ul>
 *
 * @return {@link net.dv8tion.jda.api.requests.RestAction RestAction} - Type: Message
 *         <br>The Message defined by the provided id.
 */
@Nonnull
@CheckReturnValue
default RestAction<Message> retrieveMessageById(@Nonnull String messageId) {
    AccountTypeException.check(getJDA().getAccountType(), AccountType.BOT);
    Checks.isSnowflake(messageId, "Message ID");
    JDAImpl jda = (JDAImpl) getJDA();
    Route.CompiledRoute route = Route.Messages.GET_MESSAGE.compile(getId(), messageId);
    return new RestActionImpl<>(jda, route, (response, request) -> jda.getEntityBuilder().createMessageWithChannel(response.getObject(), MessageChannel.this, false));
}
Also used : RestActionImpl(net.dv8tion.jda.internal.requests.RestActionImpl) AuditableRestActionImpl(net.dv8tion.jda.internal.requests.restaction.AuditableRestActionImpl) JDAImpl(net.dv8tion.jda.internal.JDAImpl) Route(net.dv8tion.jda.internal.requests.Route) CheckReturnValue(javax.annotation.CheckReturnValue) Nonnull(javax.annotation.Nonnull)

Example 24 with Route

use of net.dv8tion.jda.internal.requests.Route 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 25 with Route

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

Aggregations

Route (net.dv8tion.jda.internal.requests.Route)31 Nonnull (javax.annotation.Nonnull)24 RestActionImpl (net.dv8tion.jda.internal.requests.RestActionImpl)24 DataObject (net.dv8tion.jda.api.utils.data.DataObject)12 JDAImpl (net.dv8tion.jda.internal.JDAImpl)12 InsufficientPermissionException (net.dv8tion.jda.api.exceptions.InsufficientPermissionException)10 AuditableRestActionImpl (net.dv8tion.jda.internal.requests.restaction.AuditableRestActionImpl)10 CheckReturnValue (javax.annotation.CheckReturnValue)9 DataArray (net.dv8tion.jda.api.utils.data.DataArray)8 EntityBuilder (net.dv8tion.jda.internal.entities.EntityBuilder)5 JDA (net.dv8tion.jda.api.JDA)3 HierarchyException (net.dv8tion.jda.api.exceptions.HierarchyException)3 MissingAccessException (net.dv8tion.jda.api.exceptions.MissingAccessException)3 ArrayList (java.util.ArrayList)2 LinkedList (java.util.LinkedList)2 List (java.util.List)2 DeferredRestAction (net.dv8tion.jda.internal.requests.DeferredRestAction)2 WebhookEmbed (club.minnced.discord.webhook.send.WebhookEmbed)1 WebhookEmbedBuilder (club.minnced.discord.webhook.send.WebhookEmbedBuilder)1 WebhookMessage (club.minnced.discord.webhook.send.WebhookMessage)1