JDA
JDA copied to clipboard
Add `JDA#listenOnce`
Pull Request Etiquette
- [X] I have checked the PRs for upcoming features/bug fixes.
- [X] I have read the contributing guidelines.
Changes
- [X] Internal code
- [X] 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();
});