Search in sources :

Example 1 with CommandParser

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);
        }
    }
}
Also used : Preset(net.robinfriedli.aiode.entities.Preset) ArgumentPrefixProperty(net.robinfriedli.aiode.discord.property.properties.ArgumentPrefixProperty) AbstractCommand(net.robinfriedli.aiode.command.AbstractCommand) ArgumentBuildingMode(net.robinfriedli.aiode.command.parser.ArgumentBuildingMode) Aiode(net.robinfriedli.aiode.Aiode) CommandParser(net.robinfriedli.aiode.command.parser.CommandParser) CommandParseListener(net.robinfriedli.aiode.command.parser.CommandParseListener)

Example 2 with CommandParser

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));
    }
}
Also used : EmbedBuilder(net.dv8tion.jda.api.EmbedBuilder) InvalidCommandException(net.robinfriedli.aiode.exceptions.InvalidCommandException) Preset(net.robinfriedli.aiode.entities.Preset) AbstractCommand(net.robinfriedli.aiode.command.AbstractCommand) List(java.util.List) EmbedTable(net.robinfriedli.aiode.util.EmbedTable) Session(org.hibernate.Session) CommandParser(net.robinfriedli.aiode.command.parser.CommandParser)

Example 3 with CommandParser

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);
        }
    }
}
Also used : CommandParseException(net.robinfriedli.aiode.exceptions.CommandParseException) AbstractCommand(net.robinfriedli.aiode.command.AbstractCommand) UnexpectedCommandSetupException(net.robinfriedli.aiode.exceptions.UnexpectedCommandSetupException) CommandParseException(net.robinfriedli.aiode.exceptions.CommandParseException) UnexpectedCommandSetupException(net.robinfriedli.aiode.exceptions.UnexpectedCommandSetupException) CommandParser(net.robinfriedli.aiode.command.parser.CommandParser)

Aggregations

AbstractCommand (net.robinfriedli.aiode.command.AbstractCommand)3 CommandParser (net.robinfriedli.aiode.command.parser.CommandParser)3 Preset (net.robinfriedli.aiode.entities.Preset)2 List (java.util.List)1 EmbedBuilder (net.dv8tion.jda.api.EmbedBuilder)1 Aiode (net.robinfriedli.aiode.Aiode)1 ArgumentBuildingMode (net.robinfriedli.aiode.command.parser.ArgumentBuildingMode)1 CommandParseListener (net.robinfriedli.aiode.command.parser.CommandParseListener)1 ArgumentPrefixProperty (net.robinfriedli.aiode.discord.property.properties.ArgumentPrefixProperty)1 CommandParseException (net.robinfriedli.aiode.exceptions.CommandParseException)1 InvalidCommandException (net.robinfriedli.aiode.exceptions.InvalidCommandException)1 UnexpectedCommandSetupException (net.robinfriedli.aiode.exceptions.UnexpectedCommandSetupException)1 EmbedTable (net.robinfriedli.aiode.util.EmbedTable)1 Session (org.hibernate.Session)1