code: private static Stream<Message> getMessagesAsStream(TextChannel channel, long before, long after) {
          return StreamSupport.stream(Spliterators.spliteratorUnknownSize(new Iterator<Message>() {
              private final DiscordApiImpl api = ((DiscordApiImpl) channel.getApi());
              // before was set or both were not set
              private final boolean older = (before != -1) || (after == -1);
              private final boolean newer = after != -1;
              private long referenceMessageId = older ? before : after;
              private final List<JsonNode> messageJsons = Collections.synchronizedList(new ArrayList<>());
  
              private void ensureMessagesAvailable() {
                  if (messageJsons.isEmpty()) {
                      synchronized (messageJsons) {
                          if (messageJsons.isEmpty()) {
                              messageJsons.addAll(requestAsSortedJsonNodes(
                                      channel,
                                      100,
                                      older ? referenceMessageId : -1,
                                      newer ? referenceMessageId : -1,
                                      older
                              ));
                              if (!messageJsons.isEmpty()) {
                                  referenceMessageId = messageJsons.get(messageJsons.size() - 1).get("id").asLong();
                              }
                          }
                      }
                  }
              }
  
              @Override
              public boolean hasNext() {
                  ensureMessagesAvailable();
                  return !messageJsons.isEmpty();
              }
  
              @Override
              public Message next() {
                  ensureMessagesAvailable();
                  return api.getOrCreateMessage(channel, messageJsons.remove(0));
              }
          }, Spliterator.ORDERED | Spliterator.DISTINCT | Spliterator.NONNULL | Spliterator.CONCURRENT), false);
      }
docs: Gets a stream of messages in the given channel sorted from newest to oldest.
  
  <p>The messages are retrieved in batches synchronously from Discord,
  so consider not using this method from a listener directly.
  
  @param channel The channel of the messages.
  @param before Get messages before the message with this id.
  @param after Get messages after the message with this id.
  
  @return The stream.
  @see #getMessages(TextChannel, int, long, long)