Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add JDA#listenOnce #2683

Merged
merged 15 commits into from
Aug 2, 2024
Merged

Conversation

freya022
Copy link
Contributor

@freya022 freya022 commented May 27, 2024

Pull Request Etiquette

Changes

  • Internal code
  • Library interface (affecting end-user code)
  • Documentation
  • Other: _____

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
Copy link
Contributor

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

@freya022
Copy link
Contributor Author

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
Copy link

AshlyneS commented Jul 2, 2024

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
Copy link
Contributor Author

freya022 commented Jul 6, 2024

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:

  • The two first overloads would just be shortcuts, except they bring more confusion on what each parameter is, this will not be added.
  • The last overload might be used in interactions which require waiting for user feedback, each class would configure the event, filters and the outcome of each step. This won't be added as JDA remains a low-level enough library, this type of usage can easily be achieved using an utility class such as:
public abstract class OnceFactory<E extends GenericEvent> {
    private final JDA jda;

    public OnceFactory(JDA jda) {
        this.jda = jda;
    }

    protected Class<E> getType();

    protected void configure(Once.Builder<E> builder);

    public Task<E> create() {
        final var builder = jda.listenOnce(getType());
        configure(builder);

        return builder.submit();
    }
}

src/main/java/net/dv8tion/jda/api/JDA.java Show resolved Hide resolved
src/main/java/net/dv8tion/jda/api/JDA.java Show resolved Hide resolved
src/main/java/net/dv8tion/jda/api/utils/Once.java Outdated Show resolved Hide resolved
src/main/java/net/dv8tion/jda/api/utils/Once.java Outdated Show resolved Hide resolved
src/main/java/net/dv8tion/jda/api/utils/Once.java Outdated Show resolved Hide resolved
src/main/java/net/dv8tion/jda/api/utils/Once.java Outdated Show resolved Hide resolved
@MinnDevelopment MinnDevelopment merged commit 0f2e837 into discord-jda:master Aug 2, 2024
1 check passed
@freya022 freya022 deleted the feature/event-waiter branch August 2, 2024 19:57
mergify bot referenced this pull request in SvenKirschbaum/musikbot-client Aug 17, 2024
[![Mend Renovate](https://app.renovatebot.com/images/banner.svg)](https://renovatebot.com)

This PR contains the following updates:

| Package | Type | Update | Change | Age | Adoption | Passing | Confidence |
|---|---|---|---|---|---|---|---|
| debian | final | digest | `5f7d566` -> `2ccc7e3` |  |  |  |  |
| maven | stage | digest | `6bfb613` -> `7bec384` |  |  |  |  |
| rust | stage | minor | `1.79.0-bookworm` -> `1.80.1-bookworm` | [![age](https://developer.mend.io/api/mc/badges/age/docker/rust/1.80.1?slim=true)](https://docs.renovatebot.com/merge-confidence/) | [![adoption](https://developer.mend.io/api/mc/badges/adoption/docker/rust/1.80.1?slim=true)](https://docs.renovatebot.com/merge-confidence/) | [![passing](https://developer.mend.io/api/mc/badges/compatibility/docker/rust/1.79.0/1.80.1?slim=true)](https://docs.renovatebot.com/merge-confidence/) | [![confidence](https://developer.mend.io/api/mc/badges/confidence/docker/rust/1.79.0/1.80.1?slim=true)](https://docs.renovatebot.com/merge-confidence/) |
| [net.dv8tion:JDA](https://togithub.com/discord-jda/JDA) | compile | patch | `5.0.1` -> `5.0.2` | [![age](https://developer.mend.io/api/mc/badges/age/maven/net.dv8tion:JDA/5.0.2?slim=true)](https://docs.renovatebot.com/merge-confidence/) | [![adoption](https://developer.mend.io/api/mc/badges/adoption/maven/net.dv8tion:JDA/5.0.2?slim=true)](https://docs.renovatebot.com/merge-confidence/) | [![passing](https://developer.mend.io/api/mc/badges/compatibility/maven/net.dv8tion:JDA/5.0.1/5.0.2?slim=true)](https://docs.renovatebot.com/merge-confidence/) | [![confidence](https://developer.mend.io/api/mc/badges/confidence/maven/net.dv8tion:JDA/5.0.1/5.0.2?slim=true)](https://docs.renovatebot.com/merge-confidence/) |
| [com.github.sealedtx:java-youtube-downloader](https://togithub.com/sealedtx/java-youtube-downloader) | compile | patch | `3.2.3` -> `3.2.4` | [![age](https://developer.mend.io/api/mc/badges/age/maven/com.github.sealedtx:java-youtube-downloader/3.2.4?slim=true)](https://docs.renovatebot.com/merge-confidence/) | [![adoption](https://developer.mend.io/api/mc/badges/adoption/maven/com.github.sealedtx:java-youtube-downloader/3.2.4?slim=true)](https://docs.renovatebot.com/merge-confidence/) | [![passing](https://developer.mend.io/api/mc/badges/compatibility/maven/com.github.sealedtx:java-youtube-downloader/3.2.3/3.2.4?slim=true)](https://docs.renovatebot.com/merge-confidence/) | [![confidence](https://developer.mend.io/api/mc/badges/confidence/maven/com.github.sealedtx:java-youtube-downloader/3.2.3/3.2.4?slim=true)](https://docs.renovatebot.com/merge-confidence/) |

---

### Release Notes

<details>
<summary>discord-jda/JDA (net.dv8tion:JDA)</summary>

### [`v5.0.2`](https://togithub.com/discord-jda/JDA/releases/tag/v5.0.2): | Single time event listener

### Overview

This release includes some bug fixes as well as a new event listener feature to add a one-time-use event listener.

##### Once Event Listener ([#&#8203;2683](https://togithub.com/discord-jda/JDA/issues/2683))

A common problem that developers run into, is "waiting" for a specific event in the context of some command. For instance, waiting for a user to add a reaction or reply with a message in response to some prompt.

This can now be achieved using the new [listenOnce](https://docs.jda.wiki/net/dv8tion/jda/api/JDA.html#listenOnce\(java.lang.Class\)) event listener:

```java
// listen for a message event
jda.listenOnce(MessageReceivedEvent.class)
    // filter for specific event
    .filter(event -> event.getChannel().equals(channel))
    .filter(event -> event.getAuthor().equals(user))
    // handle timeout
    .timeout(timeout, () -> hook.editOriginal("Timeout!").queue())
    // subscribe to first event that matches filters
    .subscribe(event -> {
        hook.editOriginal("You sent: " + event.getMessage().getContentRaw()).queue();
    });
```

#### New Features

-   Add `TeamMember.RoleType` by [@&#8203;freya022](https://togithub.com/freya022) in [https://github.com/discord-jda/JDA/pull/2703](https://togithub.com/discord-jda/JDA/pull/2703)
-   Add abstract `createCopy` method in `SelectMenu` by [@&#8203;Kaktushose](https://togithub.com/Kaktushose) in [https://github.com/discord-jda/JDA/pull/2684](https://togithub.com/discord-jda/JDA/pull/2684)
-   Add `JDA#listenOnce` by [@&#8203;freya022](https://togithub.com/freya022) in [https://github.com/discord-jda/JDA/pull/2683](https://togithub.com/discord-jda/JDA/pull/2683)
-   Add support for profile automod by [@&#8203;MinnDevelopment](https://togithub.com/MinnDevelopment) in [https://github.com/discord-jda/JDA/pull/2580](https://togithub.com/discord-jda/JDA/pull/2580)

#### Changes

-   Expand list of retried http error codes by [@&#8203;MinnDevelopment](https://togithub.com/MinnDevelopment) in [https://github.com/discord-jda/JDA/pull/2710](https://togithub.com/discord-jda/JDA/pull/2710)
-   Update to Jackson 2.17.2 by [@&#8203;freya022](https://togithub.com/freya022) in [https://github.com/discord-jda/JDA/pull/2695](https://togithub.com/discord-jda/JDA/pull/2695)

#### Bug Fixes

-   Fix deserialization of interaction components by [@&#8203;WitchBoo](https://togithub.com/WitchBoo) in [https://github.com/discord-jda/JDA/pull/2711](https://togithub.com/discord-jda/JDA/pull/2711)

**Full Changelog**: discord-jda/JDA@v5.0.1...v5.0.2

### Installation

#### Gradle

```gradle
repositories {
    mavenCentral()
}
dependencies {
    implementation("net.dv8tion:JDA:5.0.2")
}
```

#### Maven

```xml
<dependency>
    <groupId>net.dv8tion</groupId>
    <artifactId>JDA</artifactId>
    <version>5.0.2</version> 
</dependency>
```

</details>

<details>
<summary>sealedtx/java-youtube-downloader (com.github.sealedtx:java-youtube-downloader)</summary>

### [`v3.2.4`](https://togithub.com/sealedtx/java-youtube-downloader/releases/tag/3.2.4)

[Compare Source](https://togithub.com/sealedtx/java-youtube-downloader/compare/3.2.3...3.2.4)

added missing itags

</details>

---

### Configuration

📅 **Schedule**: Branch creation - "before 6am on saturday" in timezone Europe/Berlin, Automerge - At any time (no schedule defined).

🚦 **Automerge**: Disabled by config. Please merge this manually once you are satisfied.

♻ **Rebasing**: Whenever PR becomes conflicted, or you tick the rebase/retry checkbox.

👻 **Immortal**: This PR will be recreated if closed unmerged. Get [config help](https://togithub.com/renovatebot/renovate/discussions) if that's undesired.

---

 - [ ] If you want to rebase/retry this PR, check this box

---

This PR was generated by [Mend Renovate](https://www.mend.io/free-developer-tools/renovate/). View the [repository job log](https://developer.mend.io/github/SvenKirschbaum/musikbot-client).
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants