private static CompletableFuture < MessageSet > getMessages ( TextChannel channel , int limit , long before , long after ) { CompletableFuture < MessageSet > future = new CompletableFuture <> ( ) ; channel . getApi ( ) . getThreadPool ( ) . getExecutorService ( ) . submit ( ( ) -> { try { // get the initial batch with the first <= 100 messages int initialBatchSize = ( ( limit % 100 ) == 0 ) ? 100 : limit % 100 ; MessageSet initialMessages = requestAsMessages ( channel , initialBatchSize , before , after ) ; // limit <= 100 => initial request got all messages // initialMessages is empty => READ_MESSAGE_HISTORY permission is denied or no more messages available if ( ( limit <= 100 ) || initialMessages . isEmpty ( ) ) { future . complete ( initialMessages ) ; return ; } // calculate the amount and direction of remaining message to get // this will be a multiple of 100 and at least 100 int remainingMessages = limit - initialBatchSize ; int steps = remainingMessages / 100 ; // "before" is set or both are not set boolean older = ( before != - 1 ) || ( after == - 1 ) ; boolean newer = after != - 1 ; // get remaining messages List < MessageSet > messageSets = new ArrayList <> ( ) ; MessageSet lastMessages = initialMessages ; messageSets . add ( lastMessages ) ; for ( int step = 0 ; step < steps ; ++ step ) { lastMessages = requestAsMessages ( channel , 100 , lastMessages . getOldestMessage ( ) . filter ( message -> older ) . map ( DiscordEntity :: getId ) . orElse ( - 1L ) , lastMessages . getNewestMessage ( ) . filter ( message -> newer ) . map ( DiscordEntity :: getId ) . orElse ( - 1L ) ) ; // no more messages available if ( lastMessages . isEmpty ( ) ) { break ; } messageSets . add ( lastMessages ) ; } // combine the message sets future . complete ( new MessageSetImpl ( messageSets . stream ( ) . flatMap ( Collection :: stream ) . collect ( Collectors . toList ( ) ) ) ) ; } catch ( Throwable t ) { future . completeExceptionally ( t ) ; } } ) ; return future ; }
Gets up to a given amount of messages in the given channel .