|
| 1 | +package dev.sheldan.abstracto.core.commands.config; |
| 2 | + |
| 3 | +import dev.sheldan.abstracto.core.command.condition.AbstractConditionableCommand; |
| 4 | +import dev.sheldan.abstracto.core.command.config.CommandConfiguration; |
| 5 | +import dev.sheldan.abstracto.core.command.config.HelpInfo; |
| 6 | +import dev.sheldan.abstracto.core.command.config.Parameter; |
| 7 | +import dev.sheldan.abstracto.core.command.config.features.CoreFeatureDefinition; |
| 8 | +import dev.sheldan.abstracto.core.command.execution.CommandResult; |
| 9 | +import dev.sheldan.abstracto.core.config.FeatureConfig; |
| 10 | +import dev.sheldan.abstracto.core.config.FeatureDefinition; |
| 11 | +import dev.sheldan.abstracto.core.exception.ConfigurationKeyNotFoundException; |
| 12 | +import dev.sheldan.abstracto.core.interaction.InteractionService; |
| 13 | +import dev.sheldan.abstracto.core.interaction.slash.CoreSlashCommandNames; |
| 14 | +import dev.sheldan.abstracto.core.interaction.slash.SlashCommandConfig; |
| 15 | +import dev.sheldan.abstracto.core.interaction.slash.SlashCommandPrivilegeLevels; |
| 16 | +import dev.sheldan.abstracto.core.interaction.slash.parameter.SlashCommandAutoCompleteService; |
| 17 | +import dev.sheldan.abstracto.core.interaction.slash.parameter.SlashCommandParameterService; |
| 18 | +import dev.sheldan.abstracto.core.models.database.AConfig; |
| 19 | +import dev.sheldan.abstracto.core.models.property.SystemConfigProperty; |
| 20 | +import dev.sheldan.abstracto.core.models.template.commands.GetConfigModel; |
| 21 | +import dev.sheldan.abstracto.core.service.FeatureConfigService; |
| 22 | +import dev.sheldan.abstracto.core.service.PaginatorService; |
| 23 | +import dev.sheldan.abstracto.core.service.management.ConfigManagementService; |
| 24 | +import dev.sheldan.abstracto.core.service.management.DefaultConfigManagementService; |
| 25 | +import java.util.ArrayList; |
| 26 | +import java.util.Arrays; |
| 27 | +import java.util.Comparator; |
| 28 | +import java.util.List; |
| 29 | +import java.util.Map; |
| 30 | +import java.util.concurrent.CompletableFuture; |
| 31 | +import java.util.function.Function; |
| 32 | +import java.util.stream.Collectors; |
| 33 | +import net.dv8tion.jda.api.events.interaction.command.CommandAutoCompleteInteractionEvent; |
| 34 | +import net.dv8tion.jda.api.events.interaction.command.SlashCommandInteractionEvent; |
| 35 | +import org.springframework.beans.factory.annotation.Autowired; |
| 36 | +import org.springframework.stereotype.Component; |
| 37 | + |
| 38 | +@Component |
| 39 | +public class GetConfig extends AbstractConditionableCommand { |
| 40 | + |
| 41 | + private static final String GET_CONFIG_COMMAND = "getConfig"; |
| 42 | + private static final String KEY_PARAMETER = "key"; |
| 43 | + private static final String FEATURE_PARAMETER = "feature"; |
| 44 | + |
| 45 | + private static final String GET_CONFIG_RESPONSE_TEMPLATE_KEY = "getConfig_response"; |
| 46 | + private static final String NO_CONFIGS_TEMPLATE_KEY = "getConfig_no_configs_found"; |
| 47 | + |
| 48 | + @Autowired |
| 49 | + private SlashCommandParameterService slashCommandParameterService; |
| 50 | + |
| 51 | + @Autowired |
| 52 | + private FeatureConfigService featureConfigService; |
| 53 | + |
| 54 | + @Autowired |
| 55 | + private SlashCommandAutoCompleteService slashCommandAutoCompleteService; |
| 56 | + |
| 57 | + @Autowired |
| 58 | + private DefaultConfigManagementService defaultConfigManagementService; |
| 59 | + |
| 60 | + @Autowired |
| 61 | + private ConfigManagementService configManagementService; |
| 62 | + |
| 63 | + @Autowired |
| 64 | + private PaginatorService paginatorService; |
| 65 | + |
| 66 | + @Autowired |
| 67 | + private InteractionService interactionService; |
| 68 | + |
| 69 | + @Override |
| 70 | + public CompletableFuture<CommandResult> executeSlash(SlashCommandInteractionEvent event) { |
| 71 | + Long serverId = event.getGuild().getIdLong(); |
| 72 | + List<GetConfigModel.ConfigValue> configValues; |
| 73 | + if(slashCommandParameterService.hasCommandOption(KEY_PARAMETER, event)) { |
| 74 | + String key = slashCommandParameterService.getCommandOption(KEY_PARAMETER, event, String.class); |
| 75 | + if(!defaultConfigManagementService.configKeyExists(key)) { |
| 76 | + throw new ConfigurationKeyNotFoundException(key); |
| 77 | + } |
| 78 | + |
| 79 | + GetConfigModel.ConfigValue.ConfigValueBuilder configValueBuilder = GetConfigModel.ConfigValue.builder(); |
| 80 | + if(configManagementService.configExists(serverId, key)) { |
| 81 | + AConfig aConfig = configManagementService.loadConfig(serverId, key); |
| 82 | + configValueBuilder |
| 83 | + .doubleValue(aConfig.getDoubleValue()) |
| 84 | + .hasConcreateValue(true) |
| 85 | + .longValue(aConfig.getLongValue()) |
| 86 | + .stringValue(aConfig.getStringValue()); |
| 87 | + } |
| 88 | + SystemConfigProperty defaultConfig = defaultConfigManagementService.getDefaultConfig(key); |
| 89 | + GetConfigModel.ConfigValue.ConfigValueBuilder defaultConfigValueBuilder = GetConfigModel |
| 90 | + .ConfigValue |
| 91 | + .builder() |
| 92 | + .stringValue(defaultConfig.getStringValue()) |
| 93 | + .doubleValue(defaultConfig.getDoubleValue()) |
| 94 | + .longValue(defaultConfig.getLongValue()) |
| 95 | + .key(defaultConfig.getName()); |
| 96 | + configValueBuilder |
| 97 | + .key(defaultConfig.getName()) |
| 98 | + .defaultValue(defaultConfigValueBuilder.build()); |
| 99 | + configValues = List.of(configValueBuilder.build()); |
| 100 | + |
| 101 | + } else if(slashCommandParameterService.hasCommandOption(FEATURE_PARAMETER, event)) { |
| 102 | + String featureKey = slashCommandParameterService.getCommandOption(FEATURE_PARAMETER, event, String.class); |
| 103 | + FeatureConfig feature = featureConfigService.getFeatureDisplayForFeature(featureKey); |
| 104 | + List<String> configKeys = feature.getRequiredSystemConfigKeys(); |
| 105 | + configValues = getConfigValuesForKeys(serverId, configKeys); |
| 106 | + } else { |
| 107 | + List<String> configKeys = defaultConfigManagementService.getConfigKeys(); |
| 108 | + configValues = getConfigValuesForKeys(serverId, configKeys); |
| 109 | + } |
| 110 | + |
| 111 | + if(configValues.isEmpty()) { |
| 112 | + return interactionService.replyEmbed(NO_CONFIGS_TEMPLATE_KEY, new Object() , event) |
| 113 | + .thenApply(interactionHook -> CommandResult.fromSuccess()); |
| 114 | + } |
| 115 | + |
| 116 | + configValues = new ArrayList<>(configValues); |
| 117 | + configValues.sort(Comparator.comparing(GetConfigModel.ConfigValue::getKey)); |
| 118 | + GetConfigModel model = GetConfigModel.builder() |
| 119 | + .values(configValues) |
| 120 | + .build(); |
| 121 | + |
| 122 | + return paginatorService.createPaginatorFromTemplate(GET_CONFIG_RESPONSE_TEMPLATE_KEY, model, event) |
| 123 | + .thenApply(unused -> CommandResult.fromSuccess()); |
| 124 | + } |
| 125 | + |
| 126 | + private List<GetConfigModel.ConfigValue> getConfigValuesForKeys(Long serverId, List<String> configKeys) { |
| 127 | + Map<String, AConfig> allExistingConfigs = configManagementService |
| 128 | + .loadForServer(serverId) |
| 129 | + .stream() |
| 130 | + .collect(Collectors.toMap(aConfig -> aConfig.getName().toLowerCase(), Function.identity())); |
| 131 | + return configKeys.stream().map(key -> { |
| 132 | + GetConfigModel.ConfigValue.ConfigValueBuilder configValueBuilder = GetConfigModel.ConfigValue.builder(); |
| 133 | + if(allExistingConfigs.containsKey(key.toLowerCase())) { |
| 134 | + AConfig aConfig = allExistingConfigs.get(key.toLowerCase()); |
| 135 | + configValueBuilder |
| 136 | + .doubleValue(aConfig.getDoubleValue()) |
| 137 | + .hasConcreateValue(true) |
| 138 | + .longValue(aConfig.getLongValue()) |
| 139 | + .stringValue(aConfig.getStringValue()); |
| 140 | + } |
| 141 | + SystemConfigProperty defaultConfig = defaultConfigManagementService.getDefaultConfig(key); |
| 142 | + GetConfigModel.ConfigValue.ConfigValueBuilder defaultConfigValueBuilder = GetConfigModel |
| 143 | + .ConfigValue |
| 144 | + .builder() |
| 145 | + .stringValue(defaultConfig.getStringValue()) |
| 146 | + .doubleValue(defaultConfig.getDoubleValue()) |
| 147 | + .longValue(defaultConfig.getLongValue()) |
| 148 | + .key(defaultConfig.getName()); |
| 149 | + configValueBuilder |
| 150 | + .key(defaultConfig.getName()) |
| 151 | + .defaultValue(defaultConfigValueBuilder.build()); |
| 152 | + return configValueBuilder.build(); |
| 153 | + }).toList(); |
| 154 | + } |
| 155 | + |
| 156 | + @Override |
| 157 | + public List<String> performAutoComplete(CommandAutoCompleteInteractionEvent event) { |
| 158 | + String input = event.getFocusedOption().getValue().toLowerCase(); |
| 159 | + if(slashCommandAutoCompleteService.matchesParameter(event.getFocusedOption(), KEY_PARAMETER)) { |
| 160 | + return defaultConfigManagementService |
| 161 | + .getConfigKeys() |
| 162 | + .stream() |
| 163 | + .map(String::toLowerCase) |
| 164 | + .filter(key -> key.startsWith(input)) |
| 165 | + .toList(); |
| 166 | + } else if(slashCommandAutoCompleteService.matchesParameter(event.getFocusedOption(), FEATURE_PARAMETER)) { |
| 167 | + return featureConfigService.getAllFeatures() |
| 168 | + .stream() |
| 169 | + .map(String::toLowerCase) |
| 170 | + .filter(lowerCase -> lowerCase.startsWith(input)) |
| 171 | + .toList(); |
| 172 | + } |
| 173 | + return new ArrayList<>(); |
| 174 | + } |
| 175 | + |
| 176 | + @Override |
| 177 | + public CommandConfiguration getConfiguration() { |
| 178 | + Parameter keyToGet = Parameter |
| 179 | + .builder() |
| 180 | + .name(KEY_PARAMETER) |
| 181 | + .type(String.class) |
| 182 | + .supportsAutoComplete(true) |
| 183 | + .templated(true) |
| 184 | + .optional(true) |
| 185 | + .build(); |
| 186 | + Parameter featureToGet = Parameter |
| 187 | + .builder() |
| 188 | + .name(FEATURE_PARAMETER) |
| 189 | + .type(String.class) |
| 190 | + .supportsAutoComplete(true) |
| 191 | + .templated(true) |
| 192 | + .optional(true) |
| 193 | + .build(); |
| 194 | + List<Parameter> parameters = Arrays.asList(keyToGet, featureToGet); |
| 195 | + HelpInfo helpInfo = HelpInfo |
| 196 | + .builder() |
| 197 | + .templated(true) |
| 198 | + .hasExample(true) |
| 199 | + .build(); |
| 200 | + |
| 201 | + SlashCommandConfig slashCommandConfig = SlashCommandConfig |
| 202 | + .builder() |
| 203 | + .enabled(true) |
| 204 | + .defaultPrivilege(SlashCommandPrivilegeLevels.INVITER) |
| 205 | + .rootCommandName(CoreSlashCommandNames.CONFIG) |
| 206 | + .commandName("get") |
| 207 | + .build(); |
| 208 | + |
| 209 | + return CommandConfiguration.builder() |
| 210 | + .name(GET_CONFIG_COMMAND) |
| 211 | + .module(ConfigModuleDefinition.CONFIG) |
| 212 | + .parameters(parameters) |
| 213 | + .slashCommandOnly(true) |
| 214 | + .slashCommandConfig(slashCommandConfig) |
| 215 | + .templated(true) |
| 216 | + .supportsEmbedException(true) |
| 217 | + .help(helpInfo) |
| 218 | + .causesReaction(true) |
| 219 | + .build(); |
| 220 | + } |
| 221 | + |
| 222 | + @Override |
| 223 | + public FeatureDefinition getFeature() { |
| 224 | + return CoreFeatureDefinition.CORE_FEATURE; |
| 225 | + } |
| 226 | +} |
0 commit comments