Search in sources :

Example 1 with NonThrowingAutoCloseable

use of org.javacord.api.util.NonThrowingAutoCloseable in project Javacord by BtoBastian.

the class TextChannel method typeContinuouslyAfter.

/**
 * Displays the "xyz is typing..." message continuously, starting delayed.
 * The message is continuously displayed if not quit using the returned {@code AutoCloseable}.
 * Sending a message will make the message go away shortly, but it will return immediately if not cancelled using
 * the {@code AutoCloseable}. This can be used in a try-with-resources block like
 * <code>try (NonThrowingAutoCloseable typingIndicator = textChannel.typeContinuouslyAfter(500,
 * TimeUnit.MILLISECONDS, ExceptionLogger.getConsumer(RatelimitException.class)))
 * { /* do lengthy stuff &#42;/ } sendReply();</code>.
 *
 * <p>The typing indicator will be shown delayed. This can be useful if the task you do can be finished in very
 * short time which could cause the typing indicator and the response message being sent at the same time and the
 * typing indicator could be shown for 10 seconds even if the message was sent already. With the delay this is
 * compensated, because if the returned {@code AutoCloseable} is closed before the delay is over, no typing
 * indicator will be sent at all.
 *
 * <p>Any occurring exceptions including ratelimit exceptions are given to the provided {@code exceptionHandler} or
 * ignored if it is {@code null}.
 *
 * @param exceptionHandler The handler that exceptions are given to.
 * @param delay The delay to wait until the first typing indicator is sent.
 * @param timeUnit The time unit of the delay value.
 * @return An auto-closable to stop sending the typing indicator.
 * @see #type()
 * @see #typeContinuously()
 * @see #typeContinuously(Consumer)
 * @see #typeContinuouslyAfter(long, TimeUnit)
 */
default NonThrowingAutoCloseable typeContinuouslyAfter(long delay, TimeUnit timeUnit, Consumer<Throwable> exceptionHandler) {
    // the delegate that does the actual type indicator sending and error handling
    Runnable typeRunnable = () -> {
        try {
            CompletableFuture<?> typeFuture = type();
            if (exceptionHandler != null) {
                typeFuture.exceptionally(throwable -> {
                    exceptionHandler.accept(throwable);
                    return null;
                });
            }
        } catch (Throwable t) {
            ExceptionLogger.getConsumer().accept(t);
        }
    };
    DiscordApi api = getApi();
    // schedule regular type indicator sending
    Future<?> typingIndicator = api.getThreadPool().getScheduler().scheduleWithFixedDelay(typeRunnable, TimeUnit.NANOSECONDS.convert(delay, timeUnit), 8_000_000_000L, TimeUnit.NANOSECONDS);
    // prevent messages from other commands to interrupt the typing indicator too long
    ListenerManager<MessageCreateListener> typingInterruptedListenerManager = api.addMessageCreateListener(event -> {
        if (event.getMessage().getAuthor().isYourself()) {
            typeRunnable.run();
        }
    });
    // auto-closable to cancel the continuously typing indicator
    return () -> {
        typingInterruptedListenerManager.remove();
        typingIndicator.cancel(true);
    };
}
Also used : MessageSet(org.javacord.api.entity.message.MessageSet) Arrays(java.util.Arrays) CompletableFuture(java.util.concurrent.CompletableFuture) MessageCache(org.javacord.api.util.cache.MessageCache) IncomingWebhook(org.javacord.api.entity.webhook.IncomingWebhook) Future(java.util.concurrent.Future) ListenerManager(org.javacord.api.util.event.ListenerManager) ExceptionLogger(org.javacord.api.util.logging.ExceptionLogger) StreamSupport(java.util.stream.StreamSupport) NoSuchElementException(java.util.NoSuchElementException) MessageCreateListener(org.javacord.api.listener.message.MessageCreateListener) PermissionType(org.javacord.api.entity.permission.PermissionType) Webhook(org.javacord.api.entity.webhook.Webhook) Messageable(org.javacord.api.entity.message.Messageable) Predicate(java.util.function.Predicate) TextChannelAttachableListenerManager(org.javacord.api.listener.channel.TextChannelAttachableListenerManager) Message(org.javacord.api.entity.message.Message) TimeUnit(java.util.concurrent.TimeUnit) Consumer(java.util.function.Consumer) List(java.util.List) Stream(java.util.stream.Stream) User(org.javacord.api.entity.user.User) DiscordApi(org.javacord.api.DiscordApi) Optional(java.util.Optional) NonThrowingAutoCloseable(org.javacord.api.util.NonThrowingAutoCloseable) CompletableFuture(java.util.concurrent.CompletableFuture) MessageCreateListener(org.javacord.api.listener.message.MessageCreateListener) DiscordApi(org.javacord.api.DiscordApi)

Aggregations

Arrays (java.util.Arrays)1 List (java.util.List)1 NoSuchElementException (java.util.NoSuchElementException)1 Optional (java.util.Optional)1 CompletableFuture (java.util.concurrent.CompletableFuture)1 Future (java.util.concurrent.Future)1 TimeUnit (java.util.concurrent.TimeUnit)1 Consumer (java.util.function.Consumer)1 Predicate (java.util.function.Predicate)1 Stream (java.util.stream.Stream)1 StreamSupport (java.util.stream.StreamSupport)1 DiscordApi (org.javacord.api.DiscordApi)1 Message (org.javacord.api.entity.message.Message)1 MessageSet (org.javacord.api.entity.message.MessageSet)1 Messageable (org.javacord.api.entity.message.Messageable)1 PermissionType (org.javacord.api.entity.permission.PermissionType)1 User (org.javacord.api.entity.user.User)1 IncomingWebhook (org.javacord.api.entity.webhook.IncomingWebhook)1 Webhook (org.javacord.api.entity.webhook.Webhook)1 TextChannelAttachableListenerManager (org.javacord.api.listener.channel.TextChannelAttachableListenerManager)1