use of net.robinfriedli.aiode.command.CommandContext in project aiode by robinfriedli.
the class CommandMonitoringInterceptor method performChained.
@Override
public void performChained(Command command) {
CommandContext context = command.getContext();
CommandExecutionTask task = command.getTask();
if (task == null) {
return;
}
Future<?> monitoring = DaemonThreadPool.submit((LoggingRunnable) () -> {
Thread thread = Thread.currentThread();
String oldName = thread.getName();
thread.setName("command-monitoring-" + context);
CompletableFuture<Message> stillLoadingMessage = null;
CompletableFuture<Message> warningMessage = null;
try {
task.await(MESSAGE_AFTER_THRESHOLD);
if (!task.isDone()) {
EmbedBuilder embedBuilder = new EmbedBuilder();
embedBuilder.setDescription("Still loading...");
stillLoadingMessage = messageService.send(embedBuilder, context.getChannel());
task.await(LOGGER_WARNING_AFTER_THRESHOLD);
if (!task.isDone()) {
EmbedBuilder warningEmbed = new EmbedBuilder();
warningEmbed.setColor(Color.RED);
warningEmbed.setTitle("Command timeout");
warningEmbed.setDescription(String.format("Your command '%s' is taking very long to execute. " + "If the command is not responding, consider interrupting it using the abort command.", command.display()));
warningMessage = messageService.send(warningEmbed.build(), context.getChannel());
logger.warn(String.format("Command [%s] on guild %s has exceeded the warn limit for execution duration of %d millis.", command.display(), context.getGuild(), MESSAGE_AFTER_THRESHOLD + LOGGER_WARNING_AFTER_THRESHOLD));
}
task.await();
deleteMessages(stillLoadingMessage, warningMessage);
}
} catch (InterruptedException e) {
// CommandExecutionInterceptor interrupts monitoring in post command
deleteMessages(stillLoadingMessage, warningMessage);
} finally {
thread.setName(oldName);
}
});
context.registerMonitoring(monitoring);
}
use of net.robinfriedli.aiode.command.CommandContext in project aiode by robinfriedli.
the class HistoryInterceptor method performChained.
@Override
public void performChained(Command command) {
CommandContext context = command.getContext();
CommandHistory history = new CommandHistory();
long currentTimeMillis = System.currentTimeMillis();
history.setStartMillis(currentTimeMillis);
history.setTimestamp(LocalDateTime.ofInstant(Instant.ofEpochMilli(currentTimeMillis), ZoneId.systemDefault()));
history.setCommandContextId(command.getContext().getId());
history.setCommandIdentifier(command.getIdentifier());
history.setWidget(command instanceof AbstractWidgetAction);
history.setCommandBody(command instanceof AbstractCommand ? ((AbstractCommand) command).getCommandInput() : command.getCommandBody());
history.setInput(command instanceof AbstractCommand ? context.getMessage().getContentDisplay() : command.getCommandBody());
history.setGuild(context.getGuild().getName());
history.setGuildId(context.getGuild().getId());
history.setUser(context.getUser().getName());
history.setUserId(context.getUser().getId());
context.setCommandHistory(history);
}
use of net.robinfriedli.aiode.command.CommandContext in project aiode by robinfriedli.
the class SecurityInterceptor method performChained.
@Override
public void performChained(Command command) {
try {
CommandContext context = command.getContext();
if (securityManager.isAdmin(context.getUser())) {
return;
}
if (command instanceof AbstractCommand && ((AbstractCommand) command).getCategory() == AbstractCommand.Category.SCRIPTING) {
if (!springPropertiesConfig.requireApplicationProperty(Boolean.class, "aiode.preferences.enable_scripting")) {
throw new InvalidCommandException("The bot hoster disabled scripting. None of the commands in the scripting category may be used.");
}
Session session = context.getSession();
GuildSpecification specification = context.getGuildContext().getSpecification(session);
boolean enableScripting = guildPropertyManager.getPropertyValueOptional("enableScripting", Boolean.class, specification).orElse(true);
if (!enableScripting) {
throw new InvalidCommandException("Scripting has been disabled for this guild. None of the commands in the scripting category may be used. " + "Toggle the enable scripting property using the property command to enable / disable scripting.");
}
}
PermissionTarget permissionTarget = command.getPermissionTarget();
Member member = context.getMember();
securityManager.ensurePermission(permissionTarget, member);
if (command instanceof AbstractCommand) {
AbstractCommand textCommand = (AbstractCommand) command;
Collection<ArgumentController.ArgumentUsage> argumentUsages = textCommand.getArgumentController().getUsedArguments().values();
for (ArgumentController.ArgumentUsage argumentUsage : argumentUsages) {
PermissionTarget argumentPermissionTarget = argumentUsage.getArgument().getPermissionTarget();
if (argumentPermissionTarget != null) {
securityManager.ensurePermission(argumentPermissionTarget, member);
}
}
}
} catch (ForbiddenCommandException | InvalidCommandException e) {
throw e;
} catch (Exception e) {
throw new SecurityException("Failed to verify command security due to an unexpected exception", e);
}
}
use of net.robinfriedli.aiode.command.CommandContext in project aiode by robinfriedli.
the class WidgetListener method handleWidgetExecution.
private void handleWidgetExecution(GuildMessageReactionAddEvent event, AbstractWidget activeWidget) {
TextChannel channel = event.getChannel();
Guild guild = event.getGuild();
Aiode aiode = Aiode.get();
SpotifyApi.Builder spotifyApiBuilder = aiode.getSpotifyApiBuilder();
GuildContext guildContext = aiode.getGuildManager().getContextForGuild(guild);
String emojiUnicode = event.getReaction().getReactionEmote().getName();
try {
Message message = activeWidget.getMessage().retrieve();
if (message == null) {
throw new IllegalStateException("Message of widget could not be retrieved.");
}
CommandContext commandContext = new CommandContext(event, guildContext, message, hibernateComponent.getSessionFactory(), spotifyApiBuilder, emojiUnicode);
activeWidget.handleReaction(event, commandContext);
} catch (UserException e) {
messageService.sendError(e.getMessage(), channel);
} catch (InsufficientPermissionException e) {
Permission permission = e.getPermission();
messageService.send("Bot is missing permission: " + permission.getName(), channel);
logger.warn(String.format("Missing permission %s on guild %s", permission, guild));
} catch (Exception e) {
logger.error("Exception while handling WidgetAction execution.", e);
}
}
use of net.robinfriedli.aiode.command.CommandContext in project aiode by robinfriedli.
the class EvalCommand method doRun.
@Override
public void doRun() {
CommandContext context = getContext();
Aiode aiode = Aiode.get();
SecurityManager securityManager = aiode.getSecurityManager();
GroovySandboxComponent groovySandboxComponent = aiode.getGroovySandboxComponent();
GroovyVariableManager groovyVariableManager = aiode.getGroovyVariableManager();
SafeGroovyScriptRunner groovyScriptRunner = new SafeGroovyScriptRunner(context, groovySandboxComponent, groovyVariableManager, securityManager, argumentSet("privileged"));
groovyScriptRunner.runAndSendResult(getCommandInput(), 10, TimeUnit.SECONDS);
}
Aggregations