added feature to dynamically adapt the prefix while the bot is running, multiple characters are supported

This commit is contained in:
Sheldan
2020-04-26 12:11:28 +02:00
parent 4800470f86
commit 05aa0815d6
12 changed files with 128 additions and 8 deletions

View File

@@ -89,7 +89,7 @@ public class CommandReceivedHandler extends ListenerAdapter {
String contentStripped = event.getMessage().getContentStripped();
List<String> parameters = Arrays.asList(contentStripped.split(" "));
UnParsedCommandParameter unparsedParameter = new UnParsedCommandParameter(contentStripped);
String commandName = parameters.get(0).substring(1);
String commandName = commandManager.getCommandName(parameters.get(0), event.getGuild().getIdLong());
foundCommand = commandManager.findCommandByParameters(commandName, unparsedParameter);
Parameters parsedParameters = getParsedParameters(unparsedParameter, foundCommand, event.getMessage());
CommandContext commandContext = commandContextBuilder.parameters(parsedParameters).build();

View File

@@ -6,10 +6,11 @@ import dev.sheldan.abstracto.core.command.exception.CommandNotFound;
import dev.sheldan.abstracto.core.command.exception.InsufficientParameters;
import dev.sheldan.abstracto.core.command.config.CommandConfiguration;
import dev.sheldan.abstracto.core.command.config.Parameter;
import dev.sheldan.abstracto.core.command.service.CommandRegistry;
import dev.sheldan.abstracto.core.command.execution.UnParsedCommandParameter;
import dev.sheldan.abstracto.core.service.ConfigService;
import net.dv8tion.jda.api.entities.Message;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import java.util.ArrayList;
@@ -22,6 +23,12 @@ public class CommandManager implements CommandRegistry {
@Autowired
private List<Command> commands;
@Autowired
private ConfigService configService;
@Value("${abstracto.prefix}")
private String defaultPrefix;
@Override
public Command findCommandByParameters(String name, UnParsedCommandParameter unParsedCommandParameter) {
Optional<Command> commandOptional = commands.stream().filter((Command o )-> {
@@ -90,6 +97,11 @@ public class CommandManager implements CommandRegistry {
@Override
public boolean isCommand(Message message) {
return message.getContentRaw().startsWith("!");
return message.getContentRaw().startsWith(configService.getStringValue("prefix", message.getGuild().getIdLong(), defaultPrefix));
}
@Override
public String getCommandName(String input, Long serverId) {
return input.replaceFirst(configService.getStringValue("prefix", serverId, defaultPrefix), "");
}
}

View File

@@ -0,0 +1,47 @@
package dev.sheldan.abstracto.core.commands.config;
import dev.sheldan.abstracto.core.command.Command;
import dev.sheldan.abstracto.core.command.config.CommandConfiguration;
import dev.sheldan.abstracto.core.command.config.Parameter;
import dev.sheldan.abstracto.core.command.execution.CommandContext;
import dev.sheldan.abstracto.core.command.execution.CommandResult;
import dev.sheldan.abstracto.core.config.FeatureEnum;
import dev.sheldan.abstracto.core.config.features.CoreFeatures;
import dev.sheldan.abstracto.core.service.ConfigService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import java.util.Arrays;
import java.util.List;
@Component
public class SetPrefix implements Command {
@Autowired
private ConfigService configService;
@Override
public CommandResult execute(CommandContext commandContext) {
String prefixValue = (String) commandContext.getParameters().getParameters().get(0);
configService.setStringValue("prefix", commandContext.getGuild().getIdLong(), prefixValue);
return CommandResult.fromSuccess();
}
@Override
public CommandConfiguration getConfiguration() {
Parameter newPrefixParameter = Parameter.builder().name("prefix").type(String.class).description("The new prefix to be used for this server.").build();
List<Parameter> parameters = Arrays.asList(newPrefixParameter);
return CommandConfiguration.builder()
.name("setPrefix")
.module(ConfigModuleInterface.CONFIG)
.parameters(parameters)
.description("Used to change the prefix on this server.")
.causesReaction(true)
.build();
}
@Override
public FeatureEnum getFeature() {
return CoreFeatures.CORE_FEATURE;
}
}

View File

@@ -0,0 +1,10 @@
package dev.sheldan.abstracto.core.config;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.PropertySource;
@Configuration
@PropertySource("classpath:abstracto.properties")
public class AbstractoConfig {
}

View File

@@ -0,0 +1,22 @@
package dev.sheldan.abstracto.core.listener;
import dev.sheldan.abstracto.core.models.database.AServer;
import dev.sheldan.abstracto.core.service.management.ConfigManagementService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;
@Component
public class CoreServiceConfigListener implements ServerConfigListener {
@Autowired
private ConfigManagementService configManagementService;
@Value("§{abstracto.prefix}")
private String prefix;
@Override
public void updateServerConfig(AServer server) {
configManagementService.createIfNotExists(server.getId(), "prefix", prefix);
}
}

View File

@@ -26,6 +26,15 @@ public class ConfigServiceBean implements ConfigService{
return config.getDoubleValue();
}
@Override
public String getStringValue(String name, Long serverId, String defaultValue) {
AConfig config = configManagementService.loadConfig(serverId, name);
if(config == null) {
return defaultValue;
}
return config.getStringValue();
}
@Override
public void createDoubleValueIfNotExist(String name, Long serverId, Double value) {
configManagementService.createIfNotExists(serverId, name, value);
@@ -39,4 +48,13 @@ public class ConfigServiceBean implements ConfigService{
throw new ConfigurationException(String.format("Key %s does not exist.", name));
}
}
@Override
public void setStringValue(String name, Long serverId, String value) {
if(configManagementService.configExists(serverId, name)) {
configManagementService.setStringValue(serverId, name, value);
} else {
throw new ConfigurationException(String.format("Key %s does not exist.", name));
}
}
}

View File

@@ -95,4 +95,10 @@ public class ConfigManagementServiceBean implements ConfigManagementService {
config.setDoubleValue(value);
}
@Override
public void setStringValue(Long serverId, String name, String value) {
AConfig config = loadConfig(serverId, name);
config.setStringValue(value);
}
}

View File

@@ -0,0 +1,6 @@
abstracto.startup.synchronize=true
abstracto.parameter.lowerBound=50
abstracto.features.core=true
abstracto.prefix=!

View File

@@ -15,8 +15,3 @@ logging.level.dev.sheldan=info
spring.cache.cache-names=messages
spring.cache.caffeine.spec=maximumSize=500,expireAfterAccess=600s
abstracto.startup.synchronize=true
abstracto.parameter.lowerBound=50
abstracto.features.core=true