Class DeferredRestAction<T,R extends RestAction<T>>

java.lang.Object
net.dv8tion.jda.internal.requests.DeferredRestAction<T,R>
All Implemented Interfaces:
RestAction<T>, AuditableRestAction<T>, CacheRestAction<T>

public class DeferredRestAction<T,R extends RestAction<T>> extends Object implements AuditableRestAction<T>, CacheRestAction<T>
  • Constructor Details

    • DeferredRestAction

      public DeferredRestAction(JDA api, Supplier<R> actionSupplier)
    • DeferredRestAction

      public DeferredRestAction(JDA api, Class<T> type, Supplier<T> valueSupplier, Supplier<R> actionSupplier)
  • Method Details

    • getJDA

      @Nonnull public JDA getJDA()
      Description copied from interface: RestAction
      The current JDA instance
      Specified by:
      getJDA in interface RestAction<T>
      Returns:
      The corresponding JDA instance
    • reason

      @Nonnull public AuditableRestAction<T> reason(String reason)
      Description copied from interface: AuditableRestAction
      Applies the specified reason as audit-log reason field.
      When the provided reason is empty or null it will be treated as not set. If the provided reason is longer than 512<T> characters, it will be truncated to fit the limit.

      Reasons for any AuditableRestAction may be retrieved via AuditLogEntry.getReason() in iterable AuditLogPaginationActions from Guild.retrieveAuditLogs()! For guild bans, this is also accessible via Guild.Ban.getReason().

      This will specify the reason via the X-Audit-Log-Reason Request Header.

      Specified by:
      reason in interface AuditableRestAction<T>
      Parameters:
      reason - The reason for this action which should be logged in the Guild's AuditLogs (up to 512<T> characters)
      Returns:
      The current AuditableRestAction instance for chaining convenience
      See Also:
    • setCheck

      @Nonnull public DeferredRestAction<T,R> setCheck(BooleanSupplier checks)
      Description copied from interface: AuditableRestAction
      Sets the last-second checks before finally executing the http request in the queue.
      If the provided supplier evaluates to false or throws an exception this will not be finished. When an exception is thrown from the supplier it will be provided to the failure callback.
      Specified by:
      setCheck in interface AuditableRestAction<T>
      Specified by:
      setCheck in interface CacheRestAction<T>
      Specified by:
      setCheck in interface RestAction<T>
      Parameters:
      checks - The checks to run before executing the request, or null to run no checks
      Returns:
      The current RestAction for chaining convenience
      See Also:
    • getCheck

      @Nullable public BooleanSupplier getCheck()
      Description copied from interface: RestAction
      The current checks for this RestAction.
      Specified by:
      getCheck in interface RestAction<T>
      Returns:
      The current checks, or null if none were set
      See Also:
    • timeout

      @Nonnull public DeferredRestAction<T,R> timeout(long timeout, @Nonnull TimeUnit unit)
      Description copied from interface: AuditableRestAction
      Timeout for this RestAction instance.
      If the request doesn't get executed within the timeout it will fail.

      When a RestAction times out, it will fail with a TimeoutException. This is the same as deadline(System.currentTimeMillis() + unit.toMillis(timeout)).

      Example

      
       action.timeout(10, TimeUnit.SECONDS) // 10 seconds from now
             .queueAfter(20, SECONDS); // request will not be executed within deadline and timeout immediately after 20 seconds
       
      Specified by:
      timeout in interface AuditableRestAction<T>
      Specified by:
      timeout in interface CacheRestAction<T>
      Specified by:
      timeout in interface RestAction<T>
      Parameters:
      timeout - The timeout to use
      unit - Unit for the timeout value
      Returns:
      The same RestAction instance with the applied timeout
      See Also:
    • deadline

      @Nonnull public DeferredRestAction<T,R> deadline(long timestamp)
      Description copied from interface: AuditableRestAction
      Similar to RestAction.timeout(long, TimeUnit) but schedules a deadline at which the request has to be completed.
      If the deadline is reached, the request will fail with a TimeoutException.

      This does not mean that the request will immediately timeout when the deadline is reached. JDA will check the deadline right before executing the request or within intervals in a worker thread. This only means the request will timeout if the deadline has passed.

      Example

      
       action.deadline(System.currentTimeMillis() + 10000) // 10 seconds from now
             .queueAfter(20, SECONDS); // request will not be executed within deadline and timeout immediately after 20 seconds
       
      Specified by:
      deadline in interface AuditableRestAction<T>
      Specified by:
      deadline in interface CacheRestAction<T>
      Specified by:
      deadline in interface RestAction<T>
      Parameters:
      timestamp - Millisecond timestamp at which the request will timeout
      Returns:
      The same RestAction with the applied deadline
      See Also:
    • useCache

      @Nonnull public CacheRestAction<T> useCache(boolean useCache)
      Description copied from interface: CacheRestAction
      Sets whether this request should rely on cached entities, or always retrieve a new one.
      Specified by:
      useCache in interface CacheRestAction<T>
      Parameters:
      useCache - True if the cache should be used when available, even if the entity might be outdated. False, to always request a new instance from the API.
      Returns:
      This RestAction instance
    • setCacheCheck

      public AuditableRestAction<T> setCacheCheck(BooleanSupplier checks)
    • queue

      public void queue(Consumer<? super T> success, Consumer<? super Throwable> failure)
      Description copied from interface: RestAction
      Submits a Request for execution.

      This method is asynchronous

      Example

      
       public static void sendPrivateMessage(JDA jda, String userId, String content)
       {
           // Retrieve the user by their id
           RestAction<User> action = jda.retrieveUserById(userId);
           action.queue(
               // Handle success if the user exists
               (user) -> user.openPrivateChannel().queue(
                   (channel) -> channel.sendMessage(content).queue()),
      
               // Handle failure if the user does not exist (or another issue appeared)
               (error) -> error.printStackTrace()
           );
      
           // Alternatively use submit() to remove nested callbacks
       }
       
      Specified by:
      queue in interface RestAction<T>
      Parameters:
      success - The success callback that will be called at a convenient time for the API. (can be null to use default)
      failure - The failure callback that will be called if the Request encounters an exception at its execution point. (can be null to use default)
      See Also:
    • submit

      @Nonnull public CompletableFuture<T> submit(boolean shouldQueue)
      Description copied from interface: RestAction
      Submits a Request for execution and provides a CompletableFuture representing its completion task.
      Cancelling the returned Future will result in the cancellation of the Request!
      Specified by:
      submit in interface RestAction<T>
      Parameters:
      shouldQueue - Whether the Request should automatically handle rate limitations. (default true)
      Returns:
      Never-null CompletableFuture task representing the completion promise
    • complete

      public T complete(boolean shouldQueue) throws RateLimitedException
      Description copied from interface: RestAction
      Blocks the current Thread and awaits the completion of an RestAction.submit() request.
      Used for synchronous logic.
      Specified by:
      complete in interface RestAction<T>
      Parameters:
      shouldQueue - Whether this should automatically handle rate limitations (default true)
      Returns:
      The response value
      Throws:
      RateLimitedException - If we were rate limited and the shouldQueue is false. Use RestAction.complete() to avoid this Exception.