Here is a small projet that allows you create an event waiter with the discord jda library! If you find a bug or have any suggestion, please contact me!
This projet will allow you to easily create waiters. For instance, making a reaction role or a bingo game will now be child's play!
You have to download the 3 following .java files and add them in a single package:
The only setup step is to register the EventWaiter in your JDABuilder object:
public class Bot {
public static void main(String[] args) {
JDA jda = JDABuilder.createDefault("token")
.addEventListerners(new EventWaiter())
.build();
}
}
Now you have to create a Waiter object:
note that T is a template extending from GenericEvent
- First comes the constructors :
Constructor | Description |
---|---|
Waiter(Class eventType, Predicate conditions, Consumer<WaiterAction> action, boolean autoRemove, Long expirationTime, TimeUnit timeUnit) | The full constructor. |
Waiter(Class eventType, Predicate conditions, Consumer> action, boolean autoRemove) | The constructor without expiration handling. by default the expiration time will be set to 1 minute. |
Waiter() | The empty constructor. You'll have to set the attributes by yourself. |
- Then, here is the list of all the Waiter attributes that you can acces to :
Attribute | Type | Description |
---|---|---|
eventType | Class | the class of the event you'll be waiting for |
conditions | Predicate | all the conditions that an event must meet to execute the waiter's action |
action | Consumer<WaiterAction> | the action that will be executed if the conditions are fulfilled |
autoRemove | boolean | will the waiter be unregistered once the action is executed ? if false, you may want to unregister the waiter by yourself |
expirationTime | long | the time after which the waiter will be unregistered automatically |
timeUnit | TimeUnit | the unit of the previous time |
timeoutAction | Runnable | the action that will be executed once the waiter expires (null if no action) |
You can access to all of them using getter / setters. To access to the event inside of the waiter action, use action.getEvent()
While creating the waiter, the IDE may be lost and erroring/failing at auto-completion. That's why I recommand you to first make a "template" of your constructor by setting all your lambda arguments to null, and then replace the null values by your lambdas:
Waiter<GuildMessageReceivedEvent> waiter = new Waiter<>(
GuildMessageReceivedEvent.class,
null,
null,
true
);
To register your just created waiter:
EventWaiter.register(waiter);
To unregister the waiter while you still have acces to it:
EventWaiter.unregister(waiter);
To unregister the waiter from the performed action:
EventWaiter.unregister(action);
In the examples, I will use these two variables:
Variable | Description |
---|---|
event | the waiter creation event |
e | the event that will trigger the waiter |
- A waiter that will repeat your next message (within 1 minute)
EventWaiter.register(new Waiter<>(
GuildMessageReceivedEvent.class,
e -> (!e.getMessage().equals(event.getMessage()) && e.getAuthor().equals(event.getAuthor())),
action -> action.getEvent().getChannel().sendMessage(action.getEvent().getMessage().getContentRaw()).queue(),
true
));
- A bingo game
int range = 20;
String number = Integer.toString ((int) (Math.random() * range));
System.out.println(number);
Waiter<GuildMessageReceivedEvent> waiter = new Waiter<>();
waiter
.setEventType(GuildMessageReceivedEvent.class)
.setConditions(e -> !e.getMessage().equals(event.getMessage()) && e.getChannel().equals(event.getChannel()))
.setAutoRemove(false)
.setExpirationTime(2L, TimeUnit.MINUTES)
.setTimeoutAction(() -> event.getChannel().sendMessage("no one found :sob: The number was " + number).queue())
.setAction(action -> {
if(action.getEvent().getMessage().getContentRaw().equals(number)) {
action.getEvent().getMessage().addReaction("✅").queue();
action.getEvent().getChannel().sendMessage("GG " + action.getEvent().getAuthor().getAsMention() + "! You found the number! It was " + number).queue();
EventWaiter.unregister(action);
}
else {
action.getEvent().getMessage().addReaction("❌").queue();
action.getEvent().getMessage().delete().queueAfter(5,TimeUnit.SECONDS);
}
});
EventWaiter.register(waiter);
- A reaction role
TODO create a reaction role