Search in sources :

Example 1 with ExecutionContext

use of net.robinfriedli.aiode.concurrent.ExecutionContext in project aiode by robinfriedli.

the class AccessConfigurationPartitionInterceptor method intercept.

@Override
public void intercept(QueryBuilder<?, ?, ?, ?> queryBuilder) {
    Optional<ExecutionContext> executionContext = ExecutionContext.Current.optional();
    if (executionContext.isPresent() || (session != null && guildId != null)) {
        Session session = executionContext.map(ExecutionContext::getSession).orElse(this.session);
        String id = executionContext.map(ctx -> ctx.getGuild().getId()).orElse(guildId);
        queryBuilder.where((cb, root, subQueryFactory) -> cb.equal(root.get("guildSpecification"), subQueryFactory.createUncorrelatedSubQuery(GuildSpecification.class, "pk").where((cb1, root1, subQueryFactory1) -> cb1.equal(root1.get("guildId"), id)).build(session)));
    }
}
Also used : ExecutionContext(net.robinfriedli.aiode.concurrent.ExecutionContext) Component(org.springframework.stereotype.Component) GuildSpecification(net.robinfriedli.aiode.entities.GuildSpecification) AccessConfiguration(net.robinfriedli.aiode.entities.AccessConfiguration) QueryInterceptor(net.robinfriedli.aiode.persist.qb.interceptor.QueryInterceptor) Optional(java.util.Optional) Session(org.hibernate.Session) Autowired(org.springframework.beans.factory.annotation.Autowired) QueryBuilder(net.robinfriedli.aiode.persist.qb.QueryBuilder) ExecutionContext(net.robinfriedli.aiode.concurrent.ExecutionContext) Session(org.hibernate.Session)

Example 2 with ExecutionContext

use of net.robinfriedli.aiode.concurrent.ExecutionContext in project aiode by robinfriedli.

the class PermissionExceptionHandler method handleException.

@Override
public Result handleException(Throwable uncaughtException, PermissionException exceptionToHandle) {
    if (Aiode.isInitialised()) {
        Aiode aiode = Aiode.get();
        ExecutionContext executionContext = ExecutionContext.Current.get();
        if (executionContext != null) {
            MessageService messageService = aiode.getMessageService();
            Permission permission = exceptionToHandle.getPermission();
            TextChannel channel = executionContext.getChannel();
            Guild guild = executionContext.getGuild();
            Aiode.LOGGER.warn(String.format("Bot is missing permission %s on guild %s", permission, guild));
            messageService.send("Bot is missing permission: " + permission.getName(), channel);
            return Result.HANDLED;
        }
    }
    return Result.UNHANDLED;
}
Also used : TextChannel(net.dv8tion.jda.api.entities.TextChannel) ExecutionContext(net.robinfriedli.aiode.concurrent.ExecutionContext) Permission(net.dv8tion.jda.api.Permission) Guild(net.dv8tion.jda.api.entities.Guild) Aiode(net.robinfriedli.aiode.Aiode) MessageService(net.robinfriedli.aiode.discord.MessageService)

Example 3 with ExecutionContext

use of net.robinfriedli.aiode.concurrent.ExecutionContext in project aiode by robinfriedli.

the class AbstractCommand method run.

/**
 * Run a custom command like you would enter it to discord, this includes commands and presets but excludes scripts
 * to avoid recursion.
 * This method is mainly used in groovy scripts.
 *
 * @param command the command string
 */
public void run(String command) {
    StaticSessionProvider.consumeSession(session -> {
        CommandContext fork = context.fork(command, session);
        AbstractCommand abstractCommand = commandManager.instantiateCommandForContext(fork, session, false).orElseThrow(() -> new InvalidCommandException("No command found for input"));
        ExecutionContext oldExecutionContext = ExecutionContext.Current.get();
        ExecutionContext.Current.set(fork);
        try {
            commandManager.getInterceptorChainWithoutScripting().intercept(abstractCommand);
        } finally {
            if (oldExecutionContext != null) {
                ExecutionContext.Current.set(oldExecutionContext);
            } else {
                ThreadContext.Current.drop(ExecutionContext.class);
            }
        }
    });
}
Also used : ExecutionContext(net.robinfriedli.aiode.concurrent.ExecutionContext) InvalidCommandException(net.robinfriedli.aiode.exceptions.InvalidCommandException)

Example 4 with ExecutionContext

use of net.robinfriedli.aiode.concurrent.ExecutionContext in project aiode by robinfriedli.

the class PooledTrackLoadingExecutor method execute.

