Search in sources :

Example 1 with ArgumentPrefixProperty

use of net.robinfriedli.aiode.discord.property.properties.ArgumentPrefixProperty 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)

Aggregations

Aiode (net.robinfriedli.aiode.Aiode)1 AbstractCommand (net.robinfriedli.aiode.command.AbstractCommand)1 ArgumentBuildingMode (net.robinfriedli.aiode.command.parser.ArgumentBuildingMode)1 CommandParseListener (net.robinfriedli.aiode.command.parser.CommandParseListener)1 CommandParser (net.robinfriedli.aiode.command.parser.CommandParser)1 ArgumentPrefixProperty (net.robinfriedli.aiode.discord.property.properties.ArgumentPrefixProperty)1 Preset (net.robinfriedli.aiode.entities.Preset)1