use of net.dv8tion.jda.internal.requests.RestActionImpl 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;
});
}
use of net.dv8tion.jda.internal.requests.RestActionImpl in project JDA by DV8FromTheWorld.
the class MessageReference method resolve.
/**
* Retrieves the referenced message for this message.
* <br>If the message already exists, it will be returned immediately.
*
* <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 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 update
* Whether to update the already stored message
*
* @throws net.dv8tion.jda.api.exceptions.InsufficientPermissionException
* If this reference refers to 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>
*
* @throws java.lang.IllegalStateException
* If this message reference does not have a channel
*
* @return {@link net.dv8tion.jda.api.requests.RestAction RestAction} - Type: {@link net.dv8tion.jda.api.entities.Message}
*/
@Nonnull
public RestAction<Message> resolve(boolean update) {
checkPermission(Permission.VIEW_CHANNEL);
checkPermission(Permission.MESSAGE_HISTORY);
if (channel == null)
throw new IllegalStateException("Cannot resolve a message without a channel present.");
JDAImpl jda = (JDAImpl) getJDA();
Message referenced = getMessage();
if (referenced != null && !update)
return new CompletedRestAction<>(jda, referenced);
Route.CompiledRoute route = Route.Messages.GET_MESSAGE.compile(getChannelId(), getMessageId());
return new RestActionImpl<>(jda, route, (response, request) -> {
// channel can be null for MessageReferences, but we've already checked for that above, so it is nonnull here
Message created = jda.getEntityBuilder().createMessageWithChannel(response.getObject(), channel, false);
this.referencedMessage = created;
return created;
});
}
use of net.dv8tion.jda.internal.requests.RestActionImpl in project JDA by DV8FromTheWorld.
the class Template method resolve.
/**
* Retrieves a new {@link net.dv8tion.jda.api.entities.templates.Template Template} instance for the given template code.
*
* <p>Possible {@link net.dv8tion.jda.api.requests.ErrorResponse ErrorResponses} include:
* <ul>
* <li>{@link net.dv8tion.jda.api.requests.ErrorResponse#UNKNOWN_GUILD_TEMPLATE Unknown Guild Template}
* <br>The template doesn't exist.</li>
* </ul>
*
* @param api
* The JDA instance
* @param code
* A valid template code
*
* @throws java.lang.IllegalArgumentException
* <ul>
* <li>If the provided code is null or empty</li>
* <li>If the provided code contains a whitespace</li>
* <li>If the provided JDA object is null</li>
* </ul>
*
* @return {@link net.dv8tion.jda.api.requests.RestAction RestAction} - Type: {@link net.dv8tion.jda.api.entities.templates.Template Template}
* <br>The Template object
*/
@Nonnull
public static RestAction<Template> resolve(final JDA api, final String code) {
Checks.notEmpty(code, "code");
Checks.noWhitespace(code, "code");
Checks.notNull(api, "api");
Route.CompiledRoute route = Route.Templates.GET_TEMPLATE.compile(code);
JDAImpl jda = (JDAImpl) api;
return new RestActionImpl<>(api, route, (response, request) -> jda.getEntityBuilder().createTemplate(response.getObject()));
}
use of net.dv8tion.jda.internal.requests.RestActionImpl in project JDA by DV8FromTheWorld.
the class SessionControllerAdapter method getShardedGateway.
@Nonnull
@Override
public ShardedGateway getShardedGateway(@Nonnull JDA api) {
AccountTypeException.check(api.getAccountType(), AccountType.BOT);
return new RestActionImpl<ShardedGateway>(api, Route.Misc.GATEWAY_BOT.compile()) {
@Override
public void handleResponse(Response response, Request<ShardedGateway> request) {
if (response.isOk()) {
DataObject object = response.getObject();
String url = object.getString("url");
int shards = object.getInt("shards");
int concurrency = object.getObject("session_start_limit").getInt("max_concurrency", 1);
request.onSuccess(new ShardedGateway(url, shards, concurrency));
} else if (response.code == 401) {
api.shutdownNow();
request.onFailure(new LoginException("The provided token is invalid!"));
} else {
request.onFailure(response);
}
}
}.priority().complete();
}
use of net.dv8tion.jda.internal.requests.RestActionImpl in project Sx4 by sx4-discord-bot.
the class StarboardHandler method onGuildMessageReactionAdd.
public void onGuildMessageReactionAdd(GuildMessageReactionAddEvent event) {
if (event.getUser().isBot()) {
return;
}
List<Bson> starboardPipeline = List.of(Aggregates.match(Filters.or(Filters.eq("originalMessageId", event.getMessageIdLong()), Filters.eq("messageId", event.getMessageIdLong()))), Aggregates.project(Projections.include("originalMessageId", "channelId")));
List<Bson> pipeline = List.of(Aggregates.match(Filters.eq("_id", event.getGuild().getIdLong())), Aggregates.project(Projections.fields(Projections.include("starboard"), Projections.computed("premium", Operators.lt(Operators.nowEpochSecond(), Operators.ifNull("$premium.endAt", 0L))))), Aggregates.unionWith("starboards", starboardPipeline), Aggregates.group(null, Accumulators.max("messageId", "$originalMessageId"), Accumulators.max("channelId", "$channelId"), Accumulators.max("starboard", "$starboard"), Accumulators.max("premium", "$premium")));
this.bot.getMongo().aggregateGuilds(pipeline).whenComplete((documents, aggregateException) -> {
if (ExceptionUtility.sendErrorMessage(aggregateException)) {
return;
}
if (documents.isEmpty()) {
return;
}
Document data = documents.get(0);
Document starboard = data.get("starboard", MongoDatabase.EMPTY_DOCUMENT);
if (!starboard.get("enabled", false)) {
return;
}
long channelId = starboard.get("channelId", 0L), messageChannelId = data.get("channelId", 0L);
TextChannel messageChannel = messageChannelId == 0L ? event.getChannel() : event.getGuild().getTextChannelById(messageChannelId);
TextChannel channel = channelId == 0L ? null : event.getGuild().getTextChannelById(channelId);
if (channel == null || messageChannel == null) {
return;
}
ReactionEmote emote = event.getReactionEmote();
boolean emoji = emote.isEmoji();
Document emoteData = starboard.get("emote", new Document("name", "⭐"));
if ((emoji && !emote.getEmoji().equals(emoteData.getString("name"))) || (!emoji && (!emoteData.containsKey("id") || emoteData.getLong("id") != emote.getIdLong()))) {
return;
}
Long originalMessageId = data.getLong("messageId");
long messageId = originalMessageId == null ? event.getMessageIdLong() : originalMessageId;
messageChannel.retrieveMessageById(messageId).queue(message -> {
String image = message.getAttachments().stream().filter(Attachment::isImage).map(Attachment::getUrl).findFirst().orElse(null);
Document star = new Document("userId", event.getUser().getIdLong()).append("messageId", messageId).append("guildId", event.getGuild().getIdLong());
this.bot.getMongo().insertStar(star).thenCompose(result -> {
Bson update = Updates.combine(Updates.inc("count", 1), Updates.setOnInsert("originalMessageId", messageId), Updates.setOnInsert("guildId", event.getGuild().getIdLong()), Updates.setOnInsert("channelId", event.getChannel().getIdLong()), Updates.set("content", message.getContentRaw()), Updates.set("authorId", message.getAuthor().getIdLong()));
if (image != null) {
update = Updates.combine(update, Updates.set("image", image));
}
FindOneAndUpdateOptions options = new FindOneAndUpdateOptions().returnDocument(ReturnDocument.AFTER).upsert(true);
return this.bot.getMongo().findAndUpdateStarboard(Filters.eq("originalMessageId", messageId), update, options);
}).thenCompose(updatedData -> {
WebhookMessage webhookMessage = this.getStarboardMessage(starboard, updatedData, event.getGuild(), event.getMember(), emote, data.getBoolean("premium"));
if (webhookMessage == null) {
return CompletableFuture.completedFuture(null);
}
if (updatedData.containsKey("messageId")) {
this.bot.getStarboardManager().editStarboard(updatedData.getLong("messageId"), channel.getIdLong(), starboard.get("webhook", MongoDatabase.EMPTY_DOCUMENT), webhookMessage);
// return null so no update is made in the next stage
return CompletableFuture.completedFuture(null);
} else {
return this.bot.getStarboardManager().sendStarboard(channel, starboard.get("webhook", MongoDatabase.EMPTY_DOCUMENT), webhookMessage);
}
}).whenComplete((createdMessage, exception) -> {
if (exception instanceof CompletionException) {
Throwable cause = exception.getCause();
if (cause instanceof MongoWriteException && ((MongoWriteException) cause).getError().getCategory() == ErrorCategory.DUPLICATE_KEY) {
// duplicate star just ignore
return;
}
}
if (ExceptionUtility.sendErrorMessage(exception)) {
return;
}
if (createdMessage != null) {
Route.CompiledRoute route = Route.Messages.ADD_REACTION.compile(Long.toString(createdMessage.getChannelId()), Long.toString(createdMessage.getId()), EncodingUtil.encodeReaction(emote.getAsReactionCode()), "@me");
new RestActionImpl<>(event.getJDA(), route).queue(null, ErrorResponseException.ignore(ErrorResponse.UNKNOWN_EMOJI, ErrorResponse.MISSING_PERMISSIONS, ErrorResponse.MISSING_ACCESS));
this.bot.getMongo().updateStarboard(Filters.eq("originalMessageId", messageId), Updates.set("messageId", createdMessage.getId())).whenComplete(MongoDatabase.exceptionally());
}
});
});
});
}
Aggregations