use of net.dv8tion.jda.internal.requests.RestActionImpl in project JDA by DV8FromTheWorld.
the class UserImpl method openPrivateChannel.
@Nonnull
@Override
public RestAction<PrivateChannel> openPrivateChannel() {
return new DeferredRestAction<>(getJDA(), PrivateChannel.class, this::getPrivateChannel, () -> {
Route.CompiledRoute route = Route.Self.CREATE_PRIVATE_CHANNEL.compile();
DataObject body = DataObject.empty().put("recipient_id", getId());
return new RestActionImpl<>(getJDA(), route, body, (response, request) -> {
PrivateChannel priv = api.getEntityBuilder().createPrivateChannel(response.getObject(), this);
UserImpl.this.privateChannelId = priv.getIdLong();
return priv;
});
});
}
use of net.dv8tion.jda.internal.requests.RestActionImpl 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);
});
}
use of net.dv8tion.jda.internal.requests.RestActionImpl 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);
});
}
use of net.dv8tion.jda.internal.requests.RestActionImpl 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));
}
use of net.dv8tion.jda.internal.requests.RestActionImpl 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;
});
}
Aggregations