discord-jda / JDA

Java wrapper for the popular chat & VOIP service: Discord https://discord.com
Apache License 2.0
4.26k stars 730 forks source link

Add `JDA#listenOnce` #2683

Closed freya022 closed 3 weeks ago

freya022 commented 3 months ago

Pull Request Etiquette

Changes

Closes Issue: NaN

Description

Adds an helper method to listen for an event, once.

Example: Listening to a message from a channel and a user, after using a slash command:

final Duration timeout = Duration.ofSeconds(5);
event.reply("Reply in " + TimeFormat.RELATIVE.after(timeout) + " if you can!")
       .setEphemeral(true)
       .queue();

event.getJDA().listenOnce(MessageReceivedEvent.class)
        .filter(messageEvent -> messageEvent.getChannel().getIdLong() == event.getChannel().getIdLong())
        .filter(messageEvent -> messageEvent.getAuthor().getIdLong() == event.getUser().getIdLong())
        .timeout(timeout, () -> {
            event.getHook().editOriginal("Timeout!").queue();
        })
        .submit()
        .onSuccess(messageEvent -> {
            event.getHook().editOriginal("You sent: " + messageEvent.getMessage().getContentRaw()).queue();
        });
duncte123 commented 3 months ago

There is an addEventLister method on the shardManager, maybe this helper method could be added to that as well?

freya022 commented 2 months ago

There is an addEventLister method on the shardManager, maybe this helper method could be added to that as well?

This would make things more complicated, if someone has a use case where you'd need to listen once for an event across all shards, let me know

AshlyneS commented 1 month ago

Just a quick thought, but I believe these would also be benifical. 💕

<E extends GenericEvent> Task<E> listenOnce(Class<E> type, Predicate<? super E> filter);

<E extends GenericEvent> Task<E> listenOnce(Class<E> type, Predicate<? super E> filter, Duration duration, @Nullable Runnable callback);

Otherwise at least some way to

jda.listenOnce(new SomethingCommonlyImplemented(argA, argB, etc));
freya022 commented 1 month ago

Just a quick thought, but I believe these would also be benifical. 💕

<E extends GenericEvent> Task<E> listenOnce(Class<E> type, Predicate<? super E> filter);

<E extends GenericEvent> Task<E> listenOnce(Class<E> type, Predicate<? super E> filter, Duration duration, @Nullable Runnable callback);

Otherwise at least some way to

jda.listenOnce(new SomethingCommonlyImplemented(argA, argB, etc));

After discussing: