Replies: 3 comments 4 replies
-
This guide looks great! 🥇 You may want to link to docs throughout the guide to introduce developers to key aspects of documention. |
Beta Was this translation helpful? Give feedback.
3 replies
-
should be |
Beta Was this translation helpful? Give feedback.
1 reply
-
Updated the examples with a ButtonClickEvent one... Not 100% if that is really the best example tho. |
Beta Was this translation helpful? Give feedback.
0 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
-
When using JDA-Utilities, people will quite frequently ask questions regarding the EventWaiter and how you use its
waitForEvent
method.This little discussion should serve as a small guide to assist in answering common questions about it.
Before we start
It is highly recommended to use JDA-Chewtils instead of JDA-Utilities as it is updated and maintained, and also includes new features such as Support for Slashcommands.
JDA-Chewtils is fully compatible with JDA-Utilities and the setup explained here regarding the EventWaiter is also the same for JDA-Chewtils.
What is the EventWaiter?
The name should say it all. This utility, which is part of the common JDA-Utilities package, allows you to wait for a specific event.
Supported are all classes that extend the abstract Event class from JDA. If you have your own custom events extending this class can you try it out with the EventWaiter. It may require some tinkering tho.
Structure of
waitForEvent
The EventWaiter has two specific
waitForEvent
methods: One with and one without a timeout.Common parts
Both methods share the first 3 parts:
This method will wait indefinitely for the
Predicate<T>
to be true, to execute whatever was defined in theConsumer<T>
The second
waitForEvent
method is different in that it has 3 additional variables to define a time, time unit and timeout action when the definedn
time units run out without any true condition.Full Structure:
The above example would wait 1 minute before timing out.
Please note that the last part is actually a
Runnable
and not aPredicate<T>
orConsumer<T>
like the 2nd and 3rd part are.Once a waitForEvent method has a true
Predicate<T>
will it execute whatever was defined in theConsumer<T>
.If it has a timeout defined and the
Predicate<T>
didn't return true in that time will the Runnable be executed.In either case will the EventWaiter stop listening for the event you defined, essentially dropping the event listening in that case.
Examples
Here are some examples. They range from very simple to quite difficult/advanced. If you have any other example to show, let me know and I include them.
Prerequisite: Setup EventWaiter
It's recommended to only have a single EventWaiter instance that you use through a Getter (Get method).
In the examples below will we assume the following basic setup. If you have a different setup for your bot can you freely adjust this to your preferences.
Example 1: Wait for a response.
The bot waits for a response from the user once they send a specific command.
Example 2: Make a choose-system (With Reactions)
Let's the user select whether they like apples or not by clicking on the reactions.
Example 3: Make a choose-system (With Buttons)
Similar to the 2nd example, but with Discord buttons.
Beta Was this translation helpful? Give feedback.
All reactions