mirror of
https://github.com/Sheldan/abstracto.git
synced 2026-03-26 22:19:52 +00:00
added conventions for templated commands, to reduce the amount of necessary configuration
added an exception, in case the command was missing the correct types of parameters (channel is required, no channel was found) added fix text to exception handler, in case the message of an exception is null fixed off-by-one error when searching the missing parameter changed supported chanel from guildChannel to textChannel for posttarget added slowmode command
This commit is contained in:
@@ -34,7 +34,7 @@ public class CommandManager implements CommandRegistry {
|
||||
boolean paramCountFits = unParsedCommandParameter.getParameters().size() >= commandConfiguration.getNecessaryParameterCount();
|
||||
boolean hasRemainderParameter = commandConfiguration.getParameters().stream().anyMatch(Parameter::isRemainder);
|
||||
if(unParsedCommandParameter.getParameters().size() < commandConfiguration.getNecessaryParameterCount()) {
|
||||
String nextParameterName = commandConfiguration.getParameters().get(commandConfiguration.getNecessaryParameterCount()).getName();
|
||||
String nextParameterName = commandConfiguration.getParameters().get(commandConfiguration.getNecessaryParameterCount() - 1).getName();
|
||||
throw new InsufficientParametersException("Insufficient parameters", o, nextParameterName);
|
||||
}
|
||||
parameterFit = paramCountFits || hasRemainderParameter;
|
||||
|
||||
@@ -4,6 +4,8 @@ import dev.sheldan.abstracto.command.Command;
|
||||
import dev.sheldan.abstracto.command.PostCommandExecution;
|
||||
import dev.sheldan.abstracto.command.execution.*;
|
||||
import dev.sheldan.abstracto.command.meta.UnParsedCommandParameter;
|
||||
import dev.sheldan.abstracto.commands.management.exception.IncorrectParameterException;
|
||||
import dev.sheldan.abstracto.commands.management.exception.InsufficientParametersException;
|
||||
import dev.sheldan.abstracto.core.management.ChannelManagementService;
|
||||
import dev.sheldan.abstracto.core.management.ServerManagementService;
|
||||
import dev.sheldan.abstracto.core.models.AChannel;
|
||||
@@ -11,6 +13,7 @@ import dev.sheldan.abstracto.core.models.AServer;
|
||||
import net.dv8tion.jda.api.entities.GuildChannel;
|
||||
import net.dv8tion.jda.api.entities.Member;
|
||||
import net.dv8tion.jda.api.entities.Message;
|
||||
import net.dv8tion.jda.api.entities.TextChannel;
|
||||
import net.dv8tion.jda.api.events.message.MessageReceivedEvent;
|
||||
import net.dv8tion.jda.api.hooks.ListenerAdapter;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
@@ -18,9 +21,7 @@ import org.springframework.stereotype.Service;
|
||||
import org.springframework.transaction.annotation.Transactional;
|
||||
|
||||
import javax.annotation.Nonnull;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Arrays;
|
||||
import java.util.List;
|
||||
import java.util.*;
|
||||
|
||||
@Service
|
||||
public class CommandReceivedHandler extends ListenerAdapter {
|
||||
@@ -84,25 +85,30 @@ public class CommandReceivedHandler extends ListenerAdapter {
|
||||
|
||||
public Parameters getParsedParameters(UnParsedCommandParameter unParsedCommandParameter, Command command, Message message){
|
||||
List<Object> parsedParameters = new ArrayList<>();
|
||||
int mentionedChannelsCount = 0;
|
||||
int mentionedUserCount = 0;
|
||||
for (int i = 0; i < unParsedCommandParameter.getParameters().size(); i++) {
|
||||
Parameter param = command.getConfiguration().getParameters().get(i);
|
||||
String value = unParsedCommandParameter.getParameters().get(i);
|
||||
if(param.getType().equals(Integer.class)){
|
||||
parsedParameters.add(Integer.parseInt(value));
|
||||
} else if(param.getType().equals(Double.class)){
|
||||
parsedParameters.add(Double.parseDouble(value));
|
||||
} else if(param.getType().equals(GuildChannel.class)){
|
||||
parsedParameters.add(message.getMentionedChannels().get(mentionedChannelsCount));
|
||||
mentionedChannelsCount++;
|
||||
} else if(param.getType().equals(Member.class)) {
|
||||
parsedParameters.add(message.getMentionedMembers().get(mentionedUserCount));
|
||||
mentionedUserCount++;
|
||||
} else {
|
||||
parsedParameters.add(value);
|
||||
Iterator<TextChannel> channelIterator = message.getMentionedChannels().iterator();
|
||||
Iterator<Member> memberIterator = message.getMentionedMembers().iterator();
|
||||
for (int i = 0; i < unParsedCommandParameter.getParameters().size(); i++) {
|
||||
Parameter param = command.getConfiguration().getParameters().get(i);
|
||||
String value = unParsedCommandParameter.getParameters().get(i);
|
||||
try {
|
||||
if(param.getType().equals(Integer.class)){
|
||||
parsedParameters.add(Integer.parseInt(value));
|
||||
} else if(param.getType().equals(Double.class)){
|
||||
parsedParameters.add(Double.parseDouble(value));
|
||||
} else if(param.getType().equals(Long.class)){
|
||||
parsedParameters.add(Long.parseLong(value));
|
||||
} else if(param.getType().equals(TextChannel.class)){
|
||||
parsedParameters.add(channelIterator.next());
|
||||
} else if(param.getType().equals(Member.class)) {
|
||||
parsedParameters.add(memberIterator.next());
|
||||
} else {
|
||||
parsedParameters.add(value);
|
||||
}
|
||||
} catch (NoSuchElementException e) {
|
||||
throw new IncorrectParameterException("The passed parameters did not have the correct type.", command, param.getType(), param.getName());
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return Parameters.builder().parameters(parsedParameters).build();
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,33 @@
|
||||
package dev.sheldan.abstracto.commands.management.exception;
|
||||
|
||||
import dev.sheldan.abstracto.command.Command;
|
||||
import dev.sheldan.abstracto.command.TemplatedException;
|
||||
|
||||
import java.util.HashMap;
|
||||
|
||||
public class IncorrectParameterException extends RuntimeException implements TemplatedException {
|
||||
|
||||
private Command command;
|
||||
private String parameterName;
|
||||
private Class clazz;
|
||||
|
||||
public IncorrectParameterException(String s, Command command, Class expected, String parameterName) {
|
||||
super(s);
|
||||
this.command = command;
|
||||
this.parameterName = parameterName;
|
||||
this.clazz = expected;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getTemplateName() {
|
||||
return "incorrect_parameters";
|
||||
}
|
||||
|
||||
@Override
|
||||
public Object getTemplateModel() {
|
||||
HashMap<String, Object> model = new HashMap<>();
|
||||
model.put("parameterName", parameterName);
|
||||
model.put("class", this.clazz);
|
||||
return model;
|
||||
}
|
||||
}
|
||||
@@ -25,7 +25,7 @@ public class ExceptionPostExecution implements PostCommandExecution {
|
||||
String text = templateService.renderTemplate(exception.getTemplateName(), exception.getTemplateModel());
|
||||
commandContext.getChannel().sendMessage(text).queue();
|
||||
} else {
|
||||
commandContext.getChannel().sendMessage(result.getMessage()).queue();
|
||||
commandContext.getChannel().sendMessage("Exception: " + result.getThrowable().getClass() + ": " + result.getMessage()).queue();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1 @@
|
||||
The necessary parameters were not found. A '${class.simpleName}' was expected as '${parameterName}'. Consult help to see the correct syntax.
|
||||
Reference in New Issue
Block a user