use of net.robinfriedli.aiode.exceptions.InvalidCommandException in project aiode by robinfriedli.
the class PresetCommand method doRun.
@Override
public void doRun() {
Session session = getContext().getSession();
if (argumentSet("delete")) {
Preset preset = SearchEngine.searchPreset(session, getCommandInput());
if (preset == null) {
throw new InvalidCommandException(String.format("No preset found for '%s'", getCommandInput()));
}
invoke(() -> session.delete(preset));
} else if (getCommandInput().isBlank()) {
List<Preset> presets = getQueryBuilderFactory().find(Preset.class).build(session).getResultList();
EmbedBuilder embedBuilder = new EmbedBuilder();
if (presets.isEmpty()) {
embedBuilder.setDescription("No presets saved");
} else {
EmbedTable table = new EmbedTable(embedBuilder);
table.addColumn("Name", presets, Preset::getName);
table.addColumn("Preset", presets, Preset::getPreset);
table.build();
}
sendMessage(embedBuilder);
} else {
String presetString = getCommandInput();
String name = getArgumentValue("as");
Preset existingPreset = SearchEngine.searchPreset(getContext().getSession(), name);
if (existingPreset != null) {
throw new InvalidCommandException("Preset " + name + " already exists");
}
Preset preset = new Preset(name, presetString, getContext().getGuild(), getContext().getUser());
String testString = presetString.contains("%s") ? name + " test" : name;
AbstractCommand abstractCommand = preset.instantiateCommand(getManager(), getContext(), testString);
CommandParser commandParser = new CommandParser(abstractCommand, ArgumentPrefixProperty.getForCurrentContext());
commandParser.parse();
abstractCommand.verify();
invoke(() -> session.persist(preset));
}
}
use of net.robinfriedli.aiode.exceptions.InvalidCommandException in project aiode by robinfriedli.
the class RenameCommand method doRun.
@Override
public void doRun() {
String name = getCommandInput();
CommandContext context = getContext();
Guild guild = context.getGuild();
if (name.length() < 1 || name.length() > 32) {
throw new InvalidCommandException("Length should be 1 - 32 characters");
}
RENAME_SYNC.execute(guild.getIdLong(), () -> {
invoke(() -> context.getGuildContext().setBotName(name));
try {
guild.getSelfMember().modifyNickname(name).complete();
couldChangeNickname = true;
} catch (InsufficientPermissionException ignored) {
couldChangeNickname = false;
}
});
}
use of net.robinfriedli.aiode.exceptions.InvalidCommandException 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.exceptions.InvalidCommandException in project aiode by robinfriedli.
the class AbstractCommand method splitInlineArgument.
@Deprecated
protected Pair<String, String> splitInlineArgument(String part, String argument) {
StringList words = StringList.createWithRegex(part, " ");
List<Integer> positions = words.findPositionsOf("$" + argument, true);
if (positions.isEmpty()) {
throw new InvalidCommandException("Expected inline argument: " + argument);
}
int position = positions.get(0);
if (position == 0 || position == words.size() - 1) {
throw new InvalidCommandException("No input before or after inline argument: " + argument);
}
String left = words.subList(0, position).toSeparatedString(" ");
String right = words.subList(position + 1, words.size()).toSeparatedString(" ");
if (left.isBlank() || right.isBlank()) {
throw new InvalidCommandException("No input before or after inline argument: " + argument);
}
return Pair.of(left.trim(), right.trim());
}
use of net.robinfriedli.aiode.exceptions.InvalidCommandException in project aiode by robinfriedli.
the class CreateCommand method doRun.
@Override
public void doRun() {
Session session = getContext().getSession();
Playlist existingPlaylist = SearchEngine.searchLocalList(session, getCommandInput());
if (existingPlaylist != null) {
throw new InvalidCommandException("Playlist " + getCommandInput() + " already exists");
}
Playlist playlist = new Playlist(getCommandInput(), getContext().getUser(), getContext().getGuild());
invoke(() -> session.persist(playlist));
}
Aggregations