net.dv8tion.jda.api.hooks.EventListener Java Examples

The following examples show how to use net.dv8tion.jda.api.hooks.EventListener. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: ContextEventManagerImpl.java    From JuniperBot with GNU General Public License v3.0 6 votes vote down vote up
private void loopListeners(GenericEvent event) {
    if (event instanceof GuildMessageReceivedEvent) {
        dispatchChain(GuildMessageReceivedEvent.class, (GuildMessageReceivedEvent) event);
    }
    for (EventListener listener : listeners) {
        try {
            listener.onEvent(event);
        } catch (ObjectOptimisticLockingFailureException e) {
            log.warn("[{}] optimistic lock happened for {}#{} while handling {}",
                    listener.getClass().getSimpleName(),
                    e.getPersistentClassName(),
                    e.getIdentifier(),
                    event);
        } catch (Throwable throwable) {
            log.error("[{}] had an uncaught exception for handling {}",
                    listener.getClass().getSimpleName(),
                    event,
                    throwable);
        }
    }
}
 
Example #2
Source File: EventManager.java    From SkyBot with GNU Affero General Public License v3.0 5 votes vote down vote up
@Override
public void handle(@Nonnull GenericEvent event) {
    final JDA.ShardInfo shardInfo = event.getJDA().getShardInfo();

    if (shouldFakeBlock) {
        //noinspection ConstantConditions
        if (shardInfo == null) {
            logger.warn(TextColor.RED + "Shard booting up (Event {})." + TextColor.RESET, event.getClass().getSimpleName());
            return;
        }

        if (restartingShard == -1 || restartingShard == shardInfo.getShardId()) {
            return;
        }
    }

    for (final EventListener listener : this.listeners) {
        try {
            listener.onEvent(event);
        }
        catch (Throwable thr) {
            Sentry.capture(thr);

            logger.error("Error while handling event {}({}); {}",
                event.getClass().getName(),
                listener.getClass().getSimpleName(),
                thr.getLocalizedMessage());
            logger.error("", thr);
        }
    }
}
 
Example #3
Source File: ContextEventManagerImpl.java    From JuniperBot with GNU General Public License v3.0 5 votes vote down vote up
@Override
public void register(Object listener) {
    if (!(listener instanceof EventListener)) {
        throw new IllegalArgumentException("Listener must implement EventListener");
    }
    registerListeners(Collections.singletonList((EventListener) listener));
}
 
Example #4
Source File: ContextEventManagerImpl.java    From JuniperBot with GNU General Public License v3.0 5 votes vote down vote up
private void registerListeners(List<? extends EventListener> listeners) {
    synchronized (this.listeners) {
        Set<EventListener> listenerSet = new HashSet<>(this.listeners);
        listenerSet.addAll(listeners);
        this.listeners.clear();
        this.listeners.addAll(listenerSet);
        this.listeners.sort(this::compareListeners);
    }
}
 
Example #5
Source File: ContextEventManagerImpl.java    From JuniperBot with GNU General Public License v3.0 4 votes vote down vote up
private int compareListeners(EventListener first, EventListener second) {
    return getPriority(first) - getPriority(second);
}
 
Example #6
Source File: ContextEventManagerImpl.java    From JuniperBot with GNU General Public License v3.0 4 votes vote down vote up
private int getPriority(EventListener eventListener) {
    return eventListener != null && eventListener.getClass().isAnnotationPresent(DiscordEvent.class)
            ? eventListener.getClass().getAnnotation(DiscordEvent.class).priority()
            : Integer.MAX_VALUE;
}
 
Example #7
Source File: Shard.java    From MantaroBot with GNU General Public License v3.0 4 votes vote down vote up
@Nonnull
@CheckReturnValue
public EventListener getListener() {
    return listener;
}
 
Example #8
Source File: InteractiveOperations.java    From MantaroBot with GNU General Public License v3.0 4 votes vote down vote up
/**
 * @return The listener used to check for the InteractiveOperations.
 */
public static EventListener listener() {
    return LISTENER;
}
 
Example #9
Source File: ReactionOperations.java    From MantaroBot with GNU General Public License v3.0 4 votes vote down vote up
public static EventListener listener() {
    return LISTENER;
}