/** * Creates a new {@link MessageHistory MessageHistory} object for each call of this method. * <br>MessageHistory is <b>NOT</b> an internal message cache, but rather it queries the Discord servers for previously sent messages. * * @throws net.dv8tion.jda.core.exceptions.InsufficientPermissionException * If this is a {@link net.dv8tion.jda.core.entities.TextChannel TextChannel} * and the currently logged in account does not have the permission {@link net.dv8tion.jda.core.Permission#MESSAGE_HISTORY MESSAGE_HISTORY} * * @return A {@link net.dv8tion.jda.core.entities.MessageHistory MessageHistory} related to this channel. */ default MessageHistory getHistory() { return new MessageHistory(this); }
default MessageHistory.MessageRetrieveAction getHistoryAfter(String messageId, int limit) return MessageHistory.getHistoryAfter(this, messageId).limit(limit);
default MessageHistory.MessageRetrieveAction getHistoryAround(String messageId, int limit) return MessageHistory.getHistoryAround(this, messageId).limit(limit);
private void loadMessages(MessageHistory history, int toDelete, Set<Long> userIds, Consumer<List<Message>> consumer) { long maxMessageAge = (System.currentTimeMillis() - TimeUnit.DAYS.toMillis(14) - MiscUtil.DISCORD_EPOCH) << MiscUtil.TIMESTAMP_OFFSET; List<Message> messages = new ArrayList<>(); history.retrievePast(toDelete).queue(historyMessages -> { if (historyMessages.isEmpty()) { consumer.accept(messages); return; } for (Message historyMessage : historyMessages) { if (historyMessage.isPinned() || historyMessage.getIdLong() < maxMessageAge) { continue; } if (userIds != null && !userIds.contains(historyMessage.getAuthor().getIdLong())) { continue; } if (messages.size() >= toDelete) { consumer.accept(messages); return; } messages.add(historyMessage); } consumer.accept(messages); }); }
return; MessageHistory history = new MessageHistory(channel); int toRetrieve = amount; int i = 0; outer: while (toRetrieve > 0) { if (history.retrievePast((targetUser == null ? Math.min(toRetrieve, 100) : 100)).complete().isEmpty()) { break; for (Message msg : history.getRetrievedHistory()) { if (msg.getCreationTime().plusWeeks(2).isBefore(OffsetDateTime.now())) break outer; if (msg.getId().equals(message.getId())) continue;
checkArguments(channel, messageId); Route.CompiledRoute route = Route.Messages.GET_MESSAGE_HISTORY.compile(channel.getId()).withQueryParams("around", messageId); return new MessageRetrieveAction(route, channel);
return new RestAction<List<Message>>(getJDA(), route)
/** * Used to get a Message from the set of already retrieved message via it's message Id. * <br>If a Message with the provided id has not already been retrieved (thus, doesn't not exist in this MessageHistory * object), then this method returns null. * <p> * <b>Note:</b> This methods is not the same as {@link MessageChannel#getMessageById(String)}, which itself queries * Discord. This method is for getting a message that has already been retrieved by this MessageHistory object. * * @param id * The id of the requested Message. * * @throws java.lang.IllegalArgumentException * If the provided {@code id} is null or empty. * @throws java.lang.NumberFormatException * If the provided {@code id} cannot be parsed by {@link Long#parseLong(String)} * * @return Possibly-null Message with the same {@code id} as the one provided. */ public Message getMessageById(String id) { return getMessageById(MiscUtil.parseSnowflake(id)); }
default MessageHistory.MessageRetrieveAction getHistoryBefore(String messageId, int limit) return MessageHistory.getHistoryBefore(this, messageId).limit(limit);
.retrievePast(100) .complete() .stream()
checkArguments(channel, messageId); Route.CompiledRoute route = Route.Messages.GET_MESSAGE_HISTORY.compile(channel.getId()).withQueryParams("after", messageId); return new MessageRetrieveAction(route, channel);
route = route.withQueryParams("before", String.valueOf(history.lastKey())); return new RestAction<List<Message>>(getJDA(), route)
do { int part = Math.min(MAX_BULK_SIZE, totalMessages); List<Message> messages = history.retrievePast(part).complete(); if (messages.isEmpty()) { break;
@Override protected void handleResponse(Response response, Request<MessageHistory> request) { if (!response.isOk()) { request.onFailure(response); return; } final MessageHistory result = new MessageHistory(channel); final JSONArray array = response.getArray(); final EntityBuilder builder = api.get().getEntityBuilder(); for (int i = 0; i < array.length(); i++) { try { JSONObject obj = array.getJSONObject(i); result.history.put(obj.getLong("id"), builder.createMessage(obj, channel, false)); } catch (JSONException | NullPointerException e) { LOG.warn("Encountered exception in MessagePagination", e); } } request.onSuccess(result); } }
checkArguments(channel, messageId); Route.CompiledRoute route = Route.Messages.GET_MESSAGE_HISTORY.compile(channel.getId()).withQueryParams("before", messageId); return new MessageRetrieveAction(route, channel);
@Override public void onCommand(User sender, GuildWrapper guild, TextChannel channel, Message message, String[] args, Member member) { if (args.length == 1 && args[0].matches("[0-9]{18,22}")) { if (!guild.getGuild().getSelfMember().hasPermission(channel, Permission.MESSAGE_MANAGE)) { MessageUtils.sendErrorMessage("I need the `Manage Messages` permission in order to pin!", channel); return; } Message msg = channel.getMessageById(args[0].trim()).complete(); if (msg == null) { MessageUtils.sendErrorMessage("That message could not be found!", channel); return; } msg.pin().complete(); channel.getHistory().retrievePast(1).complete().get(0).delete().queue(); } else if (args.length != 0) { if (!guild.getGuild().getSelfMember().hasPermission(channel, Permission.MESSAGE_MANAGE)) { MessageUtils.sendErrorMessage("I need the `Manage Messages` permission in order to pin!", channel); return; } Message msg = channel.sendMessage(new EmbedBuilder().setTitle(sender.getName(), null) .setThumbnail(MessageUtils.getAvatar(sender)).setDescription(MessageUtils.getMessage(args, 0)) .build()).complete(); msg.pin().complete(); channel.getHistory().retrievePast(1).complete().get(0).delete().queue(); } else { MessageUtils.sendUsage(this, channel, sender, args); } }
context.getChannel().getHistory().retrievePast(2).queue(messages -> { if (messages.size() != 2) { return;
if (reaction != null) { if (reaction.getCount() == 5) { message.pin().queue((aVoid) -> event.getChannel().getHistory().retrievePast(1).complete().get(0) .delete().queue());