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