Search in sources :

Example 6 with UserException

use of net.robinfriedli.aiode.exceptions.UserException in project aiode by robinfriedli.

the class ArgumentBuildingMode method terminate.

@Override
public void terminate() {
    try {
        if (argumentBuilder.length() == 0) {
            throw new InvalidArgumentException("Missing argument identifier");
        }
        ArgumentController argumentController = command.getArgumentController();
        String argument = argumentBuilder.toString().trim();
        String argumentValue = argumentValueBuilder.toString().trim();
        argumentController.setArgument(argument, argumentValue);
        commandParser.fireOnArgumentParsed(argument, argumentValue);
    } catch (UserException e) {
        throw new CommandParseException(e.getMessage(), command.getCommandBody(), e, conceptionIndex);
    }
}
Also used : CommandParseException(net.robinfriedli.aiode.exceptions.CommandParseException) InvalidArgumentException(net.robinfriedli.aiode.exceptions.InvalidArgumentException) ArgumentController(net.robinfriedli.aiode.command.argument.ArgumentController) UserException(net.robinfriedli.aiode.exceptions.UserException)

Example 7 with UserException

use of net.robinfriedli.aiode.exceptions.UserException 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);
    }
}
Also used : Message(net.dv8tion.jda.api.entities.Message) CommandContext(net.robinfriedli.aiode.command.CommandContext) InsufficientPermissionException(net.dv8tion.jda.api.exceptions.InsufficientPermissionException) Guild(net.dv8tion.jda.api.entities.Guild) Aiode(net.robinfriedli.aiode.Aiode) InsufficientPermissionException(net.dv8tion.jda.api.exceptions.InsufficientPermissionException) UserException(net.robinfriedli.aiode.exceptions.UserException) TextChannel(net.dv8tion.jda.api.entities.TextChannel) GuildContext(net.robinfriedli.aiode.discord.GuildContext) SpotifyApi(se.michaelthelin.spotify.SpotifyApi) Permission(net.dv8tion.jda.api.Permission) UserException(net.robinfriedli.aiode.exceptions.UserException)

Example 8 with UserException

use of net.robinfriedli.aiode.exceptions.UserException in project aiode by robinfriedli.

the class AbstractWidget method initialise.

/**
 * Set up the widget so that it is ready to be used. This creates and sends the initial message if necessary,
 * registers the widget in the guild's {@link WidgetRegistry} so that it can be found when receiving reactions and
 * sets up all actions by adding the corresponding reaction emote to the message.
 */
public void initialise() {
    CompletableFuture<Message> futureMessage = prepareInitialMessage();
    try {
        Message message = futureMessage.get();
        this.message = new DiscordEntity.Message(message);
        widgetRegistry.registerWidget(this);
        try {
            setupActions(message);
        } catch (UserException e) {
            Aiode.get().getMessageService().sendError(e.getMessage(), message.getChannel());
        }
    } catch (InterruptedException e) {
        Thread.currentThread().interrupt();
    } catch (ExecutionException e) {
        throw new RuntimeException("Could not initialise widget since setting up the message failed", e);
    }
}
Also used : Message(net.dv8tion.jda.api.entities.Message) UserException(net.robinfriedli.aiode.exceptions.UserException) ExecutionException(java.util.concurrent.ExecutionException) DiscordEntity(net.robinfriedli.aiode.discord.DiscordEntity)

Example 9 with UserException

use of net.robinfriedli.aiode.exceptions.UserException in project aiode by robinfriedli.

the class Invoker method invoke.

/**
 * Invoke a callable in a hibernate transaction. There is one Invoker per guild and this method runs synchronised,
 * making sure the same guild can not run this method concurrently. This is to counteract the
 * spamming of a command that uses this method, e.g. spamming the add command concurrently could evade the playlist
 * size limit.
 *
 * @param session  the target hibernate session, individual for each command execution
 * @param callable tho callable to run
 * @param <E>      the return type
 * @return the value the callable returns, often void
 */
public synchronized <E> E invoke(Session session, Callable<E> callable) {
    boolean isNested = false;
    if (session.getTransaction() == null || !session.getTransaction().isActive()) {
        session.beginTransaction();
    } else {
        isNested = true;
    }
    if (isNested) {
        try {
            return callable.call();
        } catch (RuntimeException e) {
            throw e;
        } catch (Exception e) {
            throw new CommandRuntimeException(e);
        }
    }
    E retVal;
    try {
        retVal = callable.call();
        session.getTransaction().commit();
    } catch (UserException e) {
        session.getTransaction().rollback();
        throw e;
    } catch (Exception e) {
        session.getTransaction().rollback();
        throw new RuntimeException("Exception in invoked callable. Transaction rolled back.", e);
    }
    return retVal;
}
Also used : CommandRuntimeException(net.robinfriedli.aiode.exceptions.CommandRuntimeException) UserException(net.robinfriedli.aiode.exceptions.UserException) CommandRuntimeException(net.robinfriedli.aiode.exceptions.CommandRuntimeException) UserException(net.robinfriedli.aiode.exceptions.UserException) CommandRuntimeException(net.robinfriedli.aiode.exceptions.CommandRuntimeException)

Aggregations

UserException (net.robinfriedli.aiode.exceptions.UserException)9 ExecutionException (java.util.concurrent.ExecutionException)3 EmbedBuilder (net.dv8tion.jda.api.EmbedBuilder)3 Message (net.dv8tion.jda.api.entities.Message)3 CompletableFuture (java.util.concurrent.CompletableFuture)2 Guild (net.dv8tion.jda.api.entities.Guild)2 MessageChannel (net.dv8tion.jda.api.entities.MessageChannel)2 TextChannel (net.dv8tion.jda.api.entities.TextChannel)2 User (net.dv8tion.jda.api.entities.User)2 ErrorResponseException (net.dv8tion.jda.api.exceptions.ErrorResponseException)2 InsufficientPermissionException (net.dv8tion.jda.api.exceptions.InsufficientPermissionException)2 AbstractCommand (net.robinfriedli.aiode.command.AbstractCommand)2 CommandContext (net.robinfriedli.aiode.command.CommandContext)2 CommandParseException (net.robinfriedli.aiode.exceptions.CommandParseException)2 CommandRuntimeException (net.robinfriedli.aiode.exceptions.CommandRuntimeException)2 GoogleJsonResponseException (com.google.api.client.googleapis.json.GoogleJsonResponseException)1 FriendlyException (com.sedmelluq.discord.lavaplayer.tools.FriendlyException)1 GroovyShell (groovy.lang.GroovyShell)1 CancellationException (java.util.concurrent.CancellationException)1 TimeoutException (java.util.concurrent.TimeoutException)1