@Override
public void execute(Runnable trackLoadingRunnable) {
    MessageChannel channel;
    ExecutionContext executionContext = null;
    if (ExecutionContext.Current.isSet()) {
        executionContext = ExecutionContext.Current.require();
        channel = executionContext.getChannel();
    } else {
        channel = guildContext.getPlayback().getCommunicationChannel();
    }
    ExecutionContext finalExecutionContext = executionContext != null ? executionContext.fork() : null;
    QueuedTask thread = new QueuedTask(queue, () -> {
        try {
            if (finalExecutionContext != null) {
                ExecutionContext.Current.set(finalExecutionContext);
            }
            if (channel != null) {
                ThreadContext.Current.install(channel);
            }
            trackLoadingRunnable.run();
        } catch (Exception e) {
            try {
                new TrackLoadingExceptionHandlerExecutor(finalExecutionContext, channel).handleException(e);
            } catch (Throwable propagate) {
                CommandRuntimeException.throwRuntimeException(propagate);
            }
        } finally {
            ThreadContext.Current.clear();
        }
    });
    queue.add(thread);
}
Also used : ExecutionContext(net.robinfriedli.aiode.concurrent.ExecutionContext) MessageChannel(net.dv8tion.jda.api.entities.MessageChannel) TrackLoadingExceptionHandlerExecutor(net.robinfriedli.aiode.exceptions.handler.TrackLoadingExceptionHandlerExecutor) QueuedTask(net.robinfriedli.aiode.concurrent.QueuedTask) CommandRuntimeException(net.robinfriedli.aiode.exceptions.CommandRuntimeException)

Example 5 with ExecutionContext

use of net.robinfriedli.aiode.concurrent.ExecutionContext in project aiode by robinfriedli.

the class ReplaceableTrackLoadingExecutor method execute.

@Override
public synchronized void execute(Runnable trackLoadingRunnable) {
    MessageChannel channel;
    ExecutionContext executionContext = null;
    if (ExecutionContext.Current.isSet()) {
        executionContext = ExecutionContext.Current.require();
        channel = executionContext.getChannel();
    } else {
        channel = guildContext.getPlayback().getCommunicationChannel();
    }
    Future<?> runningTask = currentTask.get();
    if (runningTask != null) {
        runningTask.cancel(true);
    }
    int expectedThreadNumber = threadNumber.incrementAndGet();
    ExecutionContext finalExecutionContext = executionContext != null ? executionContext.fork() : null;
    Future<?> future = PooledTrackLoadingExecutor.GLOBAL_POOL.submit(() -> {
        try {
            if (finalExecutionContext != null) {
                ExecutionContext.Current.set(finalExecutionContext);
            }
            if (channel != null) {
                ThreadContext.Current.install(channel);
            }
            trackLoadingRunnable.run();
        } catch (Exception e) {
            try {
                new TrackLoadingExceptionHandlerExecutor(finalExecutionContext, channel).handleException(e);
            } catch (Throwable propagate) {
                CommandRuntimeException.throwRuntimeException(propagate);
            }
        } finally {
            ThreadContext.Current.clear();
            if (expectedThreadNumber == threadNumber.get()) {
                currentTask.set(null);
            }
        }
    });
    currentTask.set(future);
}
Also used : ExecutionContext(net.robinfriedli.aiode.concurrent.ExecutionContext) MessageChannel(net.dv8tion.jda.api.entities.MessageChannel) TrackLoadingExceptionHandlerExecutor(net.robinfriedli.aiode.exceptions.handler.TrackLoadingExceptionHandlerExecutor) CommandRuntimeException(net.robinfriedli.aiode.exceptions.CommandRuntimeException)

Aggregations

ExecutionContext (net.robinfriedli.aiode.concurrent.ExecutionContext)8 Guild (net.dv8tion.jda.api.entities.Guild)4 TextChannel (net.dv8tion.jda.api.entities.TextChannel)4 Optional (java.util.Optional)2 JDA (net.dv8tion.jda.api.JDA)2 Member (net.dv8tion.jda.api.entities.Member)2 MessageChannel (net.dv8tion.jda.api.entities.MessageChannel)2 Aiode (net.robinfriedli.aiode.Aiode)2 GuildContext (net.robinfriedli.aiode.discord.GuildContext)2 MessageService (net.robinfriedli.aiode.discord.MessageService)2 CommandRuntimeException (net.robinfriedli.aiode.exceptions.CommandRuntimeException)2 TrackLoadingExceptionHandlerExecutor (net.robinfriedli.aiode.exceptions.handler.TrackLoadingExceptionHandlerExecutor)2 SessionFactory (org.hibernate.SessionFactory)2 Component (org.springframework.stereotype.Component)2 Arrays (java.util.Arrays)1 UUID (java.util.UUID)1 Cookie (javax.servlet.http.Cookie)1 HttpServletRequest (javax.servlet.http.HttpServletRequest)1 HttpServletResponse (javax.servlet.http.HttpServletResponse)1 Permission (net.dv8tion.jda.api.Permission)1