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)));
}
}
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;
}
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);
}
}
});
}
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);
}
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);
}
Aggregations