Search in sources :

Example 1 with EventsEnum

use of me.piggypiglet.gary.core.objects.enums.EventsEnum in project Gary by help-chat.

the class EventUtils method pullEvent.

private static GenericEvent pullEvent(EventsEnum event, JDA jda) {
    final Logger logger = LoggerFactory.getLogger("Event Puller");
    final AtomicReference<GenericEvent> pulledEvent = new AtomicReference<>();
    EventListener listener = l -> {
        if (EventsEnum.fromEvent(l) == event) {
            pulledEvent.set(l);
        }
    };
    logger.info("Adding temporary event listener for " + event.name());
    jda.addEventListener(listener);
    // exception will only be thrown on gary shutdown, no point in printing stack trace.
    while (pulledEvent.get() == null) try {
        Thread.sleep(100);
    } catch (Exception ignored) {
    }
    jda.removeEventListener(listener);
    logger.info("Removing temporary event listener for " + event.name());
    return pulledEvent.get();
}
Also used : Message(net.dv8tion.jda.api.entities.Message) GuildMessageReceivedEvent(net.dv8tion.jda.api.events.message.guild.GuildMessageReceivedEvent) JDA(net.dv8tion.jda.api.JDA) Logger(org.slf4j.Logger) EventsEnum(me.piggypiglet.gary.core.objects.enums.EventsEnum) LoggerFactory(org.slf4j.LoggerFactory) CompletableFuture(java.util.concurrent.CompletableFuture) TextChannel(net.dv8tion.jda.api.entities.TextChannel) AtomicReference(java.util.concurrent.atomic.AtomicReference) User(net.dv8tion.jda.api.entities.User) Future(java.util.concurrent.Future) EventListener(net.dv8tion.jda.api.hooks.EventListener) GuildMessageReactionAddEvent(net.dv8tion.jda.api.events.message.guild.react.GuildMessageReactionAddEvent) Constants(me.piggypiglet.gary.core.objects.Constants) MessageReaction(net.dv8tion.jda.api.entities.MessageReaction) GenericEvent(net.dv8tion.jda.api.events.GenericEvent) GenericEvent(net.dv8tion.jda.api.events.GenericEvent) AtomicReference(java.util.concurrent.atomic.AtomicReference) EventListener(net.dv8tion.jda.api.hooks.EventListener) Logger(org.slf4j.Logger)

Aggregations

CompletableFuture (java.util.concurrent.CompletableFuture)1 Future (java.util.concurrent.Future)1 AtomicReference (java.util.concurrent.atomic.AtomicReference)1 Constants (me.piggypiglet.gary.core.objects.Constants)1 EventsEnum (me.piggypiglet.gary.core.objects.enums.EventsEnum)1 JDA (net.dv8tion.jda.api.JDA)1 Message (net.dv8tion.jda.api.entities.Message)1 MessageReaction (net.dv8tion.jda.api.entities.MessageReaction)1 TextChannel (net.dv8tion.jda.api.entities.TextChannel)1 User (net.dv8tion.jda.api.entities.User)1 GenericEvent (net.dv8tion.jda.api.events.GenericEvent)1 GuildMessageReceivedEvent (net.dv8tion.jda.api.events.message.guild.GuildMessageReceivedEvent)1 GuildMessageReactionAddEvent (net.dv8tion.jda.api.events.message.guild.react.GuildMessageReactionAddEvent)1 EventListener (net.dv8tion.jda.api.hooks.EventListener)1 Logger (org.slf4j.Logger)1 LoggerFactory (org.slf4j.LoggerFactory)1