mirror of
https://github.com/Sheldan/abstracto.git
synced 2026-04-13 03:26:31 +00:00
added tests for experience tracking module
refactored some things in experience tracking changed the paging behaviour for leader board and added check for negative numbers fixed rank not being correct for further pages added test-common module to have some common code für tests fixed command creation
This commit is contained in:
@@ -208,7 +208,7 @@ public class CommandReceivedHandler extends ListenerAdapter {
|
||||
}
|
||||
}
|
||||
} catch (NoSuchElementException e) {
|
||||
throw new IncorrectParameter("The passed parameters did not have the correct type.", command, param.getType(), param.getName());
|
||||
throw new IncorrectParameter(command, param.getType(), param.getName());
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -44,7 +44,7 @@ public class CommandCreationListener {
|
||||
log.warn("Command {} has null configuration.", command);
|
||||
return;
|
||||
}
|
||||
if(commandService.doesCommandExist(command.getConfiguration().getName())) {
|
||||
if(!commandService.doesCommandExist(command.getConfiguration().getName())) {
|
||||
commandService.createCommand(command.getConfiguration().getName(), command.getConfiguration().getModule(), command.getFeature());
|
||||
}
|
||||
});
|
||||
|
||||
@@ -3,11 +3,15 @@ package dev.sheldan.abstracto.core.command.repository;
|
||||
import dev.sheldan.abstracto.core.command.models.database.ACommand;
|
||||
import org.springframework.data.jpa.repository.JpaRepository;
|
||||
import org.springframework.data.jpa.repository.QueryHints;
|
||||
import org.springframework.stereotype.Repository;
|
||||
|
||||
import javax.persistence.QueryHint;
|
||||
|
||||
@Repository
|
||||
public interface CommandRepository extends JpaRepository<ACommand, Long> {
|
||||
|
||||
@QueryHints(@QueryHint(name = org.hibernate.annotations.QueryHints.CACHEABLE, value = "true"))
|
||||
ACommand findByName(String name);
|
||||
|
||||
boolean existsByName(String name);
|
||||
}
|
||||
|
||||
@@ -45,7 +45,7 @@ public class CommandManager implements CommandRegistry {
|
||||
boolean hasRemainderParameter = commandConfiguration.getParameters().stream().anyMatch(Parameter::isRemainder);
|
||||
if(unParsedCommandParameter.getParameters().size() < commandConfiguration.getNecessaryParameterCount()) {
|
||||
String nextParameterName = commandConfiguration.getParameters().get(commandConfiguration.getNecessaryParameterCount() - 1).getName();
|
||||
throw new InsufficientParameters("Insufficient parameters", o, nextParameterName);
|
||||
throw new InsufficientParameters(o, nextParameterName);
|
||||
}
|
||||
parameterFit = paramCountFits || hasRemainderParameter;
|
||||
} else {
|
||||
|
||||
@@ -45,7 +45,7 @@ public class CommandManagementServiceBean implements CommandManagementService {
|
||||
|
||||
@Override
|
||||
public boolean doesCommandExist(String name) {
|
||||
return findCommandByName(name) != null;
|
||||
return commandRepository.existsByName(name.toLowerCase());
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -1,5 +1,9 @@
|
||||
package dev.sheldan.abstracto.core.command.condition;
|
||||
|
||||
import dev.sheldan.abstracto.core.command.config.Parameter;
|
||||
import dev.sheldan.abstracto.core.command.exception.IncorrectParameter;
|
||||
import dev.sheldan.abstracto.core.command.exception.InsufficientParameters;
|
||||
import dev.sheldan.abstracto.core.command.execution.CommandContext;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
@@ -30,4 +34,35 @@ public abstract class AbstractConditionableCommand implements ConditionalCommand
|
||||
public List<CommandCondition> getConditions() {
|
||||
return new ArrayList<>(Arrays.asList(featureEnabledCondition, commandDisabledCondition, commandDisallowedCondition, featureModeCondition));
|
||||
}
|
||||
|
||||
protected void checkParameters(CommandContext context) {
|
||||
List<Parameter> parameters = getConfiguration().getParameters();
|
||||
for (int i = 0; i < parameters.size(); i++) {
|
||||
Parameter parameter = parameters.get(i);
|
||||
Class desiredType = parameter.getType();
|
||||
if(!parameter.isOptional()) {
|
||||
checkMandatoryExp(context, i, parameter, desiredType);
|
||||
} else {
|
||||
checkOptionalParameter(context, i, parameter, desiredType);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private void checkOptionalParameter(CommandContext context, int i, Parameter parameter, Class desiredType) {
|
||||
if(context.getParameters() != null && context.getParameters().getParameters() != null && context.getParameters().getParameters().size() >= i) {
|
||||
boolean parameterIsPresent = i < context.getParameters().getParameters().size();
|
||||
if(parameterIsPresent && !desiredType.isInstance(context.getParameters().getParameters().get(i))) {
|
||||
throw new IncorrectParameter(this, desiredType, parameter.getName());
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private void checkMandatoryExp(CommandContext context, int i, Parameter parameter, Class desiredType) {
|
||||
if(context.getParameters() == null || context.getParameters().getParameters() == null || context.getParameters().getParameters().isEmpty() || i >= context.getParameters().getParameters().size()) {
|
||||
throw new InsufficientParameters(this, parameter.getName());
|
||||
}
|
||||
if(!desiredType.isInstance(context.getParameters().getParameters().get(i))) {
|
||||
throw new IncorrectParameter(this, desiredType, parameter.getName());
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -12,8 +12,8 @@ public class IncorrectParameter extends AbstractoRunTimeException implements Tem
|
||||
private final String parameterName;
|
||||
private final Class clazz;
|
||||
|
||||
public IncorrectParameter(String s, Command command, Class expected, String parameterName) {
|
||||
super(s);
|
||||
public IncorrectParameter(Command command, Class expected, String parameterName) {
|
||||
super("");
|
||||
this.command = command;
|
||||
this.parameterName = parameterName;
|
||||
this.clazz = expected;
|
||||
|
||||
@@ -13,8 +13,8 @@ public class InsufficientParameters extends AbstractoRunTimeException implements
|
||||
private final transient Command command;
|
||||
private final String parameterName;
|
||||
|
||||
public InsufficientParameters(String s, Command command, String parameterName) {
|
||||
super(s);
|
||||
public InsufficientParameters(Command command, String parameterName) {
|
||||
super("");
|
||||
this.command = command;
|
||||
this.parameterName = parameterName;
|
||||
}
|
||||
|
||||
@@ -4,6 +4,7 @@ import dev.sheldan.abstracto.core.command.config.Parameters;
|
||||
import dev.sheldan.abstracto.core.models.context.UserInitiatedServerContext;
|
||||
import lombok.Builder;
|
||||
import lombok.Getter;
|
||||
import lombok.Setter;
|
||||
import net.dv8tion.jda.api.JDA;
|
||||
import net.dv8tion.jda.api.entities.Guild;
|
||||
import net.dv8tion.jda.api.entities.Member;
|
||||
@@ -12,6 +13,7 @@ import net.dv8tion.jda.api.entities.TextChannel;
|
||||
|
||||
@Builder
|
||||
@Getter
|
||||
@Setter
|
||||
public class CommandContext {
|
||||
private TextChannel channel;
|
||||
private Guild guild;
|
||||
|
||||
@@ -94,16 +94,11 @@ public class AServer implements SnowFlake, Serializable {
|
||||
if (o == null || getClass() != o.getClass()) return false;
|
||||
AServer aServer = (AServer) o;
|
||||
return Objects.equals(id, aServer.id) &&
|
||||
Objects.equals(name, aServer.name) &&
|
||||
Objects.equals(roles, aServer.roles) &&
|
||||
Objects.equals(channels, aServer.channels) &&
|
||||
Objects.equals(channelGroups, aServer.channelGroups) &&
|
||||
Objects.equals(users, aServer.users) &&
|
||||
Objects.equals(emotes, aServer.emotes);
|
||||
Objects.equals(name, aServer.name);
|
||||
}
|
||||
|
||||
@Override
|
||||
public int hashCode() {
|
||||
return Objects.hash(id, name, roles, channels, channelGroups, users, emotes);
|
||||
return Objects.hash(id, name);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user