use of net.robinfriedli.aiode.command.parser.CommandParser in project aiode by robinfriedli.
the class GuildPropertyInterceptor method updatePresets.
private void updatePresets(AbstractGuildProperty argumentPrefixProperty, Character oldArgumentPrefix, char newArgumentPrefix, Session session) {
List<Preset> presets = queryBuilderFactory.find(Preset.class).build(session).getResultList();
String defaultValue = argumentPrefixProperty.getDefaultValue();
char[] chars = defaultValue.toCharArray();
char defaultArgPrefix;
if (chars.length == 1) {
defaultArgPrefix = chars[0];
} else {
defaultArgPrefix = ArgumentPrefixProperty.DEFAULT_FALLBACK;
}
char argPrefix = oldArgumentPrefix != null ? oldArgumentPrefix : defaultArgPrefix;
ArgumentPrefixProperty.Config argumentPrefixConfig = new ArgumentPrefixProperty.Config(argPrefix, defaultArgPrefix);
for (Preset preset : presets) {
Aiode aiode = Aiode.get();
AbstractCommand command = preset.instantiateCommand(aiode.getCommandManager(), commandContext, preset.getName());
String oldPreset = preset.getPreset();
StringBuilder newPresetBuilder = new StringBuilder(oldPreset);
List<Integer> oldPrefixOccurrences = Lists.newArrayList();
CommandParser commandParser = new CommandParser(command, argumentPrefixConfig, new CommandParseListener() {
@Override
public void onModeSwitch(CommandParser.Mode previousMode, CommandParser.Mode newMode, int index, char character) {
// they are in fact argument prefixes
if (newMode instanceof ArgumentBuildingMode) {
oldPrefixOccurrences.add(index);
}
}
});
commandParser.parse(oldPreset);
for (int oldPrefixOccurrence : oldPrefixOccurrences) {
newPresetBuilder.setCharAt(oldPrefixOccurrence, newArgumentPrefix);
}
String newPreset = newPresetBuilder.toString();
if (!oldPreset.equals(newPreset)) {
preset.setPreset(newPreset);
}
}
}
use of net.robinfriedli.aiode.command.parser.CommandParser 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.command.parser.CommandParser in project aiode by robinfriedli.
the class CommandParserInterceptor method performChained.
@Override
public void performChained(Command command) {
if (command instanceof AbstractCommand) {
AbstractCommand textBasedCommand = (AbstractCommand) command;
CommandParser commandParser = new CommandParser(textBasedCommand, ArgumentPrefixProperty.getForCurrentContext());
try {
commandParser.parse();
} catch (CommandParseException e) {
throw e;
} catch (Exception e) {
throw new UnexpectedCommandSetupException(e);
}
}
}
Aggregations