Search in sources :

Example 1 with CommandHistory

use of net.robinfriedli.aiode.entities.CommandHistory in project aiode by robinfriedli.

the class CommandExecutionInterceptor method postCommand.

/**
 * Finalize and persist the {@link CommandHistory} entry for this command execution.
 *
 * @param command               the executed command
 * @param completedSuccessfully whether the command completed sucessfully
 * @param failedManually        whether the command failed because {@link Command#isFailed()} returned true
 * @param errorMessage          the error message if an exception was thrown
 * @param unexpectedException   whether an unexpected exception was thrown, this excludes {@link UserException}
 * @param aborted               whether command execution was aborted
 */
private void postCommand(Command command, boolean completedSuccessfully, boolean failedManually, String errorMessage, boolean unexpectedException, boolean aborted) {
    CommandContext context = command.getContext();
    context.interruptMonitoring();
    HistoryPool.execute(() -> {
        CommandHistory history = context.getCommandHistory();
        if (history != null) {
            history.setDurationMs(System.currentTimeMillis() - history.getStartMillis());
            history.setCompletedSuccessfully(completedSuccessfully);
            history.setFailedManually(failedManually);
            history.setUnexpectedException(unexpectedException);
            history.setErrorMessage(errorMessage);
            history.setAborted(aborted);
            StaticSessionProvider.consumeSession(session -> session.persist(history));
        } else {
            logger.warn("Command " + command + " has no history");
        }
    });
}
Also used : CommandHistory(net.robinfriedli.aiode.entities.CommandHistory) CommandContext(net.robinfriedli.aiode.command.CommandContext)

Example 2 with CommandHistory

use of net.robinfriedli.aiode.entities.CommandHistory 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);
}
Also used : CommandHistory(net.robinfriedli.aiode.entities.CommandHistory) CommandContext(net.robinfriedli.aiode.command.CommandContext) AbstractCommand(net.robinfriedli.aiode.command.AbstractCommand) AbstractWidgetAction(net.robinfriedli.aiode.command.widget.AbstractWidgetAction)

Aggregations

CommandContext (net.robinfriedli.aiode.command.CommandContext)2 CommandHistory (net.robinfriedli.aiode.entities.CommandHistory)2 AbstractCommand (net.robinfriedli.aiode.command.AbstractCommand)1 AbstractWidgetAction (net.robinfriedli.aiode.command.widget.AbstractWidgetAction)1