Class Once.Builder<E extends GenericEvent>

java.lang.Object
net.dv8tion.jda.api.utils.Once.Builder<E>
Type Parameters:
E - Type of the event listened to
Enclosing class:
Once<E extends GenericEvent>

public static class Once.Builder<E extends GenericEvent> extends Object
Builds a one-time event listener, can be reused.
  • Constructor Details

    • Builder

      public Builder(@Nonnull JDA jda, @Nonnull Class<E> eventType)
      Creates a builder for a one-time event listener
      Parameters:
      jda - The JDA instance
      eventType - The event type to listen for
      Throws:
      IllegalArgumentException - If any of the parameters is null
  • Method Details

    • filter

      @Nonnull public Once.Builder<E> filter(@Nonnull Predicate<? super E> filter)
      Adds an event filter, all filters need to return true for the event to be consumed.

      If the filter throws an exception, this listener will unregister itself.

      Parameters:
      filter - The filter to add, returns true if the event can be consumed
      Returns:
      This instance for chaining convenience
      Throws:
      IllegalArgumentException - If the filter is null
    • timeout

      @Nonnull public Once.Builder<E> timeout(@Nonnull Duration timeout)
      Sets the timeout duration, after which the event is no longer listener for.
      Parameters:
      timeout - The duration after which the event is no longer listener for
      Returns:
      This instance for chaining convenience
      Throws:
      IllegalArgumentException - If the timeout is null
    • timeout

      @Nonnull public Once.Builder<E> timeout(@Nonnull Duration timeout, @Nullable Runnable timeoutCallback)
      Sets the timeout duration, after which the event is no longer listener for, and the callback is run.
      Parameters:
      timeout - The duration after which the event is no longer listener for
      timeoutCallback - The callback run after the duration
      Returns:
      This instance for chaining convenience
      Throws:
      IllegalArgumentException - If the timeout is null
    • setTimeoutPool

      @Nonnull public Once.Builder<E> setTimeoutPool(@Nonnull ScheduledExecutorService timeoutPool)
      Sets the thread pool used to schedule timeouts and run its callback.

      By default JDA.getGatewayPool() is used.

      Parameters:
      timeoutPool - The thread pool to use for timeouts
      Returns:
      This instance for chaining convenience
      Throws:
      IllegalArgumentException - If the timeout pool is null
    • subscribe

      @Nonnull public Task<E> subscribe(@Nonnull Consumer<E> callback)
      Starts listening for the event, once.

      The task will be completed after all filters return true.

      Exceptions thrown in blocking and async contexts includes:

      Returns:
      Task returning an event satisfying all preconditions
      Throws:
      IllegalArgumentException - If the callback is null
      See Also: